From 79b2b146685aca69ecff8f1d7095b2f3fbe55c53 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 4 May 2014 16:33:25 +0200 Subject: [PATCH] Updated tests to work better with VS2013 Test Adapter. --- .../DiskProviderFixtureBase.cs | 158 +++++++++--------- .../DiskProviderTests/FreeSpaceFixtureBase.cs | 13 +- .../PathExtensionFixture.cs | 2 +- src/NzbDrone.Core.Test/FluentTest.cs | 6 +- .../ArchiveProviderFixture.cs | 6 +- .../DiskProviderTests/DiskProviderFixture.cs | 1 + .../DiskProviderTests/FreeSpaceFixture.cs | 1 + .../ServiceFactoryFixture.cs | 10 +- src/NzbDrone.Test.Common/TestBase.cs | 30 ++-- .../DiskProviderTests/DiskProviderFixture.cs | 1 + .../DiskProviderTests/FreeSpaceFixture.cs | 1 + 11 files changed, 119 insertions(+), 110 deletions(-) diff --git a/src/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixtureBase.cs b/src/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixtureBase.cs index e4a953fce..8c62331de 100644 --- a/src/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixtureBase.cs @@ -10,30 +10,26 @@ namespace NzbDrone.Common.Test.DiskProviderTests { public class DiskProviderFixtureBase : TestBase 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) - { - _binFolderMove.Delete(true); - } + public DirectoryInfo GetFilledTempFolder() + { + 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); + VerifyCopy(source.FullName, destination.FullName); + } - Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.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(_binFolderCopy.FullName, _binFolderMove.FullName); - + Subject.MoveFolder(source.FullName, destination.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); } } } diff --git a/src/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixtureBase.cs b/src/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixtureBase.cs index f63942a11..098456276 100644 --- a/src/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskProviderTests/FreeSpaceFixtureBase.cs @@ -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(() => 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(() => Subject.GetAvailableSpace(driveletter + @":\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic())); + return; + } + + Assert.Inconclusive("No drive available for testing."); } [Test] diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 8c75708e2..4dd274a33 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -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); } diff --git a/src/NzbDrone.Core.Test/FluentTest.cs b/src/NzbDrone.Core.Test/FluentTest.cs index 7af26ed45..66addf0c7 100644 --- a/src/NzbDrone.Core.Test/FluentTest.cs +++ b/src/NzbDrone.Core.Test/FluentTest.cs @@ -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(() => "test".WithDefault(null)); + + ExceptionVerification.IgnoreWarns(); } [Test] diff --git a/src/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ArchiveProviderFixture.cs b/src/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ArchiveProviderFixture.cs index a9f3e2f0d..6f4cacea5 100644 --- a/src/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ArchiveProviderFixture.cs +++ b/src/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ArchiveProviderFixture.cs @@ -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); diff --git a/src/NzbDrone.Mono.Test/DiskProviderTests/DiskProviderFixture.cs b/src/NzbDrone.Mono.Test/DiskProviderTests/DiskProviderFixture.cs index dffd16153..542cf6d58 100644 --- a/src/NzbDrone.Mono.Test/DiskProviderTests/DiskProviderFixture.cs +++ b/src/NzbDrone.Mono.Test/DiskProviderTests/DiskProviderFixture.cs @@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests; namespace NzbDrone.Mono.Test.DiskProviderTests { [TestFixture] + [Platform("Mono")] public class DiskProviderFixture : DiskProviderFixtureBase { public DiskProviderFixture() diff --git a/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs b/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs index 109fb4825..768fb73fa 100644 --- a/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/src/NzbDrone.Mono.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests; namespace NzbDrone.Mono.Test.DiskProviderTests { [TestFixture] + [Platform("Mono")] public class FreeSpaceFixture : FreeSpaceFixtureBase { public FreeSpaceFixture() diff --git a/src/NzbDrone.Mono.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Mono.Test/ServiceFactoryFixture.cs index 1d5e77f87..811edfc66 100644 --- a/src/NzbDrone.Mono.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Mono.Test/ServiceFactoryFixture.cs @@ -13,15 +13,13 @@ namespace NzbDrone.Mono.Test [TestFixture] public class ServiceFactoryFixture : TestBase { - [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>() .Select(c => c.GetType().FullName); diff --git a/src/NzbDrone.Test.Common/TestBase.cs b/src/NzbDrone.Test.Common/TestBase.cs index 403b52fec..b04231d57 100644 --- a/src/NzbDrone.Test.Common/TestBase.cs +++ b/src/NzbDrone.Test.Common/TestBase.cs @@ -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().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() where TEvent : class, IEvent { VerifyEventPublished(Times.Once()); diff --git a/src/NzbDrone.Windows.Test/DiskProviderTests/DiskProviderFixture.cs b/src/NzbDrone.Windows.Test/DiskProviderTests/DiskProviderFixture.cs index 68b2d1f0c..f81c379f1 100644 --- a/src/NzbDrone.Windows.Test/DiskProviderTests/DiskProviderFixture.cs +++ b/src/NzbDrone.Windows.Test/DiskProviderTests/DiskProviderFixture.cs @@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests; namespace NzbDrone.Windows.Test.DiskProviderTests { [TestFixture] + [Platform("Win")] public class DiskProviderFixture : DiskProviderFixtureBase { public DiskProviderFixture() diff --git a/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs b/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs index a642b49a9..0334a6d3c 100644 --- a/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs +++ b/src/NzbDrone.Windows.Test/DiskProviderTests/FreeSpaceFixture.cs @@ -4,6 +4,7 @@ using NzbDrone.Common.Test.DiskProviderTests; namespace NzbDrone.Windows.Test.DiskProviderTests { [TestFixture] + [Platform("Win")] public class FreeSpaceFixture : FreeSpaceFixtureBase { public FreeSpaceFixture()