-
Notifications
You must be signed in to change notification settings - Fork 758
/
ConfigReader_Json.cs
261 lines (236 loc) · 12.6 KB
/
ConfigReader_Json.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
#if NETSTANDARD1_1
using System.Reflection;
#endif
namespace Xunit
{
/// <summary>
/// This class is used to read configuration information for a test assembly.
/// </summary>
public static class ConfigReader_Json
{
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static TestAssemblyConfiguration Load(Stream configStream) =>
Load(configStream, null);
/// <summary>
/// Loads the test assembly configuration for the given test assembly from a JSON stream. Caller is responsible for opening the stream.
/// </summary>
/// <param name="configStream">Stream containing config for an assembly</param>
/// <param name="warnings">A container to receive loading warnings, if desired.</param>
/// <returns>The test assembly configuration.</returns>
public static TestAssemblyConfiguration Load(Stream configStream, List<string> warnings = null) =>
LoadConfiguration(configStream, null, warnings);
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static TestAssemblyConfiguration Load(string assemblyFileName, string configFileName = null) =>
Load(assemblyFileName, configFileName, null);
/// <summary>
/// Loads the test assembly configuration for the given test assembly.
/// </summary>
/// <param name="assemblyFileName">The test assembly.</param>
/// <param name="configFileName">The test assembly configuration file.</param>
/// <param name="warnings">A container to receive loading warnings, if desired.</param>
/// <returns>The test assembly configuration.</returns>
public static TestAssemblyConfiguration Load(string assemblyFileName, string configFileName = null, List<string> warnings = null)
{
// If they provide a configuration file, we only read that, success or failure
if (configFileName != null)
{
if (!configFileName.EndsWith(".json", StringComparison.Ordinal))
return null;
#if !NETSTANDARD1_1
if (!File.Exists(configFileName))
{
warnings?.Add(string.Format(CultureInfo.CurrentCulture, "Couldn't load config file '{0}': file not found", configFileName));
return null;
}
#endif
return LoadFile(configFileName, warnings);
}
var assemblyName = Path.GetFileNameWithoutExtension(assemblyFileName);
var directoryName = Path.GetDirectoryName(assemblyFileName);
return LoadFile(Path.Combine(directoryName, string.Format(CultureInfo.InvariantCulture, "{0}.xunit.runner.json", assemblyName)), warnings)
?? LoadFile(Path.Combine(directoryName, "xunit.runner.json"), warnings);
}
static TestAssemblyConfiguration LoadConfiguration(Stream configStream, string configFileName, List<string> warnings)
{
Guard.ArgumentNotNull(nameof(configStream), configStream);
string ConfigDescription() =>
configFileName == null
? "configuration"
: string.Format(CultureInfo.CurrentCulture, "config file '{0}'", configFileName);
var result = new TestAssemblyConfiguration();
try
{
using (var reader = new StreamReader(configStream))
{
var config = JsonDeserializer.Deserialize(reader) as JsonObject;
if (config == null)
{
warnings?.Add(string.Format(CultureInfo.CurrentCulture, "Couldn't parse {0}: the root must be a JSON object", ConfigDescription()));
return null;
}
foreach (var propertyName in config.Keys)
{
var propertyValue = config.Value(propertyName);
var booleanValue = propertyValue as JsonBoolean;
if (booleanValue != null)
{
if (string.Equals(propertyName, Configuration.DiagnosticMessages, StringComparison.OrdinalIgnoreCase))
result.DiagnosticMessages = booleanValue;
if (string.Equals(propertyName, Configuration.FailSkips, StringComparison.OrdinalIgnoreCase))
result.FailSkips = booleanValue;
if (string.Equals(propertyName, Configuration.InternalDiagnosticMessages, StringComparison.OrdinalIgnoreCase))
result.InternalDiagnosticMessages = booleanValue;
if (string.Equals(propertyName, Configuration.ParallelizeAssembly, StringComparison.OrdinalIgnoreCase))
result.ParallelizeAssembly = booleanValue;
if (string.Equals(propertyName, Configuration.ParallelizeTestCollections, StringComparison.OrdinalIgnoreCase))
result.ParallelizeTestCollections = booleanValue;
if (string.Equals(propertyName, Configuration.PreEnumerateTheories, StringComparison.OrdinalIgnoreCase))
result.PreEnumerateTheories = booleanValue;
if (string.Equals(propertyName, Configuration.ShadowCopy, StringComparison.OrdinalIgnoreCase))
result.ShadowCopy = booleanValue;
if (string.Equals(propertyName, Configuration.StopOnFail, StringComparison.OrdinalIgnoreCase))
result.StopOnFail = booleanValue;
}
else if (string.Equals(propertyName, Configuration.MaxParallelThreads, StringComparison.OrdinalIgnoreCase))
{
var numberValue = propertyValue as JsonNumber;
if (numberValue != null)
{
int maxParallelThreads;
if (int.TryParse(numberValue.Raw, out maxParallelThreads) && maxParallelThreads >= -1)
result.MaxParallelThreads = maxParallelThreads;
}
}
else if (string.Equals(propertyName, Configuration.LongRunningTestSeconds, StringComparison.OrdinalIgnoreCase))
{
var numberValue = propertyValue as JsonNumber;
if (numberValue != null)
{
int seconds;
if (int.TryParse(numberValue.Raw, out seconds) && seconds > 0)
result.LongRunningTestSeconds = seconds;
}
}
else if (string.Equals(propertyName, Configuration.MethodDisplay, StringComparison.OrdinalIgnoreCase))
{
var stringValue = propertyValue as JsonString;
if (stringValue != null)
{
try
{
var methodDisplay = Enum.Parse(typeof(TestMethodDisplay), stringValue, true);
result.MethodDisplay = (TestMethodDisplay)methodDisplay;
}
catch { }
}
}
else if (string.Equals(propertyName, Configuration.MethodDisplayOptions, StringComparison.OrdinalIgnoreCase))
{
var stringValue = propertyValue as JsonString;
if (stringValue != null)
{
try
{
var methodDisplayOptions = Enum.Parse(typeof(TestMethodDisplayOptions), stringValue, true);
result.MethodDisplayOptions = (TestMethodDisplayOptions)methodDisplayOptions;
}
catch { }
}
}
else if (string.Equals(propertyName, Configuration.ParallelAlgorithm, StringComparison.OrdinalIgnoreCase))
{
var stringValue = propertyValue as JsonString;
if (stringValue != null)
{
try
{
var parallelAlgorithm = Enum.Parse(typeof(ParallelAlgorithm), stringValue, true);
result.ParallelAlgorithm = (ParallelAlgorithm)parallelAlgorithm;
}
catch { }
}
}
else if (string.Equals(propertyName, Configuration.AppDomain, StringComparison.OrdinalIgnoreCase))
{
var stringValue = propertyValue as JsonString;
if (stringValue != null)
{
try
{
var appDomain = Enum.Parse(typeof(AppDomainSupport), stringValue, true);
result.AppDomain = (AppDomainSupport)appDomain;
}
catch { }
}
}
}
}
}
catch (Exception ex)
{
warnings?.Add(string.Format(CultureInfo.CurrentCulture, "Exception loading {0}: {1}", ConfigDescription(), ex.Message));
}
return result;
}
static TestAssemblyConfiguration LoadFile(string configFileName, List<string> warnings = null)
{
#if !NETSTANDARD1_1
if (!File.Exists(configFileName))
return null;
#endif
try
{
using (var stream = File_OpenRead(configFileName))
return LoadConfiguration(stream, configFileName, warnings);
}
catch { }
return null;
}
#if NETSTANDARD1_1
static Lazy<MethodInfo> fileOpenReadMethod = new Lazy<MethodInfo>(GetFileOpenReadMethod);
static MethodInfo GetFileOpenReadMethod()
{
var fileType = Type.GetType("System.IO.File");
if (fileType == null)
throw new InvalidOperationException("Could not load type: System.IO.File");
var fileOpenReadMethod = fileType.GetRuntimeMethod("OpenRead", new[] { typeof(string) });
if (fileOpenReadMethod == null)
throw new InvalidOperationException("Could not find method: System.IO.File.OpenRead");
return fileOpenReadMethod;
}
static Stream File_OpenRead(string path)
{
return (Stream)fileOpenReadMethod.Value.Invoke(null, new object[] { path });
}
#else
static Stream File_OpenRead(string path)
{
return File.OpenRead(path);
}
#endif
static class Configuration
{
public const string AppDomain = "appDomain";
public const string DiagnosticMessages = "diagnosticMessages";
public const string FailSkips = "failSkips";
public const string InternalDiagnosticMessages = "internalDiagnosticMessages";
public const string LongRunningTestSeconds = "longRunningTestSeconds";
public const string MaxParallelThreads = "maxParallelThreads";
public const string MethodDisplay = "methodDisplay";
public const string MethodDisplayOptions = "methodDisplayOptions";
public const string ParallelAlgorithm = "parallelAlgorithm";
public const string ParallelizeAssembly = "parallelizeAssembly";
public const string ParallelizeTestCollections = "parallelizeTestCollections";
public const string PreEnumerateTheories = "preEnumerateTheories";
public const string ShadowCopy = "shadowCopy";
public const string StopOnFail = "stopOnFail";
}
}
}