-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathFormatterLoggerBase.cs
More file actions
112 lines (99 loc) · 4.14 KB
/
FormatterLoggerBase.cs
File metadata and controls
112 lines (99 loc) · 4.14 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
namespace Reqnroll.FormatterTestLogger;
public abstract class FormatterLoggerBase : ITestLoggerWithParameters
{
public bool IsInitialized { get; private set; }
public Dictionary<string, string> Parameters { get; private set; } = new();
public string TestRunDirectory { get; private set; } = string.Empty;
private string _originalEnvValue;
private bool _envSetByLogger;
private bool _subscribed;
protected string FormatterName = null;
// Meta-keys to ignore for formatter environment variable
// ReSharper disable once UnusedMember.Global
protected static readonly HashSet<string> MetaKeys = new(StringComparer.OrdinalIgnoreCase)
{
"TestRunDirectory", "TargetFramework"
};
private void SetFormattersEnvironmentVariable(Dictionary<string, string> parameters, string testRunDirectory)
{
string effectiveTestRunDirectory = null;
if (parameters != null && parameters.TryGetValue("testRunDirectory", out var paramTestRunDir) && !string.IsNullOrEmpty(paramTestRunDir))
{
effectiveTestRunDirectory = paramTestRunDir;
}
else if (!string.IsNullOrEmpty(testRunDirectory))
{
effectiveTestRunDirectory = testRunDirectory;
}
if (parameters != null)
{
var outputFilePath = parameters.TryGetValue("outputFilePath", out string outputFilePathParameter) ? outputFilePathParameter : null;
var alternateFilePath = parameters.TryGetValue("LogFileName", out string alternateFilePathParameter) ? alternateFilePathParameter : null;
outputFilePath ??= alternateFilePath;
if (string.IsNullOrEmpty(FormatterName))
{
return; // No formatter name provided, nothing to do
}
if (!string.IsNullOrEmpty(effectiveTestRunDirectory) && !string.IsNullOrEmpty(outputFilePath))
{
outputFilePath = Path.Combine(effectiveTestRunDirectory, outputFilePath);
}
else if (!string.IsNullOrEmpty(effectiveTestRunDirectory) && string.IsNullOrEmpty(outputFilePath))
{
outputFilePath = effectiveTestRunDirectory;
}
// Use proper JSON serialization to handle escaping correctly
var configObject = new
{
formatters = new Dictionary<string, object>
{
[FormatterName] = new
{
outputFilePath
}
}
};
var json = JsonSerializer.Serialize(configObject);
if (_originalEnvValue == null)
{
_originalEnvValue = Environment.GetEnvironmentVariable($"REQNROLL_FORMATTERS_LOGGER_{FormatterName}");
}
Environment.SetEnvironmentVariable($"REQNROLL_FORMATTERS_LOGGER_{FormatterName}", json);
_envSetByLogger = true;
}
}
private void SubscribeToTestRunComplete(TestLoggerEvents events)
{
if (!_subscribed && events != null)
{
events.TestRunComplete += (_, _) =>
{
if (_envSetByLogger)
{
Environment.SetEnvironmentVariable($"REQNROLL_FORMATTERS_LOGGER_{FormatterName}", _originalEnvValue);
_envSetByLogger = false;
}
};
_subscribed = true;
}
}
public void Initialize(TestLoggerEvents events, Dictionary<string, string> parameters)
{
SubscribeToTestRunComplete(events);
IsInitialized = true;
Parameters = parameters;
SetFormattersEnvironmentVariable(parameters, TestRunDirectory);
}
public void Initialize(TestLoggerEvents events, string testRunDirectory)
{
SubscribeToTestRunComplete(events);
TestRunDirectory = testRunDirectory;
IsInitialized = true;
SetFormattersEnvironmentVariable(Parameters, testRunDirectory);
}
}