You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Prowlarr/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs

186 lines
5.4 KiB

12 years ago
using System;
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.DiskProviderTests
{
[TestFixture]
12 years ago
public class DiskProviderFixture : TestBase<DiskProvider>
{
12 years ago
DirectoryInfo _binFolder;
DirectoryInfo _binFolderCopy;
DirectoryInfo _binFolderMove;
[SetUp]
public void Setup()
{
12 years ago
_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"));
12 years ago
if (_binFolderCopy.Exists)
{
12 years ago
_binFolderCopy.Delete(true);
}
12 years ago
if (_binFolderMove.Exists)
{
12 years ago
_binFolderMove.Delete(true);
}
}
[Test]
public void directory_exist_should_be_able_to_find_existing_folder()
{
12 years ago
Subject.FolderExists(TempFolder).Should().BeTrue();
}
[Test]
public void directory_exist_should_be_able_to_find_existing_unc_share()
{
WindowsOnly();
12 years ago
Subject.FolderExists(@"\\localhost\c$").Should().BeTrue();
}
12 years ago
[Test]
public void directory_exist_should_not_be_able_to_find_none_existing_folder()
{
Subject.FolderExists(@"C:\ThisBetterNotExist\".AsOsAgnostic()).Should().BeFalse();
}
[Test]
public void moveFile_should_overwrite_existing_file()
{
12 years ago
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
12 years ago
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
12 years ago
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()
{
12 years ago
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
12 years ago
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
12 years ago
Subject.MoveFile(targetPath, targetPath);
File.Exists(targetPath).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public void CopyFolder_should_copy_folder()
{
12 years ago
12 years ago
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
12 years ago
VerifyCopy();
}
[Test]
public void CopyFolder_should_overwrite_existing_folder()
{
12 years ago
12 years ago
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
//Delete Random File
12 years ago
_binFolderCopy.Refresh();
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy();
}
[Test]
public void MoveFolder_should_overwrite_existing_folder()
{
12 years ago
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
VerifyCopy();
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
12 years ago
VerifyMove();
}
[Test]
public void empty_folder_should_return_folder_modified_date()
{
var tempfolder = new DirectoryInfo(TempFolder);
12 years ago
Subject.GetLastFolderWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc);
}
[Test]
public void folder_should_return_correct_value_for_last_write()
{
11 years ago
var testFile = Path.Combine(SandboxFolder, "newfile.txt");
11 years ago
TestLogger.Info("Path is: {0}", testFile);
11 years ago
Subject.WriteAllText(testFile, "");
11 years ago
Subject.GetLastFolderWrite(SandboxFolder).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-1));
Subject.GetLastFolderWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow);
}
[Test]
[Explicit]
public void check_last_write()
{
Console.WriteLine(Subject.GetLastFolderWrite(_binFolder.FullName));
Console.WriteLine(_binFolder.LastWriteTimeUtc);
}
private void VerifyCopy()
{
12 years ago
_binFolder.Refresh();
_binFolderCopy.Refresh();
12 years ago
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
12 years ago
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
}
private void VerifyMove()
{
12 years ago
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderMove.Refresh();
12 years ago
_binFolderCopy.Exists.Should().BeFalse();
12 years ago
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
12 years ago
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
}
}
}