-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cs
More file actions
93 lines (73 loc) · 3.25 KB
/
Common.cs
File metadata and controls
93 lines (73 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ******************************************************************************
// © 2016 Sebastiaan Dammann - damsteen.nl
//
// File: : Common.cs
// Project : NUnitTestOrdering.Tests
// ******************************************************************************
namespace NUnitTestOrdering.Tests.TestData {
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Pipes;
using System.Runtime.CompilerServices;
using System.Text;
using NUnit.Framework;
/// <summary>
/// Helper class for logging. Note we explicitly _dont_ use one of the initializer attributes here,
/// as we don't want to influence how NUnit runs the tests
/// </summary>
internal sealed class Common {
private static Common LazyInstance;
public static Common Instance => LazyInstance ?? (LazyInstance = new Common());
public const string PipeName = "_TEST_PIPENAME";
private readonly NamedPipeClientStream _namedPipe;
private readonly TextWriter _logWriter;
public Common() {
string pipeName = TestContext.Parameters.Get("NAMEDPIPE") ?? Environment.GetEnvironmentVariable(PipeName, EnvironmentVariableTarget.Process);
Console.WriteLine("Using pipe name: {0}", pipeName);
// Pipename will be null when directly running tests from test assmebly runner
if (pipeName == null) {
this._logWriter = new StringWriter();
TestContext.Out.WriteLine("Pipe name was not provided! Tests may fail!");
}
else {
this._namedPipe = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);
this._namedPipe.Connect();
this._logWriter = new StreamWriter(this._namedPipe, Encoding.UTF8);
}
AppDomain.CurrentDomain.DomainUnload += (o, e) => {
this._logWriter.Flush();
this._namedPipe?.WaitForPipeDrain();
this._logWriter.Dispose();
this._namedPipe?.Dispose();
};
}
public void Log(string s) {
TestContext.Out.WriteLine(s);
this._logWriter.WriteLine(s);
this._logWriter.Flush();
this._namedPipe?.WaitForPipeDrain();
}
}
internal sealed class TestLogger {
private readonly object _testInstance;
public TestLogger(object testInstance) {
this._testInstance = testInstance;
}
public void Log(string extraData = null, [CallerMemberName] string method = null) {
string extraDataString = string.Empty;
if (!String.IsNullOrEmpty(extraData)) extraDataString = $" [{extraData}]";
Common.Instance.Log($"{this._testInstance.GetType().Name}.{method}{extraDataString}");
}
}
public abstract class LoggingTestBase {
private readonly TestLogger _logger;
protected LoggingTestBase() {
this._logger = new TestLogger(this);
}
[SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
protected void Log(string extraData = null, [CallerMemberName] string method = null) {
this._logger.Log(extraData, method);
}
}
}