2011-10-24 08:54:09 +03:00
|
|
|
// ReSharper disable InconsistentNaming
|
2012-01-19 06:57:27 +03:00
|
|
|
|
2012-01-23 07:59:23 +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
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2012-01-19 06:57:27 +03:00
|
|
|
public class DiskProviderFixture : TestBase
|
2011-10-24 08:54:09 +03:00
|
|
|
{
|
|
|
|
DirectoryInfo BinFolder;
|
|
|
|
DirectoryInfo BinFolderCopy;
|
2011-11-18 09:52:50 +03:00
|
|
|
DirectoryInfo BinFolderMove;
|
2011-10-24 08:54:09 +03:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
var binRoot = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent;
|
|
|
|
BinFolder = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin"));
|
|
|
|
BinFolderCopy = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_copy"));
|
2011-11-18 09:52:50 +03:00
|
|
|
BinFolderMove = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_move"));
|
2011-10-24 08:54:09 +03:00
|
|
|
|
|
|
|
if (BinFolderCopy.Exists)
|
|
|
|
{
|
|
|
|
BinFolderCopy.Delete(true);
|
|
|
|
}
|
2011-11-18 09:52:50 +03:00
|
|
|
|
|
|
|
if (BinFolderMove.Exists)
|
|
|
|
{
|
|
|
|
BinFolderMove.Delete(true);
|
|
|
|
}
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
|
2012-01-19 06:57:27 +03:00
|
|
|
[Test]
|
|
|
|
public void directory_exist_should_be_able_to_find_existing_folder()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<DiskProvider>().FolderExists(TempFolder).Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void directory_exist_should_be_able_to_find_existing_unc_share()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<DiskProvider>().FolderExists(@"\\localhost\c$").Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<DiskProvider>().FolderExists(@"C:\ThisBetterNotExist\").Should().BeFalse();
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:16:05 +03:00
|
|
|
[Test]
|
|
|
|
public void moveFile_should_overwrite_existing_file()
|
|
|
|
{
|
|
|
|
var diskProvider = new DiskProvider();
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
|
|
|
|
var targetPath = Path.Combine(BinFolderCopy.FullName, "file.move");
|
|
|
|
|
|
|
|
diskProvider.MoveFile(BinFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName, targetPath);
|
2012-09-29 08:09:07 +03:00
|
|
|
diskProvider.MoveFile(BinFolderCopy.GetFiles("*.pdb", SearchOption.AllDirectories).First().FullName, targetPath);
|
2011-11-18 10:16:05 +03:00
|
|
|
|
|
|
|
File.Exists(targetPath).Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
2011-12-14 11:06:54 +03:00
|
|
|
[Test]
|
|
|
|
public void moveFile_should_not_move_overwrite_itself()
|
|
|
|
{
|
|
|
|
var diskProvider = new DiskProvider();
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
|
|
|
|
var targetPath = BinFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
|
|
|
|
|
|
|
|
diskProvider.MoveFile(targetPath, targetPath);
|
|
|
|
|
|
|
|
File.Exists(targetPath).Should().BeTrue();
|
2011-12-20 03:58:26 +03:00
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-12-14 11:06:54 +03:00
|
|
|
}
|
|
|
|
|
2011-10-24 08:54:09 +03:00
|
|
|
[Test]
|
|
|
|
public void CopyFolder_should_copy_folder()
|
|
|
|
{
|
|
|
|
//Act
|
|
|
|
var diskProvider = new DiskProvider();
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
|
|
|
|
//Assert
|
2011-11-14 07:05:33 +03:00
|
|
|
VerifyCopy();
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:16:05 +03:00
|
|
|
|
2011-10-24 08:54:09 +03:00
|
|
|
[Test]
|
|
|
|
public void CopyFolder_should_overright_existing_folder()
|
|
|
|
{
|
|
|
|
//Act
|
|
|
|
var diskProvider = new DiskProvider();
|
|
|
|
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
|
|
|
|
//Delete Random File
|
2011-11-14 07:05:33 +03:00
|
|
|
BinFolderCopy.Refresh();
|
|
|
|
BinFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
|
2011-10-24 08:54:09 +03:00
|
|
|
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
|
|
|
|
//Assert
|
2011-11-14 07:05:33 +03:00
|
|
|
VerifyCopy();
|
|
|
|
}
|
|
|
|
|
2011-11-18 09:52:50 +03:00
|
|
|
[Test]
|
|
|
|
public void MoveFolder_should_overright_existing_folder()
|
|
|
|
{
|
|
|
|
var diskProvider = new DiskProvider();
|
|
|
|
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
|
|
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderMove.FullName);
|
|
|
|
VerifyCopy();
|
|
|
|
|
|
|
|
//Act
|
|
|
|
diskProvider.MoveDirectory(BinFolderCopy.FullName, BinFolderMove.FullName);
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
VerifyMove();
|
|
|
|
}
|
|
|
|
|
2011-12-10 22:22:47 +03:00
|
|
|
|
|
|
|
[TestCase(@"C:\", @"C:\")]
|
|
|
|
[TestCase(@"C:\\", @"C:\")]
|
|
|
|
[TestCase(@"c:\", @"C:\")]
|
|
|
|
[TestCase(@"c:\Test", @"C:\Test\\")]
|
|
|
|
[TestCase(@"c:\\\\\Test", @"C:\Test\\")]
|
|
|
|
[TestCase(@"c:\Test\\\\", @"C:\Test\\")]
|
|
|
|
[TestCase(@"c:\Test", @"C:\Test\\")]
|
2012-01-20 08:50:05 +03:00
|
|
|
[TestCase(@"\\Server\pool", @"\\Server\pool")]
|
|
|
|
[TestCase(@"\\Server\pool\", @"\\Server\pool")]
|
|
|
|
[TestCase(@"\\Server\pool", @"\\Server\pool\")]
|
|
|
|
[TestCase(@"\\Server\pool\", @"\\Server\pool\")]
|
|
|
|
[TestCase(@"\\smallcheese\DRIVE_G\TV-C\Simspsons", @"\\smallcheese\DRIVE_G\TV-C\Simspsons")]
|
2011-12-10 22:22:47 +03:00
|
|
|
public void paths_should_be_equeal(string first, string second)
|
|
|
|
{
|
|
|
|
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase(@"D:\Test", @"C:\Test\")]
|
|
|
|
[TestCase(@"D:\Test\Test", @"C:\TestTest\")]
|
|
|
|
public void paths_should_not_be_equeal(string first, string second)
|
|
|
|
{
|
|
|
|
DiskProvider.PathEquals(first, second).Should().BeFalse();
|
|
|
|
}
|
|
|
|
|
2012-01-23 07:59:23 +03:00
|
|
|
[Test]
|
|
|
|
public void empty_folder_should_return_folder_modified_date()
|
|
|
|
{
|
|
|
|
var tempfolder = new DirectoryInfo(TempFolder);
|
|
|
|
Mocker.Resolve<DiskProvider>().GetLastDirectoryWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void folder_should_return_correct_value_for_last_write()
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
var appPath = new EnvironmentProvider().ApplicationPath;
|
2012-01-23 07:59:23 +03:00
|
|
|
Mocker.Resolve<DiskProvider>().GetLastDirectoryWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10));
|
|
|
|
Mocker.Resolve<DiskProvider>().GetLastDirectoryWrite(appPath).Should().BeBefore(DateTime.UtcNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[Explicit]
|
|
|
|
public void check_last_write()
|
|
|
|
{
|
|
|
|
Console.WriteLine(Mocker.Resolve<DiskProvider>().GetLastDirectoryWrite(@"C:\DRIVERS"));
|
|
|
|
Console.WriteLine(new DirectoryInfo(@"C:\DRIVERS").LastWriteTimeUtc);
|
|
|
|
}
|
|
|
|
|
2012-10-17 10:39:06 +03:00
|
|
|
[Test]
|
|
|
|
public void IsChildOfPath_should_return_true_when_it_is_a_child()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<DiskProvider>().IsChildOfPath(@"C:\Test\TV", @"C:\Test").Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void IsChildOfPath_should_return_false_when_it_is_not_a_child()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<DiskProvider>().IsChildOfPath(@"C:\NOT_Test\TV", @"C:\Test").Should().BeFalse();
|
|
|
|
}
|
|
|
|
|
2011-11-14 07:05:33 +03:00
|
|
|
private void VerifyCopy()
|
|
|
|
{
|
2011-10-24 08:54:09 +03:00
|
|
|
BinFolder.Refresh();
|
|
|
|
BinFolderCopy.Refresh();
|
|
|
|
|
2011-11-14 07:05:33 +03:00
|
|
|
BinFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
|
|
|
|
.Should().HaveSameCount(BinFolder.GetFiles("*.*", SearchOption.AllDirectories));
|
|
|
|
|
|
|
|
BinFolderCopy.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories());
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
2011-11-18 09:52:50 +03:00
|
|
|
|
|
|
|
private void VerifyMove()
|
|
|
|
{
|
|
|
|
BinFolder.Refresh();
|
|
|
|
BinFolderCopy.Refresh();
|
|
|
|
BinFolderMove.Refresh();
|
|
|
|
|
|
|
|
BinFolderCopy.Exists.Should().BeFalse();
|
|
|
|
|
|
|
|
BinFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
|
|
|
|
.Should().HaveSameCount(BinFolder.GetFiles("*.*", SearchOption.AllDirectories));
|
|
|
|
|
|
|
|
BinFolderMove.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories());
|
|
|
|
}
|
2011-10-24 08:54:09 +03:00
|
|
|
}
|
|
|
|
}
|