2013-03-28 23:07:09 +01:00
|
|
|
|
using System;
|
2011-10-24 07:54:09 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NUnit.Framework;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2011-11-13 19:16:31 +01:00
|
|
|
|
using NzbDrone.Test.Common;
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2013-04-18 01:32:53 +02:00
|
|
|
|
public class DiskProviderFixture : TestBase<DiskProvider>
|
2011-10-24 07:54:09 +02:00
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
DirectoryInfo _binFolder;
|
|
|
|
|
DirectoryInfo _binFolderCopy;
|
|
|
|
|
DirectoryInfo _binFolderMove;
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2013-04-16 06:52:41 +02: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 07:54:09 +02:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
if (_binFolderCopy.Exists)
|
2011-10-24 07:54:09 +02:00
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderCopy.Delete(true);
|
2011-10-24 07:54:09 +02:00
|
|
|
|
}
|
2011-11-18 07:52:50 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
if (_binFolderMove.Exists)
|
2011-11-18 07:52:50 +01:00
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderMove.Delete(true);
|
2011-11-18 07:52:50 +01:00
|
|
|
|
}
|
2011-10-24 07:54:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 04:57:27 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void directory_exist_should_be_able_to_find_existing_folder()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.FolderExists(TempFolder).Should().BeTrue();
|
2012-01-19 04:57:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void directory_exist_should_be_able_to_find_existing_unc_share()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.FolderExists(@"\\localhost\c$").Should().BeTrue();
|
2012-01-19 04:57:27 +01:00
|
|
|
|
}
|
2013-04-16 06:52:41 +02:00
|
|
|
|
|
2012-01-19 04:57:27 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.FolderExists(@"C:\ThisBetterNotExist\").Should().BeFalse();
|
2012-01-19 04:57:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 08:16:05 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void moveFile_should_overwrite_existing_file()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
2011-11-18 08:16:05 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
|
2011-11-18 08:16:05 +01:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.MoveFile(_binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName, targetPath);
|
|
|
|
|
Subject.MoveFile(_binFolderCopy.GetFiles("*.pdb", SearchOption.AllDirectories).First().FullName, targetPath);
|
2011-11-18 08:16:05 +01:00
|
|
|
|
|
|
|
|
|
File.Exists(targetPath).Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-14 09:06:54 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void moveFile_should_not_move_overwrite_itself()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
2011-12-14 09:06:54 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
|
2011-12-14 09:06:54 +01:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.MoveFile(targetPath, targetPath);
|
2011-12-14 09:06:54 +01:00
|
|
|
|
|
|
|
|
|
File.Exists(targetPath).Should().BeTrue();
|
2011-12-20 01:58:26 +01:00
|
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-12-14 09:06:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 07:54:09 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void CopyFolder_should_copy_folder()
|
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
2013-04-16 06:52:41 +02:00
|
|
|
|
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
2011-11-14 05:05:33 +01:00
|
|
|
|
VerifyCopy();
|
2011-10-24 07:54:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 08:16:05 +01:00
|
|
|
|
|
2011-10-24 07:54:09 +02:00
|
|
|
|
[Test]
|
2013-05-23 02:29:41 +02:00
|
|
|
|
public void CopyFolder_should_overwrite_existing_folder()
|
2011-10-24 07:54:09 +02:00
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
//Delete Random File
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderCopy.Refresh();
|
|
|
|
|
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
|
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
|
2011-11-14 05:05:33 +01:00
|
|
|
|
VerifyCopy();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 07:52:50 +01:00
|
|
|
|
[Test]
|
2013-05-23 02:29:41 +02:00
|
|
|
|
public void MoveFolder_should_overwrite_existing_folder()
|
2011-11-18 07:52:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
|
|
|
|
|
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
|
2011-11-18 07:52:50 +01:00
|
|
|
|
VerifyCopy();
|
|
|
|
|
|
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
|
2013-04-16 06:52:41 +02:00
|
|
|
|
|
|
|
|
|
|
2011-11-18 07:52:50 +01:00
|
|
|
|
VerifyMove();
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 20:22:47 +01: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 06:50:05 +01: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")]
|
2013-05-23 02:29:41 +02:00
|
|
|
|
public void paths_should_be_equal(string first, string second)
|
2011-12-10 20:22:47 +01:00
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
if (first.StartsWith("\\"))
|
|
|
|
|
{
|
|
|
|
|
//verify the linux equivalent.
|
2013-05-25 00:18:37 +02:00
|
|
|
|
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
2013-04-18 01:32:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-10 20:22:47 +01:00
|
|
|
|
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase(@"D:\Test", @"C:\Test\")]
|
|
|
|
|
[TestCase(@"D:\Test\Test", @"C:\TestTest\")]
|
2013-05-23 02:29:41 +02:00
|
|
|
|
public void paths_should_not_be_equal(string first, string second)
|
2011-12-10 20:22:47 +01:00
|
|
|
|
{
|
|
|
|
|
DiskProvider.PathEquals(first, second).Should().BeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 05:59:23 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void empty_folder_should_return_folder_modified_date()
|
|
|
|
|
{
|
|
|
|
|
var tempfolder = new DirectoryInfo(TempFolder);
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.GetLastFolderWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc);
|
2012-01-23 05:59:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void folder_should_return_correct_value_for_last_write()
|
|
|
|
|
{
|
2013-07-05 06:43:28 +02:00
|
|
|
|
var appPath = new AppFolderInfo(Subject).AppDataFolder;
|
2013-05-25 00:18:37 +02:00
|
|
|
|
|
2013-05-23 02:29:41 +02:00
|
|
|
|
TestLogger.Info("Path is: {0}", appPath);
|
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
Subject.WriteAllText(Path.Combine(appPath, "newfile.txt"), "");
|
2013-05-25 00:18:37 +02:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Subject.GetLastFolderWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10));
|
|
|
|
|
Subject.GetLastFolderWrite(appPath).Should().BeBefore(DateTime.UtcNow);
|
2012-01-23 05:59:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
[Explicit]
|
|
|
|
|
public void check_last_write()
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
Console.WriteLine(Subject.GetLastFolderWrite(@"C:\DRIVERS"));
|
2013-04-16 06:52:41 +02:00
|
|
|
|
Console.WriteLine(new DirectoryInfo(@"C:\DRIVERS").LastWriteTimeUtc);
|
2012-01-23 05:59:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 05:05:33 +01:00
|
|
|
|
private void VerifyCopy()
|
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolder.Refresh();
|
|
|
|
|
_binFolderCopy.Refresh();
|
2011-10-24 07:54:09 +02:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
|
|
|
|
|
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
|
2011-11-14 05:05:33 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
|
2011-10-24 07:54:09 +02:00
|
|
|
|
}
|
2011-11-18 07:52:50 +01:00
|
|
|
|
|
|
|
|
|
private void VerifyMove()
|
|
|
|
|
{
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolder.Refresh();
|
|
|
|
|
_binFolderCopy.Refresh();
|
|
|
|
|
_binFolderMove.Refresh();
|
2011-11-18 07:52:50 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderCopy.Exists.Should().BeFalse();
|
2011-11-18 07:52:50 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
|
|
|
|
|
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
|
2011-11-18 07:52:50 +01:00
|
|
|
|
|
2013-04-16 06:52:41 +02:00
|
|
|
|
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
|
2011-11-18 07:52:50 +01:00
|
|
|
|
}
|
2011-10-24 07:54:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|