This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
9bcd364
commit e9f7a7f
Showing
15 changed files
with
294 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
NuKeeper.Integration.Tests/LogHelpers/NuKeeperTestLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
NuKeeper.Integration.Tests/LogHelpers/NugetTestLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.