Lidarr/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs

186 lines
5.4 KiB
C#
Raw Normal View History

2013-03-28 22:07:09 +00:00
using System;
2011-10-24 05:54:09 +00:00
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
2011-11-13 18:16:31 +00:00
using NzbDrone.Test.Common;
2011-10-24 05:54:09 +00:00
namespace NzbDrone.Common.Test.DiskProviderTests
2011-10-24 05:54:09 +00:00
{
[TestFixture]
2013-04-17 23:32:53 +00:00
public class DiskProviderFixture : TestBase<DiskProvider>
2011-10-24 05:54:09 +00:00
{
2013-04-16 04:52:41 +00:00
DirectoryInfo _binFolder;
DirectoryInfo _binFolderCopy;
DirectoryInfo _binFolderMove;
2011-10-24 05:54:09 +00:00
[SetUp]
public void Setup()
{
2013-04-16 04:52:41 +00: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 05:54:09 +00:00
2013-04-16 04:52:41 +00:00
if (_binFolderCopy.Exists)
2011-10-24 05:54:09 +00:00
{
2013-04-16 04:52:41 +00:00
_binFolderCopy.Delete(true);
2011-10-24 05:54:09 +00:00
}
2013-04-16 04:52:41 +00:00
if (_binFolderMove.Exists)
{
2013-04-16 04:52:41 +00:00
_binFolderMove.Delete(true);
}
2011-10-24 05:54:09 +00:00
}
[Test]
public void directory_exist_should_be_able_to_find_existing_folder()
{
2013-04-17 23:32:53 +00:00
Subject.FolderExists(TempFolder).Should().BeTrue();
}
[Test]
public void directory_exist_should_be_able_to_find_existing_unc_share()
{
2013-07-26 05:00:57 +00:00
WindowsOnly();
2013-04-17 23:32:53 +00:00
Subject.FolderExists(@"\\localhost\c$").Should().BeTrue();
}
2013-04-16 04:52:41 +00:00
[Test]
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
{
2013-07-26 05:00:57 +00:00
Subject.FolderExists(@"C:\ThisBetterNotExist\".AsOsAgnostic()).Should().BeFalse();
}
[Test]
public void moveFile_should_overwrite_existing_file()
{
2013-04-17 23:32:53 +00:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 04:52:41 +00:00
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
2013-04-17 23:32:53 +00: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-17 23:32:53 +00:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 04:52:41 +00:00
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
2013-04-17 23:32:53 +00:00
Subject.MoveFile(targetPath, targetPath);
File.Exists(targetPath).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1);
}
2011-10-24 05:54:09 +00:00
[Test]
public void CopyFolder_should_copy_folder()
{
2013-04-16 04:52:41 +00:00
2013-04-17 23:32:53 +00:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 04:52:41 +00:00
2011-10-24 05:54:09 +00:00
VerifyCopy();
2011-10-24 05:54:09 +00:00
}
2011-10-24 05:54:09 +00:00
[Test]
public void CopyFolder_should_overwrite_existing_folder()
2011-10-24 05:54:09 +00:00
{
2013-04-16 04:52:41 +00:00
2011-10-24 05:54:09 +00:00
2013-04-17 23:32:53 +00:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2011-10-24 05:54:09 +00:00
//Delete Random File
2013-04-16 04:52:41 +00:00
_binFolderCopy.Refresh();
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2011-10-24 05:54:09 +00:00
VerifyCopy();
}
[Test]
public void MoveFolder_should_overwrite_existing_folder()
{
2013-04-17 23:32:53 +00:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
VerifyCopy();
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
2013-04-16 04:52:41 +00:00
VerifyMove();
}
[Test]
public void empty_folder_should_return_folder_modified_date()
{
var tempfolder = new DirectoryInfo(TempFolder);
2013-04-17 23:32:53 +00:00
Subject.GetLastFolderWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc);
}
[Test]
public void folder_should_return_correct_value_for_last_write()
{
2013-07-27 04:34:11 +00:00
var testFile = Path.Combine(SandboxFolder, "newfile.txt");
2013-07-27 04:34:11 +00:00
TestLogger.Info("Path is: {0}", testFile);
2013-07-27 04:34:11 +00:00
Subject.WriteAllText(testFile, "");
2013-07-27 04:34:11 +00:00
Subject.GetLastFolderWrite(SandboxFolder).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-1));
Subject.GetLastFolderWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow);
}
[Test]
[Explicit]
public void check_last_write()
{
2013-07-26 05:00:57 +00:00
Console.WriteLine(Subject.GetLastFolderWrite(_binFolder.FullName));
Console.WriteLine(_binFolder.LastWriteTimeUtc);
}
private void VerifyCopy()
{
2013-04-16 04:52:41 +00:00
_binFolder.Refresh();
_binFolderCopy.Refresh();
2011-10-24 05:54:09 +00:00
2013-04-16 04:52:41 +00:00
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
2013-04-16 04:52:41 +00:00
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
2011-10-24 05:54:09 +00:00
}
private void VerifyMove()
{
2013-04-16 04:52:41 +00:00
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderMove.Refresh();
2013-04-16 04:52:41 +00:00
_binFolderCopy.Exists.Should().BeFalse();
2013-04-16 04:52:41 +00:00
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
2013-04-16 04:52:41 +00:00
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
}
2011-10-24 05:54:09 +00:00
}
}