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

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

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

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

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

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

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

@ -109,9 +109,15 @@ namespace NzbDrone.Test.Common
try 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) catch (Exception)
@ -119,7 +125,6 @@ namespace NzbDrone.Test.Common
} }
} }
protected IAppFolderInfo TestFolderInfo { get; private set; } protected IAppFolderInfo TestFolderInfo { get; private set; }
protected void WindowsOnly() protected void WindowsOnly()
@ -148,26 +153,11 @@ namespace NzbDrone.Test.Common
TestFolderInfo = Mocker.GetMock<IAppFolderInfo>().Object; TestFolderInfo = Mocker.GetMock<IAppFolderInfo>().Object;
} }
protected string GetTestFilePath(string fileName) protected string GetTempFilePath()
{
return Path.Combine(SandboxFolder, fileName);
}
protected string GetTestFilePath()
{ {
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 protected void VerifyEventPublished<TEvent>() where TEvent : class, IEvent
{ {
VerifyEventPublished<TEvent>(Times.Once()); VerifyEventPublished<TEvent>(Times.Once());

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

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

Loading…
Cancel
Save