Updated tests to work better with VS2013 Test Adapter.

pull/2/head
Taloth Saldono 11 years ago
parent 388943ea1b
commit 79b2b14668

@ -10,30 +10,26 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{
public class DiskProviderFixtureBase<TSubject> : TestBase<TSubject> where TSubject : class, IDiskProvider
{
DirectoryInfo _binFolder;
DirectoryInfo _binFolderCopy;
DirectoryInfo _binFolderMove;
[SetUp]
public void Setup()
{
_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"));
if (_binFolderCopy.Exists)
{
foreach (var file in _binFolderCopy.GetFiles("*", SearchOption.AllDirectories))
{
file.Attributes = FileAttributes.Normal;
}
_binFolderCopy.Delete(true);
}
if (_binFolderMove.Exists)
public DirectoryInfo GetFilledTempFolder()
{
_binFolderMove.Delete(true);
}
var tempFolder = GetTempFilePath();
Directory.CreateDirectory(tempFolder);
File.WriteAllText(Path.Combine(tempFolder, Path.GetRandomFileName()), "RootFile");
var subDir = Path.Combine(tempFolder, Path.GetRandomFileName());
Directory.CreateDirectory(subDir);
File.WriteAllText(Path.Combine(subDir, Path.GetRandomFileName()), "SubFile1");
File.WriteAllText(Path.Combine(subDir, Path.GetRandomFileName()), "SubFile2");
return new DirectoryInfo(tempFolder);
}
[Test]
@ -57,79 +53,94 @@ namespace NzbDrone.Common.Test.DiskProviderTests
}
[Test]
public void moveFile_should_overwrite_existing_file()
public void MoveFile_should_overwrite_existing_file()
{
var source1 = GetTempFilePath();
var source2 = GetTempFilePath();
var destination = GetTempFilePath();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
File.WriteAllText(source1, "SourceFile1");
File.WriteAllText(source2, "SourceFile2");
var targetPath = Path.Combine(_binFolderCopy.FullName, "file.move");
Subject.MoveFile(source1, destination);
Subject.MoveFile(source2, destination);
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();
File.Exists(destination).Should().BeTrue();
}
[Test]
public void moveFile_should_not_move_overwrite_itself()
public void MoveFile_should_not_move_overwrite_itself()
{
var source = GetTempFilePath();
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
var targetPath = _binFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName;
File.WriteAllText(source, "SourceFile1");
Subject.MoveFile(targetPath, targetPath);
Subject.MoveFile(source, source);
File.Exists(targetPath).Should().BeTrue();
File.Exists(source).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public void CopyFolder_should_copy_folder()
{
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy();
var source = GetFilledTempFolder();
var destination = new DirectoryInfo(GetTempFilePath());
Subject.CopyFolder(source.FullName, destination.FullName);
VerifyCopy(source.FullName, destination.FullName);
}
[Test]
public void CopyFolder_should_overwrite_existing_folder()
{
var source = GetFilledTempFolder();
var destination = new DirectoryInfo(GetTempFilePath());
Subject.CopyFolder(source.FullName, destination.FullName);
//Delete Random File
destination.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
Subject.CopyFolder(source.FullName, destination.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy(source.FullName, destination.FullName);
}
//Delete Random File
_binFolderCopy.Refresh();
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories).First().Delete();
[Test]
public void MoveFolder_should_move_folder()
{
var original = GetFilledTempFolder();
var source = new DirectoryInfo(GetTempFilePath());
var destination = new DirectoryInfo(GetTempFilePath());
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(original.FullName, source.FullName);
Subject.MoveFolder(source.FullName, destination.FullName);
VerifyCopy();
VerifyMove(original.FullName, source.FullName, destination.FullName);
}
[Test]
public void MoveFolder_should_overwrite_existing_folder()
{
var original = GetFilledTempFolder();
var source = new DirectoryInfo(GetTempFilePath());
var destination = new DirectoryInfo(GetTempFilePath());
Subject.CopyFolder(original.FullName, source.FullName);
Subject.CopyFolder(original.FullName, destination.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
Subject.CopyFolder(_binFolder.FullName, _binFolderMove.FullName);
VerifyCopy();
Subject.MoveFolder(source.FullName, destination.FullName);
Subject.MoveFolder(_binFolderCopy.FullName, _binFolderMove.FullName);
VerifyMove();
VerifyMove(original.FullName, source.FullName, destination.FullName);
}
[Test]
public void move_read_only_file()
{
var source = GetTestFilePath();
var destination = GetTestFilePath();
var source = GetTempFilePath();
var destination = GetTempFilePath();
Subject.WriteAllText(source, "SourceFile");
Subject.WriteAllText(destination, "DestinationFile");
@ -150,23 +161,25 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test]
public void folder_should_return_correct_value_for_last_write()
{
var testDir = Path.Combine(SandboxFolder, "LastWrite");
var testDir = GetTempFilePath();
var testFile = Path.Combine(testDir, Path.GetRandomFileName());
Directory.CreateDirectory(testDir);
Subject.FolderSetLastWriteTimeUtc(TempFolder, DateTime.UtcNow.AddMinutes(-5));
TestLogger.Info("Path is: {0}", testFile);
Subject.WriteAllText(testFile, "Test");
Subject.FolderGetLastWrite(SandboxFolder).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-1));
Subject.FolderGetLastWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow.AddMinutes(1));
Subject.FolderGetLastWrite(TempFolder).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-1));
Subject.FolderGetLastWrite(TempFolder).Should().BeBefore(DateTime.UtcNow.AddMinutes(1));
}
[Test]
public void should_return_false_for_unlocked_file()
{
var testFile = GetTestFilePath();
var testFile = GetTempFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
Subject.IsFileLocked(testFile).Should().BeFalse();
@ -175,7 +188,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test]
public void should_return_false_for_unlocked_and_readonly_file()
{
var testFile = GetTestFilePath();
var testFile = GetTempFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
File.SetAttributes(testFile, FileAttributes.ReadOnly);
@ -186,7 +199,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test]
public void should_return_true_for_unlocked_file()
{
var testFile = GetTestFilePath();
var testFile = GetTempFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
using (var file = File.OpenWrite(testFile))
@ -198,7 +211,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test]
public void should_be_able_to_set_permission_from_parrent()
{
var testFile = GetTestFilePath();
var testFile = GetTempFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
Subject.InheritFolderPermissions(testFile);
@ -208,33 +221,26 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Explicit]
public void check_last_write()
{
Console.WriteLine(Subject.FolderGetLastWrite(_binFolder.FullName));
Console.WriteLine(_binFolder.LastWriteTimeUtc);
Console.WriteLine(Subject.FolderGetLastWrite(GetFilledTempFolder().FullName));
Console.WriteLine(GetFilledTempFolder().LastWriteTimeUtc);
}
private void VerifyCopy()
private void VerifyCopy(string source, string destination)
{
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderCopy.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
var sourceFiles = Directory.GetFileSystemEntries(source, "*", SearchOption.AllDirectories).Select(v => v.Substring(source.Length + 1)).ToArray();
var destFiles = Directory.GetFileSystemEntries(destination, "*", SearchOption.AllDirectories).Select(v => v.Substring(destination.Length + 1)).ToArray();
_binFolderCopy.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
CollectionAssert.AreEquivalent(sourceFiles, destFiles);
}
private void VerifyMove()
private void VerifyMove(string source, string from, string destination)
{
_binFolder.Refresh();
_binFolderCopy.Refresh();
_binFolderMove.Refresh();
_binFolderCopy.Exists.Should().BeFalse();
Directory.Exists(from).Should().BeFalse();
_binFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
.Should().HaveSameCount(_binFolder.GetFiles("*.*", SearchOption.AllDirectories));
var sourceFiles = Directory.GetFileSystemEntries(source, "*", SearchOption.AllDirectories).Select(v => v.Substring(source.Length + 1)).ToArray();
var destFiles = Directory.GetFileSystemEntries(destination, "*", SearchOption.AllDirectories).Select(v => v.Substring(destination.Length + 1)).ToArray();
_binFolderMove.GetDirectories().Should().HaveSameCount(_binFolder.GetDirectories());
CollectionAssert.AreEquivalent(sourceFiles, destFiles);
}
}
}

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Disk;
@ -61,7 +62,17 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{
WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvailableSpace(@"Z:\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic()));
// Find a drive that doesn't exist.
for (char driveletter = 'Z'; driveletter > 'D' ; driveletter--)
{
if (new DriveInfo(driveletter.ToString()).IsReady)
continue;
Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvailableSpace(driveletter + @":\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic()));
return;
}
Assert.Inconclusive("No drive available for testing.");
}
[Test]

@ -120,7 +120,7 @@ namespace NzbDrone.Common.Test
public void get_actual_casing_should_return_actual_casing_for_local_file_in_windows()
{
WindowsOnly();
var path = Process.GetCurrentProcess().MainModule.FileName;
var path = Environment.ExpandEnvironmentVariables("%SystemRoot%\\System32");
path.ToUpper().GetActualCasing().Should().Be(path);
path.ToLower().GetActualCasing().Should().Be(path);
}

@ -4,6 +4,7 @@ using System.Text;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test
{
@ -22,10 +23,11 @@ namespace NzbDrone.Core.Test
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void WithDefault_Fail()
{
"test".WithDefault(null);
Assert.Throws<ArgumentNullException>(() => "test".WithDefault(null));
ExceptionVerification.IgnoreWarns();
}
[Test]

@ -13,12 +13,10 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
[Test]
public void Should_extract_to_correct_folder()
{
var destination = Path.Combine(TempFolder, "destination");
var destinationFolder = new DirectoryInfo(GetTempFilePath());
var testArchive = OsInfo.IsWindows ? "TestArchive.zip" : "TestArchive.tar.gz";
Subject.Extract(GetTestFilePath(testArchive), destination);
var destinationFolder = new DirectoryInfo(destination);
Subject.Extract(Path.Combine("Files", testArchive), destinationFolder.FullName);
destinationFolder.Exists.Should().BeTrue();
destinationFolder.GetDirectories().Should().HaveCount(1);

@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests;
namespace NzbDrone.Mono.Test.DiskProviderTests
{
[TestFixture]
[Platform("Mono")]
public class DiskProviderFixture : DiskProviderFixtureBase<DiskProvider>
{
public DiskProviderFixture()

@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests;
namespace NzbDrone.Mono.Test.DiskProviderTests
{
[TestFixture]
[Platform("Mono")]
public class FreeSpaceFixture : FreeSpaceFixtureBase<DiskProvider>
{
public FreeSpaceFixture()

@ -13,15 +13,13 @@ namespace NzbDrone.Mono.Test
[TestFixture]
public class ServiceFactoryFixture : TestBase<ServiceFactory>
{
[SetUp]
public void setup()
{
Mocker.SetConstant(MainAppContainerBuilder.BuildContainer(new StartupContext()));
}
[Test]
public void event_handlers_should_be_unique()
{
MonoOnly();
Mocker.SetConstant(MainAppContainerBuilder.BuildContainer(new StartupContext()));
var handlers = Subject.BuildAll<IHandle<ApplicationShutdownRequested>>()
.Select(c => c.GetType().FullName);

@ -109,9 +109,15 @@ namespace NzbDrone.Test.Common
try
{
if (Directory.Exists(TempFolder))
var tempFolder = new DirectoryInfo(TempFolder);
if (tempFolder.Exists)
{
Directory.Delete(TempFolder, true);
foreach (var file in tempFolder.GetFiles("*", SearchOption.AllDirectories))
{
file.IsReadOnly = false;
}
tempFolder.Delete(true);
}
}
catch (Exception)
@ -119,7 +125,6 @@ namespace NzbDrone.Test.Common
}
}
protected IAppFolderInfo TestFolderInfo { get; private set; }
protected void WindowsOnly()
@ -148,26 +153,11 @@ namespace NzbDrone.Test.Common
TestFolderInfo = Mocker.GetMock<IAppFolderInfo>().Object;
}
protected string GetTestFilePath(string fileName)
{
return Path.Combine(SandboxFolder, fileName);
}
protected string GetTestFilePath()
protected string GetTempFilePath()
{
return GetTestFilePath(Path.GetRandomFileName());
return Path.Combine(TempFolder, Path.GetRandomFileName());
}
protected string SandboxFolder
{
get
{
var folder = Path.Combine(Directory.GetCurrentDirectory(), "Files");
Directory.CreateDirectory(folder);
return folder;
}
}
protected void VerifyEventPublished<TEvent>() where TEvent : class, IEvent
{
VerifyEventPublished<TEvent>(Times.Once());

@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests;
namespace NzbDrone.Windows.Test.DiskProviderTests
{
[TestFixture]
[Platform("Win")]
public class DiskProviderFixture : DiskProviderFixtureBase<DiskProvider>
{
public DiskProviderFixture()

@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests;
namespace NzbDrone.Windows.Test.DiskProviderTests
{
[TestFixture]
[Platform("Win")]
public class FreeSpaceFixture : FreeSpaceFixtureBase<DiskProvider>
{
public FreeSpaceFixture()

Loading…
Cancel
Save