Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
When there are errors during the Integration tests, the Nukeeper and …
Browse files Browse the repository at this point in the history
…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 <stuinder@sib.inlichtingenbureau.nl>
  • Loading branch information
StephanTuinder and Stephan Tuinder committed May 5, 2020
1 parent 9bcd364 commit e9f7a7f
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 125 deletions.
14 changes: 6 additions & 8 deletions NuKeeper.Integration.Tests/Engine/RepositoryFilterTests.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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<INuKeeperLogger>();

var collaborationFactory = Substitute.For<ICollaborationFactory>();
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);
}
}
}
83 changes: 83 additions & 0 deletions NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs
Original file line number Diff line number Diff line change
@@ -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<string> _buffer = new ConcurrentQueue<string>();

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}");
}
}
}
}
}
}
}
105 changes: 105 additions & 0 deletions NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs
Original file line number Diff line number Diff line change
@@ -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<string> _buffer = new ConcurrentQueue<string>();

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);
}
}
}
32 changes: 5 additions & 27 deletions NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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<ILogger>(), Substitute.For<INuKeeperLogger>()));
new PackageVersionsLookup(NugetLogger, NukeeperLogger));
}

private static PackageIdentity Current(string packageId)
Expand Down
21 changes: 8 additions & 13 deletions NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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<INuKeeperLogger>();
var lookup = new ApiPackageLookup(new PackageVersionsLookup(
Substitute.For<ILogger>(), 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)
Expand Down
16 changes: 6 additions & 10 deletions NuKeeper.Integration.Tests/NuGet/Api/PackageVersionsLookupTests.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -130,10 +127,9 @@ public async Task CanBeCalledInParallel()
}
}

private static IPackageVersionsLookup BuildPackageLookup()
private IPackageVersionsLookup BuildPackageLookup()
{
return new PackageVersionsLookup(
Substitute.For<ILogger>(), Substitute.For<INuKeeperLogger>());
return new PackageVersionsLookup(NugetLogger, NukeeperLogger);
}
}
}
Loading

0 comments on commit e9f7a7f

Please sign in to comment.