Skip to content

Commit

Permalink
Set test log output via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
markus101 committed Sep 17, 2017
1 parent 8bc55c5 commit ea7d7d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
47 changes: 44 additions & 3 deletions src/NzbDrone.Test.Common/LoggingTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
Expand All @@ -20,16 +22,52 @@ protected static void InitLogging()
if (LogManager.Configuration == null || LogManager.Configuration.AllTargets.None(c => c is ExceptionVerification))
{
LogManager.Configuration = new LoggingConfiguration();
var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" };
LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget));

var logOutput = TestLogOutput.Console;
Enum.TryParse<TestLogOutput>(Environment.GetEnvironmentVariable("SONARR_TESTS_LOG_OUTPUT"), out logOutput);

switch (logOutput)
{
case TestLogOutput.Console:
RegisterConsoleLogger();
break;
case TestLogOutput.File:
RegisterFileLogger();
break;
}

RegisterExceptionVerification();

LogManager.ReconfigExistingLoggers();
}
}

private static void RegisterConsoleLogger()
{
var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" };
LogManager.Configuration.AddTarget(consoleTarget.GetType().Name, consoleTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget));
}

private static void RegisterFileLogger()
{
const string layout = @"${level}|${message}${onexception:inner=${newline}${newline}${exception:format=ToString}${newline}}";

var fileTarget = new FileTarget();

fileTarget.Name = "Test File Logger";
fileTarget.FileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestLog.txt");
fileTarget.AutoFlush = false;
fileTarget.KeepFileOpen = true;
fileTarget.ConcurrentWrites = true;
fileTarget.ConcurrentWriteAttemptDelay = 50;
fileTarget.ConcurrentWriteAttempts = 10;
fileTarget.Layout = layout;

LogManager.Configuration.AddTarget(fileTarget.GetType().Name, fileTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
}

private static void RegisterExceptionVerification()
{
var exceptionVerification = new ExceptionVerification();
Expand All @@ -42,6 +80,7 @@ public void LoggingTestSetup()
{
InitLogging();
ExceptionVerification.Reset();
TestLogger.Info("--- Start: {0} ---", TestContext.CurrentContext.Test.FullName);
}

[TearDown]
Expand All @@ -53,6 +92,8 @@ public void LoggingDownBase()
{
ExceptionVerification.AssertNoUnexpectedLogs();
}

TestLogger.Info("--- End: {0} ---", TestContext.CurrentContext.Test.FullName);
}
}
}
1 change: 1 addition & 0 deletions src/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="StringExtensions.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="TestException.cs" />
<Compile Include="TestLogOutput.cs" />
<Compile Include="TestValidator.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/NzbDrone.Test.Common/TestLogOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NzbDrone.Test.Common
{
public enum TestLogOutput
{
Console = 0,
File = 1,
None = 2
}
}
8 changes: 7 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ WHERE="cat != ManualTest"
TEST_DIR="."
TEST_PATTERN="*Test.dll"
ASSEMBLIES=""
TEST_LOG_FILE="TestLog.txt"

if [ -d "$TEST_DIR/_tests" ]; then
TEST_DIR="$TEST_DIR/_tests"
fi

rm -f "$TEST_LOG_FILE"

# Uncomment to log test output to a file instead of the console
# export SONARR_TESTS_LOG_OUTPUT="File"

NUNIT="$TEST_DIR/NUnit.ConsoleRunner.3.2.0/tools/nunit3-console.exe"
NUNIT_COMMAND="$NUNIT"
NUNIT_PARAMS="--teamcity"
NUNIT_PARAMS="--teamcity --workers=1"

if [ "$PLATFORM" = "Windows" ]; then
WHERE="$WHERE && cat != LINUX"
Expand Down

0 comments on commit ea7d7d0

Please sign in to comment.