-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTestJavaScript.cs
More file actions
251 lines (227 loc) · 10.5 KB
/
Copy pathUnitTestJavaScript.cs
File metadata and controls
251 lines (227 loc) · 10.5 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
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
using System.Diagnostics;
namespace WebExpress.WebApp.Test.JsTest
{
/// <summary>
/// Runs the headless JavaScript tests under JsTest as part of the regular
/// xUnit run. The JavaScript tests exercise the shipped client sources and
/// therefore need a JavaScript engine; they are executed through the
/// Node.js test runner. Node.js is an optional, external prerequisite, so
/// a missing or outdated installation skips the test with a warning
/// instead of failing the build. The lookup is platform independent: an
/// explicit WEBEXPRESS_NODE override, the PATH, and well-known install
/// locations (including the Node.js bundled with Visual Studio) are
/// probed in that order.
/// </summary>
public class UnitTestJavaScript
{
/// <summary>
/// The minimum Node.js major version required by the test harness.
/// </summary>
private const int MinimumNodeMajorVersion = 18;
/// <summary>
/// The maximum time the Node.js test runner may take before the run
/// is treated as failed.
/// </summary>
private static readonly TimeSpan _timeout = TimeSpan.FromMinutes(2);
private readonly ITestOutputHelper _output;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="output">The xUnit output sink for the runner log.</param>
public UnitTestJavaScript(ITestOutputHelper output)
{
_output = output;
}
/// <summary>
/// Executes every JsTest/*.test.mjs file through the Node.js test
/// runner and fails with the captured runner output when a JavaScript
/// test fails. Skips with a warning when Node.js is unavailable.
/// </summary>
[Fact]
public void RunJavaScriptTests()
{
var node = FindNodeExecutable();
if (node == null)
{
Assert.Skip("Warning: Node.js was not found (checked WEBEXPRESS_NODE, the PATH and " +
"well-known install locations). The JavaScript tests under JsTest were skipped.");
}
var major = GetNodeMajorVersion(node);
if (major < MinimumNodeMajorVersion)
{
Assert.Skip($"Warning: Node.js at '{node}' is unusable or too old " +
$"(major version {major}, required {MinimumNodeMajorVersion} or newer). " +
"The JavaScript tests under JsTest were skipped.");
}
var directory = GetJsTestDirectory();
var files = Directory.GetFiles(directory, "*.test.mjs").OrderBy(f => f).ToArray();
Assert.True(files.Length > 0, $"No *.test.mjs files were found in '{directory}'.");
var (exitCode, log) = RunNode(node, directory, ["--test", .. files]);
_output.WriteLine($"node: {node}");
_output.WriteLine(log);
Assert.True(exitCode == 0,
$"The JavaScript tests failed (node exit code {exitCode}).{Environment.NewLine}{log}");
}
/// <summary>
/// Locates the Node.js executable in a platform independent way. The
/// WEBEXPRESS_NODE environment variable wins so CI systems can pin a
/// specific runtime; otherwise the PATH and the conventional install
/// locations of the current platform are probed.
/// </summary>
/// <returns>The full path of the executable, or null when not found.</returns>
private static string FindNodeExecutable()
{
var overridePath = Environment.GetEnvironmentVariable("WEBEXPRESS_NODE");
if (!string.IsNullOrWhiteSpace(overridePath) && File.Exists(overridePath))
{
return overridePath;
}
var fileName = OperatingSystem.IsWindows() ? "node.exe" : "node";
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? "";
foreach (var entry in pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
var candidate = Path.Combine(entry.Trim(), fileName);
if (File.Exists(candidate))
{
return candidate;
}
}
return EnumerateWellKnownLocations().FirstOrDefault(File.Exists);
}
/// <summary>
/// Yields the conventional Node.js install locations of the current
/// platform. On Windows this includes the private Node.js that Visual
/// Studio ships for its build tooling, which is sufficient for the
/// test runner even when no standalone Node.js is installed.
/// </summary>
/// <returns>Candidate paths of the executable.</returns>
private static IEnumerable<string> EnumerateWellKnownLocations()
{
if (!OperatingSystem.IsWindows())
{
yield return "/usr/local/bin/node";
yield return "/usr/bin/node";
yield return "/opt/homebrew/bin/node";
yield break;
}
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
yield return Path.Combine(programFiles, "nodejs", "node.exe");
yield return Path.Combine(programFilesX86, "nodejs", "node.exe");
yield return Path.Combine(localAppData, "Programs", "nodejs", "node.exe");
var visualStudioRoot = Path.Combine(programFiles, "Microsoft Visual Studio");
if (!Directory.Exists(visualStudioRoot))
{
yield break;
}
foreach (var versionDirectory in Directory.EnumerateDirectories(visualStudioRoot))
{
foreach (var editionDirectory in Directory.EnumerateDirectories(versionDirectory))
{
yield return Path.Combine(editionDirectory,
"MSBuild", "Microsoft", "VisualStudio", "NodeJs", "node.exe");
}
}
}
/// <summary>
/// Determines the major version of a Node.js executable by invoking
/// "node --version".
/// </summary>
/// <param name="node">The path of the executable.</param>
/// <returns>The major version, or -1 when the probe fails.</returns>
private static int GetNodeMajorVersion(string node)
{
try
{
var info = new ProcessStartInfo
{
FileName = node,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
info.ArgumentList.Add("--version");
using var process = Process.Start(info);
var version = process.StandardOutput.ReadToEnd().Trim();
process.WaitForExit(10000);
// the output has the form "v24.12.0"
var major = version.TrimStart('v').Split('.')[0];
return int.TryParse(major, out var value) ? value : -1;
}
catch
{
return -1;
}
}
/// <summary>
/// Locates the JsTest source folder by walking up from the test
/// assembly location to the project directory. The tests must run
/// from the source tree because the harness resolves the shipped
/// JavaScript assets relative to its own location.
/// </summary>
/// <returns>The full path of the JsTest folder.</returns>
private static string GetJsTestDirectory()
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory != null)
{
var candidate = Path.Combine(directory.FullName, "JsTest");
if (Directory.Exists(candidate))
{
return candidate;
}
directory = directory.Parent;
}
throw new DirectoryNotFoundException(
$"The JsTest folder was not found above '{AppContext.BaseDirectory}'.");
}
/// <summary>
/// Runs the Node.js executable with the given arguments and captures
/// the combined output.
/// </summary>
/// <param name="node">The path of the executable.</param>
/// <param name="workingDirectory">The working directory of the run.</param>
/// <param name="arguments">The command line arguments.</param>
/// <returns>The exit code and the combined stdout/stderr log.</returns>
private static (int ExitCode, string Log) RunNode(string node, string workingDirectory, IEnumerable<string> arguments)
{
var info = new ProcessStartInfo
{
FileName = node,
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
foreach (var argument in arguments)
{
info.ArgumentList.Add(argument);
}
using var process = Process.Start(info);
// drain both streams concurrently so neither pipe can fill up and
// deadlock the runner
var stdout = process.StandardOutput.ReadToEndAsync();
var stderr = process.StandardError.ReadToEndAsync();
if (!process.WaitForExit((int)_timeout.TotalMilliseconds))
{
try
{
process.Kill(entireProcessTree: true);
}
catch
{
// the process ended between the timeout and the kill
}
return (-1, $"The Node.js test runner timed out after {_timeout.TotalSeconds:0} seconds.");
}
process.WaitForExit();
var log = string.Join(Environment.NewLine,
new[] { stdout.GetAwaiter().GetResult(), stderr.GetAwaiter().GetResult() }
.Where(part => !string.IsNullOrWhiteSpace(part)));
return (process.ExitCode, log);
}
}
}