1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs

235 lines
7.0 KiB
C#
Raw Normal View History

2013-03-29 01:07:09 +03:00
using System;
2011-10-24 08:54:09 +03:00
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
2011-11-13 21:16:31 +03:00
using NzbDrone.Test.Common;
2011-10-24 08:54:09 +03:00
namespace NzbDrone.Common.Test.DiskProviderTests
2011-10-24 08:54:09 +03:00
{
[TestFixture]
2013-04-18 02:32:53 +03:00
public class DiskProviderFixture : TestBase<DiskProvider>
2011-10-24 08:54:09 +03:00
{
2013-04-16 07:52:41 +03:00
DirectoryInfo _binFolder;
DirectoryInfo _binFolderCopy;
DirectoryInfo _binFolderMove;
2011-10-24 08:54:09 +03:00
[SetUp]
public void Setup()
{
2013-04-16 07:52:41 +03:00
_binFolder = new DirectoryInfo(Directory.GetCurrentDirectory());
_binFolderCopy = new DirectoryInfo(Path.Combine(_binFolder.Parent.FullName, "bin_copy"));
_binFolderMove = new DirectoryInfo(Path.Combine(_binFolder.Parent.FullName, "bin_move"));
2011-10-24 08:54:09 +03:00
2013-04-16 07:52:41 +03:00
if (_binFolderCopy.Exists)
2011-10-24 08:54:09 +03:00
{
foreach (var file in _binFolderCopy.GetFiles("*", SearchOption.AllDirectories))
{
file.Attributes = FileAttributes.Normal;
}
2013-04-16 07:52:41 +03:00
_binFolderCopy.Delete(true);
2011-10-24 08:54:09 +03:00
}
2013-04-16 07:52:41 +03:00
if (_binFolderMove.Exists)
{
2013-04-16 07:52:41 +03:00
_binFolderMove.Delete(true);
}
2011-10-24 08:54:09 +03:00
}
[Test]
public void directory_exist_should_be_able_to_find_existing_folder()
{
2013-04-18 02:32:53 +03:00
Subject.FolderExists(TempFolder).Should().BeTrue();
}
[Test]
public void directory_exist_should_be_able_to_find_existing_unc_share()
{
2013-07-26 08:00:57 +03:00
WindowsOnly();
2013-04-18 02:32:53 +03:00
Subject.FolderExists(@"\\localhost\c$").Should().BeTrue();
}
2013-04-16 07:52:41 +03:00
[Test]
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
{
2013-07-26 08:00:57 +03:00
Subject.FolderExists(@"C:\ThisBetterNotExist\".AsOsAgnostic()).Should().BeFalse();
}
[Test]
public void moveFile_should_overwrite_existing_file()
{
2013-04-18 02:32:53 +03:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 07:52:41 +03:00
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
2013-04-18 02:32:53 +03:00
Subject.MoveFile(_binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName, targetPath);
Subject.MoveFile(_binFolderCopy.GetFiles("*.pdb", SearchOption.AllDirectories).First().FullName, targetPath);
File.Exists(targetPath).Should().BeTrue();
}
[Test]
public void moveFile_should_not_move_overwrite_itself()
{
2013-04-18 02:32:53 +03:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 07:52:41 +03:00
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
2013-04-18 02:32:53 +03:00
Subject.MoveFile(targetPath, targetPath);
File.Exists(targetPath).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1);
}
2011-10-24 08:54:09 +03:00
[Test]
public void CopyFolder_should_copy_folder()
{
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy();
2011-10-24 08:54:09 +03:00
}
2011-10-24 08:54:09 +03:00
[Test]
public void CopyFolder_should_overwrite_existing_folder()
2011-10-24 08:54:09 +03:00
{
2013-04-16 07:52:41 +03:00
2011-10-24 08:54:09 +03:00
2013-04-18 02:32:53 +03:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2011-10-24 08:54:09 +03:00
//Delete Random File
2013-04-16 07:52:41 +03:00
_binFolderCopy.Refresh();
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2011-10-24 08:54:09 +03:00
VerifyCopy();
}
[Test]
public void MoveFolder_should_overwrite_existing_folder()
{
2013-04-18 02:32:53 +03:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
VerifyCopy();
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
2013-04-16 07:52:41 +03:00
VerifyMove();
}
[Test]
public void move_read_only_file()
{
var source = GetTestFilePath();
var destination = GetTestFilePath();
Subject.WriteAllText(source, "SourceFile");
Subject.WriteAllText(destination, "DestinationFile");
File.SetAttributes(source, FileAttributes.ReadOnly);
File.SetAttributes(destination, FileAttributes.ReadOnly);
Subject.MoveFile(source, destination);
}
[Test]
public void empty_folder_should_return_folder_modified_date()
{
var tempfolder = new DirectoryInfo(TempFolder);
2013-04-18 02:32:53 +03:00
Subject.GetLastFolderWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc);
}
[Test]
public void folder_should_return_correct_value_for_last_write()
{
var testFile = GetTestFilePath();
2013-07-27 07:34:11 +03:00
TestLogger.Info("Path is: {0}", testFile);
2013-07-27 07:34:11 +03:00
Subject.WriteAllText(testFile, "");
2013-07-27 07:34:11 +03:00
Subject.GetLastFolderWrite(SandboxFolder).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-1));
Subject.GetLastFolderWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow);
}
[Test]
public void should_return_false_for_unlocked_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
Subject.IsFileLocked(testFile).Should().BeFalse();
}
[Test]
public void should_return_false_for_unlocked_and_readonly_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
File.SetAttributes(testFile, FileAttributes.ReadOnly);
Subject.IsFileLocked(testFile).Should().BeFalse();
}
[Test]
public void should_return_true_for_unlocked_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
using (var file = File.OpenWrite(testFile))
{
Subject.IsFileLocked(testFile).Should().BeTrue();
}
}
[Test]
[Explicit]
public void check_last_write()
{
2013-07-26 08:00:57 +03:00
Console.WriteLine(Subject.GetLastFolderWrite(_binFolder.FullName));
Console.WriteLine(_binFolder.LastWriteTimeUtc);
}
private void VerifyCopy()
{
2013-04-16 07:52:41 +03:00
_binFolder.Refresh();
_binFolderCopy.Refresh();
2011-10-24 08:54:09 +03:00
2013-04-16 07:52:41 +03:00
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
2013-04-16 07:52:41 +03:00
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
2011-10-24 08:54:09 +03:00
}
private void VerifyMove()
{
2013-04-16 07:52:41 +03:00
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderMove.Refresh();
2013-04-16 07:52:41 +03:00
_binFolderCopy.Exists.Should().BeFalse();
2013-04-16 07:52:41 +03:00
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
2013-04-16 07:52:41 +03:00
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
}
2011-10-24 08:54:09 +03:00
}
}