From e9f7a7f5cfd2071d85ab830fd6a121a0346188e4 Mon Sep 17 00:00:00 2001 From: Stephan T <50425448+StephanTuinder@users.noreply.github.com> Date: Tue, 5 May 2020 14:00:41 +0200 Subject: [PATCH] When there are errors during the Integration tests, the Nukeeper and Nuget log is put into the test output (#845) * When there are errors during the test, the log is put into the output * cleanup * Made loggers thread-safe * Name change: "public class BaseTest" -> "public abstract class TestWithFailureLogging" As suggested by Anthony Steele Co-authored-by: Stephan Tuinder --- .../Engine/RepositoryFilterTests.cs | 14 +-- .../LogHelpers/NuKeeperTestLogger.cs | 83 ++++++++++++++ .../LogHelpers/NugetTestLogger.cs | 105 ++++++++++++++++++ .../NuGet/Api/ApiPackageLookupTests.cs | 32 +----- .../NuGet/Api/BulkPackageLookupTests.cs | 21 ++-- .../NuGet/Api/PackageVersionsLookupTests.cs | 16 +-- .../DotNetUpdatePackageCommandTests.cs | 18 ++- .../NuGet/Process/NuGetPathTests.cs | 6 +- .../Process/NuGetUpdatePackageCommandTests.cs | 20 ++-- ...UpdateDirectoryBuildTargetsCommandTests.cs | 16 ++- .../NuGet/Process/UpdateNuspecCommandTests.cs | 16 ++- .../UpdateProjectImportsCommandTests.cs | 7 +- .../ProcessRunner/ExternalProcessTests.cs | 14 +-- .../RepositoryScannerTests.cs | 22 ++-- .../TestWithFailureLogging.cs | 29 +++++ 15 files changed, 294 insertions(+), 125 deletions(-) create mode 100644 NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs create mode 100644 NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs create mode 100644 NuKeeper.Integration.Tests/TestWithFailureLogging.cs diff --git a/NuKeeper.Integration.Tests/Engine/RepositoryFilterTests.cs b/NuKeeper.Integration.Tests/Engine/RepositoryFilterTests.cs index 4afd09469..7314eb6d5 100644 --- a/NuKeeper.Integration.Tests/Engine/RepositoryFilterTests.cs +++ b/NuKeeper.Integration.Tests/Engine/RepositoryFilterTests.cs @@ -1,17 +1,16 @@ -using System; -using System.Threading.Tasks; using NSubstitute; using NuKeeper.Abstractions.CollaborationPlatform; using NuKeeper.Abstractions.Configuration; -using NuKeeper.Abstractions.Logging; using NuKeeper.Engine; using NuKeeper.GitHub; using NUnit.Framework; +using System; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.Engine { [TestFixture] - public class RepositoryFilterTests + public class RepositoryFilterTests : TestWithFailureLogging { [Test] public async Task ShouldFilterOutNonDotnetRepository() @@ -37,17 +36,16 @@ public async Task ShouldNotFilterOutADotnetRepository() Assert.True(result); } - private static RepositoryFilter MakeRepositoryFilter() + private RepositoryFilter MakeRepositoryFilter() { const string testKeyWithOnlyPublicAccess = "c13d2ce7774d39ae99ddaad46bd69c3d459b9992"; - var logger = Substitute.For(); var collaborationFactory = Substitute.For(); - var gitHubClient = new OctokitClient(logger); + var gitHubClient = new OctokitClient(NukeeperLogger); gitHubClient.Initialise(new AuthSettings(new Uri("https://api.github.com"), testKeyWithOnlyPublicAccess)); collaborationFactory.CollaborationPlatform.Returns(gitHubClient); - return new RepositoryFilter(collaborationFactory, logger); + return new RepositoryFilter(collaborationFactory, NukeeperLogger); } } } diff --git a/NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs b/NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs new file mode 100644 index 000000000..3765216e1 --- /dev/null +++ b/NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs @@ -0,0 +1,83 @@ +using NuKeeper.Abstractions.Logging; +using NUnit.Framework; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace NuKeeper.Integration.Tests.LogHelpers +{ + public class NuKeeperTestLogger : INuKeeperLogger + { + private readonly LogLevel _logLevel; + + private readonly ConcurrentQueue _buffer = new ConcurrentQueue(); + + public NuKeeperTestLogger(LogLevel logLevel = LogLevel.Detailed) + { + _logLevel = logLevel; + } + + public void ClearLog() + { + _buffer.Clear(); + } + + public void DumpLogToTestOutput() + { + var test = TestContext.CurrentContext.Test.Name; + + if (_buffer.Count > 0) + { + TestContext.Error.WriteLine($"{test}: NuKeeper Log:"); + while (_buffer.TryDequeue(out var line)) + { + TestContext.Error.WriteLine(line); + } + } + } + + public void Error(string message, Exception ex = null) + { + Log(message, ex: ex); + } + + public void Detailed(string message) + { + Log(message, LogLevel.Detailed); + } + + public void Minimal(string message) + { + Log(message, LogLevel.Minimal); + } + + public void Normal(string message) + { + Log(message, LogLevel.Normal); + } + + private void Log(string message, LogLevel? level = null, Exception ex = null) + { + var test = TestContext.CurrentContext.Test.Name; + + if (_logLevel >= (level ?? LogLevel.Detailed)) + { + var levelString = level?.ToString() ?? "Error"; + + _buffer.Enqueue($"{test}: {levelString} - {message}"); + + if (ex != null) + { + _buffer.Enqueue($"{test}: {ex.GetType().Name} : {ex.Message}"); + foreach (var line in ex.StackTrace.Split(Environment.NewLine.ToCharArray())) + { + if (!string.IsNullOrEmpty(line)) + { + _buffer.Enqueue($"{test}: {line}"); + } + } + } + } + } + } +} diff --git a/NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs b/NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs new file mode 100644 index 000000000..0dd948d27 --- /dev/null +++ b/NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs @@ -0,0 +1,105 @@ +using NuGet.Common; +using NUnit.Framework; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace NuKeeper.Integration.Tests.LogHelpers +{ + class NugetTestLogger : ILogger + { + private readonly LogLevel _logLevel; + + private readonly ConcurrentQueue _buffer = new ConcurrentQueue(); + + public NugetTestLogger(LogLevel logLevel = LogLevel.Verbose) + { + _logLevel = logLevel; + } + + public void ClearLog() + { + _buffer.Clear(); + } + + public void DumpLogToTestOutput() + { + var test = TestContext.CurrentContext.Test.Name; + + if (_buffer.Count > 0) + { + TestContext.Error.WriteLine($"{test}: NuKeeper Log:"); + while (_buffer.TryDequeue(out var line)) + { + TestContext.Error.WriteLine(line); + } + } + } + + public void Log(LogLevel level, string data) + { + var test = TestContext.CurrentContext.Test.Name; + + if (level >= _logLevel ) + { + _buffer.Enqueue($"{test}: {level} - {data}"); + } + } + + public void Log(ILogMessage message) + { + Log(message.Level, message.Message); + } + + public Task LogAsync(LogLevel level, string data) + { + return Task.Run(() => + { + Log(level, data); + }); + } + + public Task LogAsync(ILogMessage message) + { + return Task.Run(() => + { + Log(message); + }); + } + + public void LogDebug(string data) + { + Log(LogLevel.Debug, data); + } + + public void LogError(string data) + { + Log(LogLevel.Error, data); + } + + public void LogInformation(string data) + { + Log(LogLevel.Information, data); + } + + public void LogInformationSummary(string data) + { + Log(LogLevel.Information, data); + } + + public void LogMinimal(string data) + { + Log(LogLevel.Minimal, data); + } + + public void LogVerbose(string data) + { + Log(LogLevel.Verbose, data); + } + + public void LogWarning(string data) + { + Log(LogLevel.Warning, data); + } + } +} diff --git a/NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs b/NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs index 2a2b7a1ff..413ffd0e8 100644 --- a/NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs @@ -1,19 +1,16 @@ -using System; -using System.Threading.Tasks; -using NSubstitute; -using NuGet.Common; using NuGet.Packaging.Core; using NuGet.Versioning; using NuKeeper.Abstractions.Configuration; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Inspection.NuGetApi; using NUnit.Framework; +using System; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Api { [TestFixture] - public class ApiPackageLookupTests + public class ApiPackageLookupTests : TestWithFailureLogging { [Test] public async Task AmbiguousPackageName_ShouldReturnCorrectResult() @@ -105,29 +102,10 @@ public async Task MinorUpdateToWellKnownPackage() Assert.That(package.Major.Identity.Version.Major, Is.GreaterThan(8)); } - // [Test] - // public async Task BetaVersion_ShouldReturnBetas() - // { - // var lookup = BuildPackageLookup(); - // - // // libgit2sharp is known for staying in preview for ages - // var package = await lookup.FindVersionUpdate( - // new PackageIdentity("libgit2sharp", new NuGetVersion(0, 26, 0, "preview-0017")), - // NuGetSources.GlobalFeed, - // VersionChange.Minor, - // UsePrerelease.FromPrerelease); - // - // Assert.That(package, Is.Not.Null); - // Assert.That(package.Selected(), Is.Not.Null); - // - // var isBeta = package.Patch.Identity.Version.IsPrerelease; - // Assert.That(isBeta, Is.True); - // } - - private static IApiPackageLookup BuildPackageLookup() + private IApiPackageLookup BuildPackageLookup() { return new ApiPackageLookup( - new PackageVersionsLookup(Substitute.For(), Substitute.For())); + new PackageVersionsLookup(NugetLogger, NukeeperLogger)); } private static PackageIdentity Current(string packageId) diff --git a/NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs b/NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs index a4ac11100..71b7182d7 100644 --- a/NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs @@ -1,21 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using NSubstitute; -using NuGet.Common; using NuGet.Packaging.Core; using NuGet.Versioning; using NuKeeper.Abstractions.Configuration; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Inspection.NuGetApi; using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Api { [TestFixture] - public class BulkPackageLookupTests + public class BulkPackageLookupTests : TestWithFailureLogging { [Test] public async Task CanFindUpdateForOneWellKnownPackage() @@ -129,12 +126,10 @@ public async Task ValidPackagesWorkDespiteInvalidPackages() Assert.That(results, Does.ContainKey("Newtonsoft.Json")); } - private static BulkPackageLookup BuildBulkPackageLookup() + private BulkPackageLookup BuildBulkPackageLookup() { - var nuKeeperLogger = Substitute.For(); - var lookup = new ApiPackageLookup(new PackageVersionsLookup( - Substitute.For(), nuKeeperLogger)); - return new BulkPackageLookup(lookup, new PackageLookupResultReporter(nuKeeperLogger)); + var lookup = new ApiPackageLookup(new PackageVersionsLookup(NugetLogger, NukeeperLogger)); + return new BulkPackageLookup(lookup, new PackageLookupResultReporter(NukeeperLogger)); } private static PackageIdentity Current(string packageId) diff --git a/NuKeeper.Integration.Tests/NuGet/Api/PackageVersionsLookupTests.cs b/NuKeeper.Integration.Tests/NuGet/Api/PackageVersionsLookupTests.cs index 35ec5bca1..3b808eda5 100644 --- a/NuKeeper.Integration.Tests/NuGet/Api/PackageVersionsLookupTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Api/PackageVersionsLookupTests.cs @@ -1,18 +1,15 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using NSubstitute; -using NuGet.Common; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Abstractions.NuGetApi; using NuKeeper.Inspection.NuGetApi; using NUnit.Framework; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.Nuget.Api { [TestFixture] - public class PackageVersionsLookupTests + public class PackageVersionsLookupTests : TestWithFailureLogging { [Test] public async Task WellKnownPackageName_ShouldReturnResultsList() @@ -130,10 +127,9 @@ public async Task CanBeCalledInParallel() } } - private static IPackageVersionsLookup BuildPackageLookup() + private IPackageVersionsLookup BuildPackageLookup() { - return new PackageVersionsLookup( - Substitute.For(), Substitute.For()); + return new PackageVersionsLookup(NugetLogger, NukeeperLogger); } } } diff --git a/NuKeeper.Integration.Tests/NuGet/Process/DotNetUpdatePackageCommandTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/DotNetUpdatePackageCommandTests.cs index 577e2e295..b912e70bf 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/DotNetUpdatePackageCommandTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/DotNetUpdatePackageCommandTests.cs @@ -1,23 +1,21 @@ -using System; -using System.IO; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using NSubstitute; using NuGet.Configuration; using NuGet.Versioning; using NuKeeper.Abstractions.Inspections.Files; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Abstractions.RepositoryInspection; using NuKeeper.Inspection.Files; using NuKeeper.Update.Process; using NuKeeper.Update.ProcessRunner; using NUnit.Framework; +using System; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Process { [TestFixture] - public class DotNetUpdatePackageCommandTests + public class DotNetUpdatePackageCommandTests : TestWithFailureLogging { private readonly string _testWebApiProject = @" @@ -131,7 +129,7 @@ private async Task ExecuteValidUpdateTest( var projectPath = Path.Combine(workDirectory, testProject); await File.WriteAllTextAsync(projectPath, projectContents); - var command = new DotNetUpdatePackageCommand(new ExternalProcess(Substitute.For())); + var command = new DotNetUpdatePackageCommand(new ExternalProcess(NukeeperLogger)); var packageToUpdate = new PackageInProject("Microsoft.AspNet.WebApi.Client", oldPackageVersion, new PackagePath(workDirectory, testProject, packageReferenceType)); @@ -145,9 +143,9 @@ private async Task ExecuteValidUpdateTest( Does.Not.Contain(expectedPackageString.Replace("{packageVersion}", oldPackageVersion, StringComparison.OrdinalIgnoreCase))); } - private static IFolder UniqueTemporaryFolder() + private IFolder UniqueTemporaryFolder() { - var factory = new FolderFactory(Substitute.For()); + var factory = new FolderFactory(NukeeperLogger); return factory.UniqueTemporaryFolder(); } } diff --git a/NuKeeper.Integration.Tests/NuGet/Process/NuGetPathTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/NuGetPathTests.cs index bf2c0daae..4527d856a 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/NuGetPathTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/NuGetPathTests.cs @@ -1,17 +1,15 @@ -using NSubstitute; -using NuKeeper.Abstractions.Logging; using NuKeeper.Update.Process; using NUnit.Framework; namespace NuKeeper.Integration.Tests.NuGet.Process { [TestFixture] - public class NuGetPathTests + public class NuGetPathTests : TestWithFailureLogging { [Test] public void HasNugetPath() { - var nugetPath = new NuGetPath(Substitute.For()).Executable; + var nugetPath = new NuGetPath(NukeeperLogger).Executable; Assert.That(nugetPath, Is.Not.Empty); FileAssert.Exists(nugetPath); diff --git a/NuKeeper.Integration.Tests/NuGet/Process/NuGetUpdatePackageCommandTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/NuGetUpdatePackageCommandTests.cs index 9bf6d79e0..542fb20f8 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/NuGetUpdatePackageCommandTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/NuGetUpdatePackageCommandTests.cs @@ -1,22 +1,20 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using NSubstitute; using NuGet.Configuration; using NuGet.Versioning; using NuKeeper.Abstractions.Inspections.Files; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; -using NUnit.Framework; +using NuKeeper.Abstractions.RepositoryInspection; using NuKeeper.Inspection.Files; using NuKeeper.Update.Process; using NuKeeper.Update.ProcessRunner; -using NuKeeper.Abstractions.RepositoryInspection; +using NUnit.Framework; +using System; +using System.IO; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Process { [TestFixture] - public class NuGetUpdatePackageCommandTests + public class NuGetUpdatePackageCommandTests : TestWithFailureLogging { private readonly string _testDotNetClassicProject = @" @@ -75,7 +73,7 @@ public async Task ShouldUpdateDotnetClassicProject() await File.WriteAllTextAsync(Path.Combine(workDirectory, "nuget.config"), _nugetConfig); - var logger = Substitute.For(); + var logger = NukeeperLogger; var externalProcess = new ExternalProcess(logger); var monoExecutor = new MonoExecutor(logger, externalProcess); @@ -103,9 +101,9 @@ public async Task ShouldUpdateDotnetClassicProject() StringComparison.OrdinalIgnoreCase))); } - private static IFolder UniqueTemporaryFolder() + private IFolder UniqueTemporaryFolder() { - var factory = new FolderFactory(Substitute.For()); + var factory = new FolderFactory(NukeeperLogger); return factory.UniqueTemporaryFolder(); } } diff --git a/NuKeeper.Integration.Tests/NuGet/Process/UpdateDirectoryBuildTargetsCommandTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/UpdateDirectoryBuildTargetsCommandTests.cs index 5654e9dfe..744c683aa 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/UpdateDirectoryBuildTargetsCommandTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/UpdateDirectoryBuildTargetsCommandTests.cs @@ -1,19 +1,17 @@ -using System; -using System.IO; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using NSubstitute; using NuGet.Versioning; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Abstractions.RepositoryInspection; using NuKeeper.Update.Process; using NUnit.Framework; +using System; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Process { [TestFixture] - public class UpdateDirectoryBuildTargetsCommandTests + public class UpdateDirectoryBuildTargetsCommandTests : TestWithFailureLogging { private readonly string _testFileWithUpdate = @""; @@ -33,7 +31,7 @@ public async Task ShouldUpdateValidFileWithIncludeAttribute() await ExecuteValidUpdateTest(_testFileWithInclude, ""); } - private static async Task ExecuteValidUpdateTest(string testProjectContents, string expectedPackageString, [CallerMemberName] string memberName = "") + private async Task ExecuteValidUpdateTest(string testProjectContents, string expectedPackageString, [CallerMemberName] string memberName = "") { const string oldPackageVersion = "5.2.31"; const string newPackageVersion = "5.3.4"; @@ -46,7 +44,7 @@ private static async Task ExecuteValidUpdateTest(string testProjectContents, str var projectPath = Path.Combine(workDirectory, testFile); await File.WriteAllTextAsync(projectPath, projectContents); - var command = new UpdateDirectoryBuildTargetsCommand(Substitute.For()); + var command = new UpdateDirectoryBuildTargetsCommand(NukeeperLogger); var package = new PackageInProject("foo", oldPackageVersion, new PackagePath(workDirectory, testFile, PackageReferenceType.DirectoryBuildTargets)); diff --git a/NuKeeper.Integration.Tests/NuGet/Process/UpdateNuspecCommandTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/UpdateNuspecCommandTests.cs index 3ee625dfd..c1dd27867 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/UpdateNuspecCommandTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/UpdateNuspecCommandTests.cs @@ -1,19 +1,17 @@ -using System; -using System.IO; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using NSubstitute; using NuGet.Versioning; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.NuGet; using NuKeeper.Abstractions.RepositoryInspection; using NuKeeper.Update.Process; using NUnit.Framework; +using System; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Process { [TestFixture] - public class UpdateNuspecCommandTests + public class UpdateNuspecCommandTests : TestWithFailureLogging { private readonly string _testNuspec = @" @@ -27,7 +25,7 @@ public async Task ShouldUpdateValidNuspecFile() await ExecuteValidUpdateTest(_testNuspec); } - private static async Task ExecuteValidUpdateTest(string testProjectContents, [CallerMemberName] string memberName = "") + private async Task ExecuteValidUpdateTest(string testProjectContents, [CallerMemberName] string memberName = "") { const string oldPackageVersion = "5.2.31"; const string newPackageVersion = "5.3.4"; @@ -42,7 +40,7 @@ private static async Task ExecuteValidUpdateTest(string testProjectContents, [Ca var projectPath = Path.Combine(workDirectory, testNuspec); await File.WriteAllTextAsync(projectPath, projectContents); - var command = new UpdateNuspecCommand(Substitute.For()); + var command = new UpdateNuspecCommand(NukeeperLogger); var package = new PackageInProject("foo", oldPackageVersion, new PackagePath(workDirectory, testNuspec, PackageReferenceType.Nuspec)); diff --git a/NuKeeper.Integration.Tests/NuGet/Process/UpdateProjectImportsCommandTests.cs b/NuKeeper.Integration.Tests/NuGet/Process/UpdateProjectImportsCommandTests.cs index 3d917420a..d78014e80 100644 --- a/NuKeeper.Integration.Tests/NuGet/Process/UpdateProjectImportsCommandTests.cs +++ b/NuKeeper.Integration.Tests/NuGet/Process/UpdateProjectImportsCommandTests.cs @@ -1,11 +1,10 @@ -using System; -using System.IO; -using System.Threading.Tasks; using NuKeeper.Abstractions.NuGet; using NuKeeper.Abstractions.RepositoryInspection; -using NuKeeper.Inspection.RepositoryInspection; using NuKeeper.Update.Process; using NUnit.Framework; +using System; +using System.IO; +using System.Threading.Tasks; namespace NuKeeper.Integration.Tests.NuGet.Process { diff --git a/NuKeeper.Integration.Tests/ProcessRunner/ExternalProcessTests.cs b/NuKeeper.Integration.Tests/ProcessRunner/ExternalProcessTests.cs index e55f2d94c..da5f4a121 100644 --- a/NuKeeper.Integration.Tests/ProcessRunner/ExternalProcessTests.cs +++ b/NuKeeper.Integration.Tests/ProcessRunner/ExternalProcessTests.cs @@ -1,15 +1,13 @@ +using NuKeeper.Update.ProcessRunner; +using NUnit.Framework; using System; using System.Globalization; using System.Threading.Tasks; -using NSubstitute; -using NuKeeper.Abstractions.Logging; -using NuKeeper.Update.ProcessRunner; -using NUnit.Framework; namespace NuKeeper.Integration.Tests.ProcessRunner { [TestFixture] - public class ExternalProcessTests + public class ExternalProcessTests : TestWithFailureLogging { [Test] public async Task ValidCommandShouldSucceed() @@ -44,14 +42,14 @@ public void InvalidCommandShouldThrowWhenSuccessIsEnsured() () => RunExternalProcess(Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), true)); } - private static async Task RunExternalProcess(string command, bool ensureSuccess) + private async Task RunExternalProcess(string command, bool ensureSuccess) { return await RunExternalProcess(command, "", ensureSuccess); } - private static async Task RunExternalProcess(string command, string args, bool ensureSuccess) + private async Task RunExternalProcess(string command, string args, bool ensureSuccess) { - IExternalProcess process = new ExternalProcess(Substitute.For()); + IExternalProcess process = new ExternalProcess(NukeeperLogger); return await process.Run(".", command, args, ensureSuccess); } diff --git a/NuKeeper.Integration.Tests/RepositoryInspection/RepositoryScannerTests.cs b/NuKeeper.Integration.Tests/RepositoryInspection/RepositoryScannerTests.cs index 5c7add76b..c2f933acc 100644 --- a/NuKeeper.Integration.Tests/RepositoryInspection/RepositoryScannerTests.cs +++ b/NuKeeper.Integration.Tests/RepositoryInspection/RepositoryScannerTests.cs @@ -1,20 +1,18 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using NSubstitute; using NuGet.Versioning; using NuKeeper.Abstractions.Inspections.Files; -using NuKeeper.Abstractions.Logging; using NuKeeper.Abstractions.RepositoryInspection; using NuKeeper.Inspection.Files; using NuKeeper.Inspection.RepositoryInspection; using NUnit.Framework; +using System; +using System.IO; +using System.Linq; +using System.Reflection; namespace NuKeeper.Integration.Tests.RepositoryInspection { [TestFixture] - public class RepositoryScannerTests + public class RepositoryScannerTests : TestWithFailureLogging { const string SinglePackageInFile = @" @@ -230,7 +228,7 @@ public void CorrectItemsInPackagesProps() public void SelfTest() { var scanner = MakeScanner(); - var baseFolder = new Folder(Substitute.For(), GetOwnRootDir()); + var baseFolder = new Folder(NukeeperLogger, GetOwnRootDir()); var results = scanner.FindAllNuGetPackages(baseFolder); @@ -253,15 +251,15 @@ private static DirectoryInfo GetOwnRootDir() return projectRootDir; } - private static IFolder UniqueTemporaryFolder() + private IFolder UniqueTemporaryFolder() { - var folderFactory = new FolderFactory(Substitute.For()); + var folderFactory = new FolderFactory(NukeeperLogger); return folderFactory.UniqueTemporaryFolder(); } - private static IRepositoryScanner MakeScanner() + private IRepositoryScanner MakeScanner() { - var logger = Substitute.For(); + var logger = NukeeperLogger; return new RepositoryScanner( new ProjectFileReader(logger), new PackagesFileReader(logger), diff --git a/NuKeeper.Integration.Tests/TestWithFailureLogging.cs b/NuKeeper.Integration.Tests/TestWithFailureLogging.cs new file mode 100644 index 000000000..d0d3c2785 --- /dev/null +++ b/NuKeeper.Integration.Tests/TestWithFailureLogging.cs @@ -0,0 +1,29 @@ +using NuGet.Common; +using NuKeeper.Abstractions.Logging; +using NuKeeper.Integration.Tests.LogHelpers; +using NUnit.Framework; +using NUnit.Framework.Interfaces; + +namespace NuKeeper.Integration.Tests +{ + public abstract class TestWithFailureLogging + { + private NuKeeperTestLogger _nkLogger = new NuKeeperTestLogger(); + private NugetTestLogger _ngLogger = new NugetTestLogger(); + + public INuKeeperLogger NukeeperLogger => _nkLogger; + public ILogger NugetLogger => _ngLogger; + + [TearDown] + public void DumpLogWithError() + { + if (TestContext.CurrentContext.Result.Outcome != ResultState.Success) + { + _nkLogger.DumpLogToTestOutput(); + _ngLogger.DumpLogToTestOutput(); + } + _nkLogger.ClearLog(); + _ngLogger.ClearLog(); + } + } +}