1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-14 15:03:42 +01:00
Radarr/NzbDrone.Common.Test/DiskProviderFixture.cs

213 lines
6.6 KiB
C#
Raw Normal View History

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;
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
}
2013-04-16 06:52:41 +02:00
if (_binFolderMove.Exists)
{
2013-04-16 06:52:41 +02:00
_binFolderMove.Delete(true);
}
2011-10-24 07:54:09 +02: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();
}
[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();
}
2013-04-16 06:52:41 +02: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();
}
[Test]
public void moveFile_should_overwrite_existing_file()
{
2013-04-18 01:32:53 +02:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 06:52:41 +02:00
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
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);
File.Exists(targetPath).Should().BeTrue();
}
[Test]
public void moveFile_should_not_move_overwrite_itself()
{
2013-04-18 01:32:53 +02:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 06:52:41 +02:00
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
2013-04-18 01:32:53 +02:00
Subject.MoveFile(targetPath, targetPath);
File.Exists(targetPath).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1);
}
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
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2013-04-16 06:52:41 +02:00
2011-10-24 07:54:09 +02:00
VerifyCopy();
2011-10-24 07:54:09 +02:00
}
2011-10-24 07:54:09 +02:00
[Test]
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
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();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
2011-10-24 07:54:09 +02:00
VerifyCopy();
}
[Test]
public void MoveFolder_should_overwrite_existing_folder()
{
2013-04-18 01:32:53 +02:00
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
VerifyCopy();
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
2013-04-16 06:52:41 +02:00
VerifyMove();
}
[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\\")]
[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")]
public void paths_should_be_equal(string first, string second)
{
2013-04-18 01:32:53 +02:00
if (first.StartsWith("\\"))
{
//verify the linux equivalent.
DiskProvider.PathEquals(first, second).Should().BeTrue();
2013-04-18 01:32:53 +02:00
}
DiskProvider.PathEquals(first, second).Should().BeTrue();
}
[TestCase(@"D:\Test", @"C:\Test\")]
[TestCase(@"D:\Test\Test", @"C:\TestTest\")]
public void paths_should_not_be_equal(string first, string second)
{
DiskProvider.PathEquals(first, second).Should().BeFalse();
}
[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);
}
[Test]
public void folder_should_return_correct_value_for_last_write()
{
var appPath = new AppFolderInfo(Subject).AppDataFolder;
TestLogger.Info("Path is: {0}", appPath);
Subject.WriteAllText(Path.Combine(appPath, "newfile.txt"), "");
2013-04-18 01:32:53 +02:00
Subject.GetLastFolderWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10));
Subject.GetLastFolderWrite(appPath).Should().BeBefore(DateTime.UtcNow);
}
[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);
}
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));
2013-04-16 06:52:41 +02:00
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
2011-10-24 07:54:09 +02:00
}
private void VerifyMove()
{
2013-04-16 06:52:41 +02:00
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderMove.Refresh();
2013-04-16 06:52:41 +02:00
_binFolderCopy.Exists.Should().BeFalse();
2013-04-16 06:52:41 +02:00
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
2013-04-16 06:52:41 +02:00
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
}
2011-10-24 07:54:09 +02:00
}
}