-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathBuildTestBase.cs
744 lines (631 loc) · 34.2 KB
/
BuildTestBase.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;
using System.Xml;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
#nullable enable
// [assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]
namespace Wasm.Build.Tests
{
public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixture>, IDisposable
{
public const string DefaultTargetFramework = "net9.0";
protected static readonly bool s_skipProjectCleanup;
protected static readonly string s_xharnessRunnerCommand;
protected string? _projectDir;
protected readonly ITestOutputHelper _testOutput;
protected string _logPath;
protected bool _enablePerTestCleanup = false;
protected SharedBuildPerTestClassFixture _buildContext;
protected string _nugetPackagesDir = string.Empty;
/* This will trigger importing WasmOverridePacks.targets for the tests,
* which will override the runtime pack with with the locally built one.
* But note that this only partially helps with "switching workloads" because
* the tasks/targets, aot compiler, etc would still be from the old version
*/
public bool UseWBTOverridePackTargets = false;
// FIXME: use an envvar to override this
protected static int s_defaultPerTestTimeoutMs = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 30*60*1000 : 15*60*1000;
protected static BuildEnvironment s_buildEnv;
private const string s_runtimePackPathPattern = "\\*\\* MicrosoftNetCoreAppRuntimePackDir : '([^ ']*)'";
private const string s_nugetInsertionTag = "<!-- TEST_RESTORE_SOURCES_INSERTION_LINE -->";
private static Regex s_runtimePackPathRegex;
private static int s_testCounter;
private readonly int _testIdx;
public static bool IsUsingWorkloads => s_buildEnv.IsWorkload;
public static bool IsNotUsingWorkloads => !s_buildEnv.IsWorkload;
public static string GetNuGetConfigPathFor(string targetFramework) =>
Path.Combine(BuildEnvironment.TestDataPath, "nuget9.config");
// targetFramework == "net7.0" ? "nuget7.config" : "nuget8.config");
static BuildTestBase()
{
try
{
s_buildEnv = new BuildEnvironment();
if (EnvironmentVariables.WasiSdkPath is null)
throw new Exception($"Error: WASI_SDK_PATH is not set");
s_buildEnv.EnvVars["WASI_SDK_PATH"] = EnvironmentVariables.WasiSdkPath;
s_runtimePackPathRegex = new Regex(s_runtimePackPathPattern);
s_skipProjectCleanup = !string.IsNullOrEmpty(EnvironmentVariables.SkipProjectCleanup) && EnvironmentVariables.SkipProjectCleanup == "1";
if (string.IsNullOrEmpty(EnvironmentVariables.XHarnessCliPath))
s_xharnessRunnerCommand = "xharness";
else
s_xharnessRunnerCommand = EnvironmentVariables.XHarnessCliPath;
Console.WriteLine ("");
Console.WriteLine ($"==============================================================================================");
Console.WriteLine ($"=============== Running with {(s_buildEnv.IsWorkload ? "Workloads" : "No workloads")} ===============");
Console.WriteLine ($"==============================================================================================");
Console.WriteLine ("");
}
catch (Exception ex)
{
Console.WriteLine ($"Exception: {ex}");
throw;
}
}
public BuildTestBase(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
{
_testIdx = Interlocked.Increment(ref s_testCounter);
_buildContext = buildContext;
_testOutput = new TestOutputWrapper(output);
_logPath = s_buildEnv.LogRootPath; // FIXME:
}
public static IEnumerable<IEnumerable<object?>> ConfigWithAOTData(bool aot, string? config=null)
{
if (config == null)
{
return new IEnumerable<object?>[]
{
#if TEST_DEBUG_CONFIG_ALSO
// list of each member data - for Debug+@aot
new object?[] { new BuildArgs("placeholder", "Debug", aot, "placeholder", string.Empty) }.AsEnumerable(),
#endif
// list of each member data - for Release+@aot
new object?[] { new BuildArgs("placeholder", "Release", aot, "placeholder", string.Empty) }.AsEnumerable()
}.AsEnumerable();
}
else
{
return new IEnumerable<object?>[]
{
new object?[] { new BuildArgs("placeholder", config, aot, "placeholder", string.Empty) }.AsEnumerable()
};
}
}
[MemberNotNull(nameof(_projectDir), nameof(_logPath))]
protected void InitPaths(string id)
{
if (_projectDir == null)
_projectDir = Path.Combine(BuildEnvironment.TmpPath, id);
_logPath = Path.Combine(s_buildEnv.LogRootPath, id);
_nugetPackagesDir = Path.Combine(BuildEnvironment.TmpPath, "nuget", id);
if (Directory.Exists(_nugetPackagesDir))
Directory.Delete(_nugetPackagesDir, recursive: true);
Directory.CreateDirectory(_nugetPackagesDir!);
Directory.CreateDirectory(_logPath);
}
protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages = false, string targetFramework = DefaultTargetFramework)
{
Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, "Directory.Build.props"), s_buildEnv.DirectoryBuildPropsContents);
File.WriteAllText(Path.Combine(dir, "Directory.Build.targets"), s_buildEnv.DirectoryBuildTargetsContents);
File.Copy(BuildEnvironment.WasmOverridePacksTargetsPath, Path.Combine(dir, Path.GetFileName(BuildEnvironment.WasmOverridePacksTargetsPath)), overwrite: true);
string targetNuGetConfigPath = Path.Combine(dir, "nuget.config");
if (addNuGetSourceForLocalPackages)
{
File.WriteAllText(targetNuGetConfigPath,
GetNuGetConfigWithLocalPackagesPath(
GetNuGetConfigPathFor(targetFramework),
s_buildEnv.BuiltNuGetsPath));
}
else
{
File.Copy(GetNuGetConfigPathFor(targetFramework), targetNuGetConfigPath);
}
}
protected const string SimpleProjectTemplate =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{DefaultTargetFramework}</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Exe</OutputType>
##EXTRA_PROPERTIES##
</PropertyGroup>
<ItemGroup>
##EXTRA_ITEMS##
</ItemGroup>
##INSERT_AT_END##
</Project>";
protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProperties="", string extraItems="", string insertAtEnd="", string projectTemplate=SimpleProjectTemplate)
{
if (buildArgs.AOT)
{
extraProperties = $"{extraProperties}\n<RunAOTCompilation>true</RunAOTCompilation>";
}
string projectContents = projectTemplate
.Replace("##EXTRA_PROPERTIES##", extraProperties)
.Replace("##EXTRA_ITEMS##", extraItems)
.Replace("##INSERT_AT_END##", insertAtEnd);
return buildArgs with { ProjectFileContents = projectContents };
}
public (string projectDir, string buildOutput) BuildProject(BuildArgs buildArgs,
string id,
BuildProjectOptions options)
{
string msgPrefix = options.Label != null ? $"[{options.Label}] " : string.Empty;
if (options.UseCache && _buildContext.TryGetBuildFor(buildArgs, out BuildProduct? product))
{
_testOutput.WriteLine ($"Using existing build found at {product.ProjectDir}, with build log at {product.LogFile}");
if (!product.Result)
throw new XunitException($"Found existing build at {product.ProjectDir}, but it had failed. Check build log at {product.LogFile}");
_projectDir = product.ProjectDir;
// use this test's id for the run logs
_logPath = Path.Combine(s_buildEnv.LogRootPath, id);
return (_projectDir, "FIXME");
}
if (options.CreateProject)
{
InitPaths(id);
InitProjectDir(_projectDir);
options.InitProject?.Invoke();
File.WriteAllText(Path.Combine(_projectDir, $"{buildArgs.ProjectName}.csproj"), buildArgs.ProjectFileContents);
File.Copy(Path.Combine(AppContext.BaseDirectory,
options.TargetFramework == "net7.0" ? "data/test-main-7.0.js" : "test-main.js"),
Path.Combine(_projectDir, "test-main.js"));
}
else if (_projectDir is null)
{
throw new Exception("_projectDir should be set, to use options.createProject=false");
}
StringBuilder sb = new();
sb.Append(options.Publish ? "publish" : "build");
if (options.Publish && options.BuildOnlyAfterPublish)
sb.Append(" -p:WasmBuildOnlyAfterPublish=true");
sb.Append($" {s_buildEnv.DefaultBuildArgs}");
sb.Append($" /p:Configuration={buildArgs.Config}");
string logFileSuffix = options.Label == null ? string.Empty : options.Label.Replace(' ', '_');
string logFilePath = Path.Combine(_logPath, $"{buildArgs.ProjectName}{logFileSuffix}.binlog");
_testOutput.WriteLine($"-------- Building ---------");
_testOutput.WriteLine($"Binlog path: {logFilePath}");
sb.Append($" /bl:\"{logFilePath}\" /nologo");
sb.Append($" /v:{options.Verbosity ?? "minimal"}");
if (buildArgs.ExtraBuildArgs != null)
sb.Append($" {buildArgs.ExtraBuildArgs} ");
_testOutput.WriteLine($"Building {buildArgs.ProjectName} in {_projectDir}");
(int exitCode, string buildOutput) result;
try
{
var envVars = s_buildEnv.EnvVars;
if (options.ExtraBuildEnvironmentVariables is not null)
{
envVars = new Dictionary<string, string>(s_buildEnv.EnvVars);
foreach (var kvp in options.ExtraBuildEnvironmentVariables!)
envVars[kvp.Key] = kvp.Value;
}
envVars["NUGET_PACKAGES"] = _nugetPackagesDir;
if (UseWBTOverridePackTargets && s_buildEnv.IsWorkload)
envVars["WBTOverrideRuntimePack"] = "true";
result = AssertBuild(sb.ToString(), id, expectSuccess: options.ExpectSuccess, envVars: envVars);
// check that we are using the correct runtime pack!
if (options.ExpectSuccess && options.AssertAppBundle)
{
AssertRuntimePackPath(result.buildOutput, options.TargetFramework ?? DefaultTargetFramework);
string bundleDir = Path.Combine(GetBinDir(config: buildArgs.Config, targetFramework: options.TargetFramework ?? DefaultTargetFramework), "AppBundle");
AssertBasicAppBundle(bundleDir,
buildArgs.ProjectName,
buildArgs.Config,
options.TargetFramework ?? DefaultTargetFramework,
options.HasIcudt,
options.DotnetWasmFromRuntimePack ?? !buildArgs.AOT);
}
if (options.UseCache)
_buildContext.CacheBuild(buildArgs, new BuildProduct(_projectDir, logFilePath, true));
return (_projectDir, result.buildOutput);
}
catch
{
if (options.UseCache)
_buildContext.CacheBuild(buildArgs, new BuildProduct(_projectDir, logFilePath, false));
throw;
}
}
private static string GetNuGetConfigWithLocalPackagesPath(string templatePath, string localNuGetsPath)
{
string contents = File.ReadAllText(templatePath);
if (contents.IndexOf(s_nugetInsertionTag, StringComparison.InvariantCultureIgnoreCase) < 0)
throw new Exception($"Could not find {s_nugetInsertionTag} in {templatePath}");
return contents.Replace(s_nugetInsertionTag, $@"<add key=""nuget-local"" value=""{localNuGetsPath}"" />");
}
public string CreateWasmTemplateProject(string id, string template = "wasmbrowser", string extraArgs = "", bool runAnalyzers = true)
{
InitPaths(id);
InitProjectDir(_projectDir, addNuGetSourceForLocalPackages: true);
File.WriteAllText(Path.Combine(_projectDir, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(_projectDir, "Directory.Build.targets"),
"""
<Project>
<Target Name="PrintRuntimePackPath" BeforeTargets="Build">
<Message Text="** MicrosoftNetCoreAppRuntimePackDir : '@(ResolvedRuntimePack -> '%(PackageDirectory)')'" Importance="High" Condition="@(ResolvedRuntimePack->Count()) > 0" />
</Target>
<Import Project="WasmOverridePacks.targets" />
</Project>
""");
new DotNetCommand(s_buildEnv, _testOutput, useDefaultArgs: false)
.WithWorkingDirectory(_projectDir!)
.ExecuteWithCapturedOutput($"new {template} {extraArgs}")
.EnsureSuccessful();
string projectfile = Path.Combine(_projectDir!, $"{id}.csproj");
if (runAnalyzers)
AddItemsPropertiesToProject("<RunAnalyzers>true</RunAnalyzers>");
return projectfile;
}
protected (CommandResult, string) BuildInternal(string id, string config, bool publish=false, bool setWasmDevel=true, params string[] extraArgs)
{
string label = publish ? "publish" : "build";
_testOutput.WriteLine($"{Environment.NewLine}** {label} **{Environment.NewLine}");
string logPath = Path.Combine(s_buildEnv.LogRootPath, id, $"{id}-{label}.binlog");
string[] combinedArgs = new[]
{
label, // same as the command name
$"-bl:{logPath}",
$"-p:Configuration={config}",
"-p:BlazorEnableCompression=false",
"-nr:false",
setWasmDevel ? "-p:_WasmDevel=true" : string.Empty
}.Concat(extraArgs).ToArray();
CommandResult res = new DotNetCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!)
.WithEnvironmentVariable("NUGET_PACKAGES", _nugetPackagesDir)
.ExecuteWithCapturedOutput(combinedArgs)
.EnsureSuccessful();
return (res, logPath);
}
static void AssertRuntimePackPath(string buildOutput, string targetFramework)
{
var match = s_runtimePackPathRegex.Match(buildOutput);
if (!match.Success || match.Groups.Count != 2)
throw new XunitException($"Could not find the pattern in the build output: '{s_runtimePackPathPattern}'.{Environment.NewLine}Build output: {buildOutput}");
string expectedRuntimePackDir = s_buildEnv.GetRuntimePackDir(targetFramework);
string actualPath = match.Groups[1].Value;
if (string.Compare(actualPath, expectedRuntimePackDir) != 0)
throw new XunitException($"Runtime pack path doesn't match.{Environment.NewLine}Expected: '{expectedRuntimePackDir}'{Environment.NewLine}Actual: '{actualPath}'");
}
protected static void AssertBasicAppBundle(string bundleDir,
string projectName,
string config,
string targetFramework,
bool hasIcudt = true,
bool dotnetWasmFromRuntimePack = true)
{
#if false
AssertFilesExist(bundleDir, new []
{
"dotnet.wasm",
"_framework/blazor.boot.json",
"dotnet.js"
});
AssertFilesExist(bundleDir, new[] { "icudt.dat" }, expectToExist: hasIcudt);
string managedDir = Path.Combine(bundleDir, "managed");
AssertFilesExist(managedDir, new[] { $"{projectName}.dll" });
bool is_debug = config == "Debug";
if (is_debug)
{
// Use cecil to check embedded pdb?
// AssertFilesExist(managedDir, new[] { $"{projectName}.pdb" });
//FIXME: um.. what about these? embedded? why is linker omitting them?
//foreach (string file in Directory.EnumerateFiles(managedDir, "*.dll"))
//{
//string pdb = Path.ChangeExtension(file, ".pdb");
//Assert.True(File.Exists(pdb), $"Could not find {pdb} for {file}");
//}
}
AssertDotNetWasmJs(bundleDir, fromRuntimePack: dotnetWasmFromRuntimePack, targetFramework);
#endif
}
protected static void AssertFilesDontExist(string dir, string[] filenames, string? label = null)
=> AssertFilesExist(dir, filenames, label, expectToExist: false);
protected static void AssertFilesExist(string dir, string[] filenames, string? label = null, bool expectToExist=true)
{
string prefix = label != null ? $"{label}: " : string.Empty;
if (!Directory.Exists(dir))
throw new XunitException($"[{label}] {dir} not found");
foreach (string filename in filenames)
{
string path = Path.Combine(dir, filename);
if (expectToExist && !File.Exists(path))
throw new XunitException($"{prefix}Expected the file to exist: {path}");
if (!expectToExist && File.Exists(path))
throw new XunitException($"{prefix}Expected the file to *not* exist: {path}");
}
}
protected static void AssertSameFile(string file0, string file1, string? label=null) => AssertFile(file0, file1, label, same: true);
protected static void AssertNotSameFile(string file0, string file1, string? label=null) => AssertFile(file0, file1, label, same: false);
protected static void AssertFile(string file0, string file1, string? label=null, bool same=true)
{
Assert.True(File.Exists(file0), $"{label}: Expected to find {file0}");
Assert.True(File.Exists(file1), $"{label}: Expected to find {file1}");
FileInfo finfo0 = new(file0);
FileInfo finfo1 = new(file1);
if (same && finfo0.Length != finfo1.Length)
throw new XunitException($"{label}:{Environment.NewLine} File sizes don't match for {file0} ({finfo0.Length}), and {file1} ({finfo1.Length})");
if (!same && finfo0.Length == finfo1.Length)
throw new XunitException($"{label}:{Environment.NewLine} File sizes should not match for {file0} ({finfo0.Length}), and {file1} ({finfo1.Length})");
}
protected (int exitCode, string buildOutput) AssertBuild(string args, string label="build", bool expectSuccess=true, IDictionary<string, string>? envVars=null, int? timeoutMs=null)
{
var result = RunProcess(s_buildEnv.DotNet, _testOutput, args, workingDir: _projectDir, label: label, envVars: envVars, timeoutMs: timeoutMs ?? s_defaultPerTestTimeoutMs);
if (expectSuccess && result.exitCode != 0)
throw new XunitException($"Build process exited with non-zero exit code: {result.exitCode}");
if (!expectSuccess && result.exitCode == 0)
throw new XunitException($"Build should have failed, but it didn't. Process exited with exitCode : {result.exitCode}");
return result;
}
private string FindSubDirIgnoringCase(string parentDir, string dirName)
{
IEnumerable<string> matchingDirs = Directory.EnumerateDirectories(parentDir,
dirName,
new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive });
string? first = matchingDirs.FirstOrDefault();
if (matchingDirs.Count() > 1)
throw new Exception($"Found multiple directories with names that differ only in case. {string.Join(", ", matchingDirs.ToArray())}");
return first ?? Path.Combine(parentDir, dirName);
}
protected string GetBinDir(string config, string targetFramework=DefaultTargetFramework, string? baseDir=null)
{
var dir = baseDir ?? _projectDir;
Assert.NotNull(dir);
return Path.Combine(dir!, "bin", config, targetFramework, BuildEnvironment.DefaultRuntimeIdentifier);
}
protected string GetObjDir(string config, string targetFramework=DefaultTargetFramework, string? baseDir=null)
{
var dir = baseDir ?? _projectDir;
Assert.NotNull(dir);
return Path.Combine(dir!, "obj", config, targetFramework, BuildEnvironment.DefaultRuntimeIdentifier);
}
public static (int exitCode, string buildOutput) RunProcess(string path,
ITestOutputHelper _testOutput,
string args = "",
IDictionary<string, string>? envVars = null,
string? workingDir = null,
string? label = null,
bool logToXUnit = true,
int? timeoutMs = null)
{
var t = RunProcessAsync(path, _testOutput, args, envVars, workingDir, label, logToXUnit, timeoutMs);
t.Wait();
return t.Result;
}
public static async Task<(int exitCode, string buildOutput)> RunProcessAsync(string path,
ITestOutputHelper _testOutput,
string args = "",
IDictionary<string, string>? envVars = null,
string? workingDir = null,
string? label = null,
bool logToXUnit = true,
int? timeoutMs = null)
{
_testOutput.WriteLine($"Running {path} {args}");
_testOutput.WriteLine($"WorkingDirectory: {workingDir}");
StringBuilder outputBuilder = new ();
object syncObj = new();
var processStartInfo = new ProcessStartInfo
{
FileName = path,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
};
if (workingDir == null || !Directory.Exists(workingDir))
throw new Exception($"Working directory {workingDir} not found");
if (workingDir != null)
processStartInfo.WorkingDirectory = workingDir;
if (envVars != null)
{
if (envVars.Count > 0)
_testOutput.WriteLine("Setting environment variables for execution:");
foreach (KeyValuePair<string, string> envVar in envVars)
{
processStartInfo.EnvironmentVariables[envVar.Key] = envVar.Value;
_testOutput.WriteLine($"\t{envVar.Key} = {envVar.Value}");
}
// runtime repo sets this, which interferes with the tests
processStartInfo.RemoveEnvironmentVariables("MSBuildSDKsPath");
}
Process process = new ();
process.StartInfo = processStartInfo;
process.EnableRaisingEvents = true;
// AutoResetEvent resetEvent = new (false);
// process.Exited += (_, _) => { _testOutput.WriteLine ($"- exited called"); resetEvent.Set(); };
if (!process.Start())
throw new ArgumentException("No process was started: process.Start() return false.");
try
{
DataReceivedEventHandler logStdErr = (sender, e) => LogData($"[{label}-stderr]", e.Data);
DataReceivedEventHandler logStdOut = (sender, e) => LogData($"[{label}]", e.Data);
process.ErrorDataReceived += logStdErr;
process.OutputDataReceived += logStdOut;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
using CancellationTokenSource cts = new();
cts.CancelAfter(timeoutMs ?? s_defaultPerTestTimeoutMs);
await process.WaitForExitAsync(cts.Token);
if (cts.IsCancellationRequested)
{
// process didn't exit
process.Kill(entireProcessTree: true);
lock (syncObj)
{
var lastLines = outputBuilder.ToString().Split('\r', '\n').TakeLast(20);
throw new XunitException($"Process timed out. Last 20 lines of output:{Environment.NewLine}{string.Join(Environment.NewLine, lastLines)}");
}
}
// this will ensure that all the async event handling has completed
// and should be called after process.WaitForExit(int)
// https://learn.microsoft.com/dotnet/api/system.diagnostics.process.waitforexit?view=net-5.0#System_Diagnostics_Process_WaitForExit_System_Int32_
process.WaitForExit();
process.ErrorDataReceived -= logStdErr;
process.OutputDataReceived -= logStdOut;
process.CancelErrorRead();
process.CancelOutputRead();
lock (syncObj)
{
var exitCode = process.ExitCode;
return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n'));
}
}
catch (Exception ex)
{
_testOutput.WriteLine($"-- exception -- {ex}");
throw;
}
void LogData(string label, string? message)
{
lock (syncObj)
{
if (logToXUnit && message != null)
{
_testOutput.WriteLine($"{label} {message}");
}
outputBuilder.AppendLine($"{label} {message}");
}
}
}
public static string AddItemsPropertiesToProject(string projectFile, string? extraProperties=null, string? extraItems=null, string? atTheEnd=null)
{
if (extraProperties == null && extraItems == null && atTheEnd == null)
return projectFile;
XmlDocument doc = new();
doc.Load(projectFile);
XmlNode root = doc.DocumentElement ?? throw new Exception();
if (extraItems != null)
{
XmlNode node = doc.CreateNode(XmlNodeType.Element, "ItemGroup", null);
node.InnerXml = extraItems;
root.AppendChild(node);
}
if (extraProperties != null)
{
XmlNode node = doc.CreateNode(XmlNodeType.Element, "PropertyGroup", null);
node.InnerXml = extraProperties;
root.AppendChild(node);
}
if (atTheEnd != null)
{
XmlNode node = doc.CreateNode(XmlNodeType.DocumentFragment, "foo", null);
node.InnerXml = atTheEnd;
root.InsertAfter(node, root.LastChild);
}
doc.Save(projectFile);
return projectFile;
}
public void Dispose()
{
if (_projectDir != null && _enablePerTestCleanup)
_buildContext.RemoveFromCache(_projectDir, keepDir: s_skipProjectCleanup);
}
public static string GetRandomId() => TestUtils.FixupSymbolName(Path.GetRandomFileName());
private static string GetEnvironmentVariableOrDefault(string envVarName, string defaultValue)
{
string? value = Environment.GetEnvironmentVariable(envVarName);
return string.IsNullOrEmpty(value) ? defaultValue : value;
}
internal BuildPaths GetBuildPaths(BuildArgs buildArgs, bool forPublish=true)
{
string objDir = GetObjDir(buildArgs.Config);
string bundleDir = Path.Combine(GetBinDir(baseDir: _projectDir, config: buildArgs.Config), "AppBundle");
string wasmDir = Path.Combine(objDir, "wasm", forPublish ? "for-publish" : "for-build");
return new BuildPaths(wasmDir, objDir, GetBinDir(buildArgs.Config), bundleDir);
}
internal IDictionary<string, FileStat> StatFiles(IEnumerable<string> fullpaths)
{
Dictionary<string, FileStat> table = new();
foreach (string file in fullpaths)
{
if (File.Exists(file))
table.Add(Path.GetFileName(file), new FileStat(FullPath: file, Exists: true, LastWriteTimeUtc: File.GetLastWriteTimeUtc(file), Length: new FileInfo(file).Length));
else
table.Add(Path.GetFileName(file), new FileStat(FullPath: file, Exists: false, LastWriteTimeUtc: DateTime.MinValue, Length: 0));
}
return table;
}
protected static string GetSkiaSharpReferenceItems()
=> @"<PackageReference Include=""SkiaSharp"" Version=""2.88.3"" />
<PackageReference Include=""SkiaSharp.NativeAssets.WebAssembly"" Version=""2.88.3"" />
<NativeFileReference Include=""$(SkiaSharpStaticLibraryPath)\3.1.12\st\*.a"" />";
protected static string s_mainReturns42 = @"
public class TestClass {
public static int Main()
{
return 42;
}
}";
public static IEnumerable<object?[]> TestDataForConsolePublishAndRun() =>
new IEnumerable<object?>[]
{
new object?[] { "Debug" },
new object?[] { "Release" }
}
.AsEnumerable()
.MultiplyWithSingleArgs(true, false) /*propertyValue*/
.MultiplyWithSingleArgs(true, false) /*aot*/
.UnwrapItemsAsArrays();
protected CommandResult RunWithoutBuild(string config, string id, bool enableHttp = false)
{
// wasmtime --wasi http is necessary because the default dotnet.wasm (without native rebuild depends on wasi:http world)
string runArgs = $"run --no-build -c {config} --forward-exit-code";
if (enableHttp)
{
runArgs += " --extra-host-arg=--wasi --extra-host-arg=http";
}
runArgs += " x y z";
int expectedExitCode = 42;
CommandResult res = new RunCommand(s_buildEnv, _testOutput, label: id)
.WithWorkingDirectory(_projectDir!)
.ExecuteWithCapturedOutput(runArgs)
.EnsureExitCode(expectedExitCode);
Assert.Contains("Hello, Wasi Console!", res.Output);
Assert.Contains("args[0] = x", res.Output);
Assert.Contains("args[1] = y", res.Output);
Assert.Contains("args[2] = z", res.Output);
return res;
}
}
public record BuildArgs(string ProjectName,
string Config,
bool AOT,
string ProjectFileContents,
string? ExtraBuildArgs);
public record BuildProduct(string ProjectDir, string LogFile, bool Result);
public record BuildProjectOptions
(
Action? InitProject = null,
bool? DotnetWasmFromRuntimePack = null,
bool HasIcudt = true,
bool UseCache = true,
bool ExpectSuccess = true,
bool AssertAppBundle = true,
bool CreateProject = true,
bool Publish = true,
bool BuildOnlyAfterPublish = true,
string? Verbosity = null,
string? Label = null,
string? TargetFramework = null,
IDictionary<string, string>? ExtraBuildEnvironmentVariables = null
);
public enum NativeFilesType { FromRuntimePack, Relinked, AOT };
}