-
Notifications
You must be signed in to change notification settings - Fork 105
/
NUnitEngineAdapter.cs
193 lines (167 loc) · 6.69 KB
/
NUnitEngineAdapter.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ***********************************************************************
// Copyright (c) 2020-2021 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
using System.IO;
using NUnit.Engine;
using NUnit.VisualStudio.TestAdapter.Internal;
namespace NUnit.VisualStudio.TestAdapter.NUnitEngine
{
public interface INUnitEngineAdapter
{
NUnitResults Explore();
void CloseRunner();
NUnitResults Explore(TestFilter filter);
NUnitResults Run(ITestEventListener listener, TestFilter filter);
void StopRun();
T GetService<T>()
where T : class;
void GenerateTestOutput(NUnitResults testResults, string assemblyPath, string testOutputXmlFolder);
string GetXmlFilePath(string folder, string defaultFileName, string extension);
}
public class NUnitEngineAdapter : INUnitEngineAdapter, IDisposable
{
private IAdapterSettings settings;
private ITestLogger logger;
private TestPackage package;
private ITestEngine TestEngine { get; set; }
private ITestRunner Runner { get; set; }
internal event Action<ITestEngine> InternalEngineCreated;
public bool EngineEnabled => TestEngine != null;
public void Initialize()
{
#if NET462
var engineX = new TestEngine();
#else
var engineX = TestEngineActivator.CreateInstance();
#endif
if (engineX == null)
engineX = new TestEngine();
InternalEngineCreated?.Invoke(engineX);
TestEngine = engineX;
var tmpPath = Path.Combine(Path.GetTempPath(), "NUnit.Engine");
if (!Directory.Exists(tmpPath))
Directory.CreateDirectory(tmpPath);
TestEngine.WorkDirectory = tmpPath;
TestEngine.InternalTraceLevel = InternalTraceLevel.Off;
}
public void InitializeSettingsAndLogging(IAdapterSettings setting, ITestLogger testLog)
{
logger = testLog;
settings = setting;
#if EnableTraceLevelAtInitialization
#endif
}
public void CreateRunner(TestPackage testPackage)
{
package = testPackage;
Runner = TestEngine.GetRunner(package);
}
public NUnitResults Explore()
{
return Explore(TestFilter.Empty);
}
public NUnitResults Explore(TestFilter filter)
{
var timing = new TimingLogger(settings, logger);
var results = new NUnitResults(Runner.Explore(filter));
return LogTiming(filter, timing, results);
}
public NUnitResults Run(ITestEventListener listener, TestFilter filter)
{
var timing = new TimingLogger(settings, logger);
var results = new NUnitResults(Runner.Run(listener, filter));
return LogTiming(filter, timing, results);
}
private NUnitResults LogTiming(TestFilter filter, TimingLogger timing, NUnitResults results)
{
timing.LogTime($"Execution engine run time with filter length {filter.Text.Length}");
if (filter.Text.Length < 300)
logger.Debug($"Filter: {filter.Text}");
return results;
}
public T GetService<T>()
where T : class
{
var service = TestEngine.Services.GetService<T>();
if (service == null)
{
logger.Warning($"Engine GetService can't create service {typeof(T)}.");
}
return service;
}
public void StopRun()
{
Runner?.StopRun(true);
}
public void CloseRunner()
{
if (Runner == null)
return;
if (Runner.IsTestRunning)
Runner.StopRun(true);
try
{
Runner.Unload();
Runner.Dispose();
}
catch (NUnitEngineUnloadException ex)
{
logger.Warning($"Engine encountered NUnitEngineUnloadException : {ex.Message}");
}
Runner = null;
}
public void Dispose()
{
CloseRunner();
TestEngine?.Dispose();
}
public void GenerateTestOutput(NUnitResults testResults, string assemblyPath, string testOutputXmlFolder)
{
if (!settings.UseTestOutputXml)
return;
string path = GetXmlFilePath(testOutputXmlFolder, Path.GetFileNameWithoutExtension(assemblyPath), "xml");
var resultService = GetService<IResultService>();
// Following null argument should work for nunit3 format. Empty array is OK as well.
// If you decide to handle other formats in the runsettings, it needs more work.
var resultWriter = resultService.GetResultWriter("nunit3", null);
resultWriter.WriteResultFile(testResults.FullTopNode, path);
logger.Info($" Test results written to {path}");
}
public string GetXmlFilePath(string folder, string defaultFileName, string extension)
{
if (!settings.NewOutputXmlFileForEachRun)
{
// overwrite the existing file
return Path.Combine(folder, $"{defaultFileName}.{extension}");
}
// allways create a new file
int i = 1;
while (true)
{
string path = Path.Combine(folder, $"{defaultFileName}.{i++}.{extension}");
if (!File.Exists(path))
return path;
}
}
}
}