-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathProject.cs
554 lines (461 loc) · 20.6 KB
/
Project.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.Json;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Templates.Test.Helpers;
[DebuggerDisplay("{ToString(),nq}")]
public class Project : IDisposable
{
private const string _urlsNoHttps = "http://127.0.0.1:0";
private const string _urls = "http://127.0.0.1:0;https://127.0.0.1:0";
public static string ArtifactsLogDir
{
get
{
var helixWorkItemUploadRoot = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT");
if (!string.IsNullOrEmpty(helixWorkItemUploadRoot))
{
return helixWorkItemUploadRoot;
}
var testLogFolder = typeof(Project).Assembly.GetCustomAttribute<TestFrameworkFileLoggerAttribute>()?.BaseDirectory;
if (string.IsNullOrEmpty(testLogFolder))
{
throw new InvalidOperationException($"No test log folder specified via {nameof(TestFrameworkFileLoggerAttribute)}.");
}
return testLogFolder;
}
}
public static string DotNetEfFullPath => (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DotNetEfFullPath")))
? typeof(ProjectFactoryFixture).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.First(attribute => attribute.Key == "DotNetEfFullPath")
.Value
: Environment.GetEnvironmentVariable("DotNetEfFullPath");
public string ProjectName { get; set; }
public string ProjectArguments { get; set; }
public string ProjectGuid { get; set; }
public string TemplateOutputDir { get; set; }
public string TargetFramework { get; set; } = GetAssemblyMetadata("Test.DefaultTargetFramework");
public string RuntimeIdentifier { get; set; } = string.Empty;
public static DevelopmentCertificate DevCert { get; } = DevelopmentCertificate.Get(typeof(Project).Assembly);
public string TemplateBuildDir => Path.Combine(TemplateOutputDir, "bin", "Debug", TargetFramework, RuntimeIdentifier);
public string TemplatePublishDir => Path.Combine(TemplateOutputDir, "bin", "Release", TargetFramework, RuntimeIdentifier, "publish");
public ITestOutputHelper Output { get; set; }
public IMessageSink DiagnosticsMessageSink { get; set; }
internal async Task RunDotNetNewAsync(
string templateName,
string auth = null,
string language = null,
bool useLocalDB = false,
bool noHttps = false,
bool errorOnRestoreError = true,
bool isItemTemplate = false,
string[] args = null,
// Used to set special options in MSBuild
IDictionary<string, string> environmentVariables = null)
{
var hiveArg = $"--debug:disable-sdk-templates --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"";
var argString = $"new {templateName} {hiveArg}";
environmentVariables ??= new Dictionary<string, string>();
if (!isItemTemplate)
{
argString += " --no-restore";
}
if (!string.IsNullOrEmpty(auth))
{
argString += $" --auth {auth}";
}
if (!string.IsNullOrEmpty(language))
{
argString += $" -lang {language}";
}
if (useLocalDB)
{
argString += $" --use-local-db";
}
if (noHttps)
{
argString += $" --no-https";
}
if (args != null)
{
foreach (var arg in args)
{
argString += " " + arg;
}
}
// Save a copy of the arguments used for better diagnostic error messages later.
// We omit the hive argument and the template output dir as they are not relevant and add noise.
ProjectArguments = argString.Replace(hiveArg, "");
argString += $" -o {TemplateOutputDir}";
if (Directory.Exists(TemplateOutputDir))
{
Output.WriteLine($"Template directory already exists, deleting contents of {TemplateOutputDir}");
Directory.Delete(TemplateOutputDir, recursive: true);
}
using var createExecution = ProcessEx.Run(Output, AppContext.BaseDirectory, DotNetMuxer.MuxerPathOrDefault(), argString, environmentVariables);
await createExecution.Exited;
var createResult = new ProcessResult(createExecution);
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create", this, createResult));
if (!isItemTemplate)
{
argString = "restore /bl";
using var restoreExecution = ProcessEx.Run(Output, TemplateOutputDir, DotNetMuxer.MuxerPathOrDefault(), argString, environmentVariables);
await restoreExecution.Exited;
var restoreResult = new ProcessResult(restoreExecution);
// Because dotnet new automatically restores but silently ignores restore errors, need to handle restore errors explicitly
if (errorOnRestoreError && (restoreExecution.Output.Contains("Restore failed.") || restoreExecution.Error.Contains("Restore failed.")))
{
restoreResult.ExitCode = -1;
}
CaptureBinLogOnFailure(restoreExecution);
Assert.True(0 == restoreResult.ExitCode, ErrorMessages.GetFailedProcessMessage("restore", this, restoreResult));
}
}
internal async Task RunDotNetPublishAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool noRestore = true)
{
Output.WriteLine("Publishing ASP.NET Core application...");
// Avoid restoring as part of build or publish. These projects should have already restored as part of running dotnet new. Explicitly disabling restore
// should avoid any global contention and we can execute a build or publish in a lock-free way
var restoreArgs = noRestore ? "--no-restore" : null;
using var execution = ProcessEx.Run(Output, TemplateOutputDir, DotNetMuxer.MuxerPathOrDefault(), $"publish {restoreArgs} -c Release /bl {additionalArgs}", packageOptions);
await execution.Exited;
var result = new ProcessResult(execution);
// Fail if there were build warnings
if (execution.Output.Contains(": warning") || execution.Error.Contains(": warning"))
{
result.ExitCode = -1;
}
CaptureBinLogOnFailure(execution);
Assert.True(0 == result.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", this, result));
}
internal async Task RunDotNetBuildAsync(IDictionary<string, string> packageOptions = null, string additionalArgs = null, bool errorOnBuildWarning = true)
{
Output.WriteLine("Building ASP.NET Core application...");
// Avoid restoring as part of build or publish. These projects should have already restored as part of running dotnet new. Explicitly disabling restore
// should avoid any global contention and we can execute a build or publish in a lock-free way
using var execution = ProcessEx.Run(Output, TemplateOutputDir, DotNetMuxer.MuxerPathOrDefault(), $"build --no-restore -c Debug /bl {additionalArgs}", packageOptions);
await execution.Exited;
var result = new ProcessResult(execution);
// Fail if there were build warnings
if (errorOnBuildWarning && (execution.Output.Contains(": warning") || execution.Error.Contains(": warning")))
{
result.ExitCode = -1;
}
CaptureBinLogOnFailure(execution);
Assert.True(0 == result.ExitCode, ErrorMessages.GetFailedProcessMessage("build", this, result));
}
internal AspNetProcess StartBuiltProjectAsync(bool hasListeningUri = true, ILogger logger = null, bool noHttps = false)
{
var environment = new Dictionary<string, string>
{
["ASPNETCORE_URLS"] = noHttps ? _urlsNoHttps : _urls,
["ASPNETCORE_ENVIRONMENT"] = "Development",
["ASPNETCORE_Logging__Console__LogLevel__Default"] = "Debug",
["ASPNETCORE_Logging__Console__LogLevel__System"] = "Debug",
["ASPNETCORE_Logging__Console__LogLevel__Microsoft"] = "Debug",
["ASPNETCORE_Logging__Console__FormatterOptions__IncludeScopes"] = "true",
};
var projectDll = Path.Combine(TemplateBuildDir, $"{ProjectName}.dll");
return new AspNetProcess(DevCert, Output, TemplateOutputDir, projectDll, environment, published: false, hasListeningUri: hasListeningUri, logger: logger);
}
internal AspNetProcess StartPublishedProjectAsync(bool hasListeningUri = true, bool usePublishedAppHost = false, bool noHttps = false)
{
var environment = new Dictionary<string, string>
{
["ASPNETCORE_URLS"] = noHttps ? _urlsNoHttps : _urls,
["ASPNETCORE_Logging__Console__LogLevel__Default"] = "Debug",
["ASPNETCORE_Logging__Console__LogLevel__System"] = "Debug",
["ASPNETCORE_Logging__Console__LogLevel__Microsoft"] = "Debug",
["ASPNETCORE_Logging__Console__FormatterOptions__IncludeScopes"] = "true",
};
var projectDll = Path.Combine(TemplatePublishDir, $"{ProjectName}.dll");
return new AspNetProcess(DevCert, Output, TemplatePublishDir, projectDll, environment, published: true, hasListeningUri: hasListeningUri, usePublishedAppHost: usePublishedAppHost);
}
internal async Task RunDotNetEfCreateMigrationAsync(string migrationName)
{
var args = $"--verbose --no-build migrations add {migrationName}";
var command = DotNetMuxer.MuxerPathOrDefault();
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DotNetEfFullPath")))
{
args = $"\"{DotNetEfFullPath}\" " + args;
}
else
{
command = "dotnet-ef";
}
using var result = ProcessEx.Run(Output, TemplateOutputDir, command, args);
await result.Exited;
var processResult = new ProcessResult(result);
Assert.True(0 == processResult.ExitCode, ErrorMessages.GetFailedProcessMessage("run EF migrations", this, processResult));
}
internal async Task RunDotNetEfUpdateDatabaseAsync()
{
var args = "--verbose --no-build database update";
var command = DotNetMuxer.MuxerPathOrDefault();
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DotNetEfFullPath")))
{
args = $"\"{DotNetEfFullPath}\" " + args;
}
else
{
command = "dotnet-ef";
}
using var result = ProcessEx.Run(Output, TemplateOutputDir, command, args);
await result.Exited;
var processResult = new ProcessResult(result);
Assert.True(0 == processResult.ExitCode, ErrorMessages.GetFailedProcessMessage("update database", this, processResult));
}
// If this fails, you should generate new migrations via migrations/updateMigrations.cmd
public void AssertEmptyMigration(string migration)
{
var fullPath = Path.Combine(TemplateOutputDir, "Data/Migrations");
var file = Directory.EnumerateFiles(fullPath).Where(f => f.EndsWith($"{migration}.cs", StringComparison.Ordinal)).FirstOrDefault();
Assert.NotNull(file);
var contents = File.ReadAllText(file);
var emptyMigration = @"/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}";
// This comparison can break depending on how GIT checked out newlines on different files.
Assert.Contains(RemoveNewLines(emptyMigration), RemoveNewLines(contents));
static string RemoveNewLines(string str)
{
return str.Replace("\n", string.Empty).Replace("\r", string.Empty);
}
}
public void AssertFileExists(string path, bool shouldExist)
{
var fullPath = Path.Combine(TemplateOutputDir, path);
var doesExist = File.Exists(fullPath);
if (shouldExist)
{
Assert.True(doesExist, "Expected file to exist, but it doesn't: " + path);
}
else
{
Assert.False(doesExist, "Expected file not to exist, but it does: " + path);
}
}
public async Task VerifyLaunchSettings(string[] expectedLaunchProfileNames)
{
var launchSettingsFiles = Directory.EnumerateFiles(TemplateOutputDir, "launchSettings.json", SearchOption.AllDirectories);
foreach (var filePath in launchSettingsFiles)
{
using var launchSettingsFile = File.OpenRead(filePath);
using var launchSettings = await JsonDocument.ParseAsync(launchSettingsFile);
var profiles = launchSettings.RootElement.GetProperty("profiles");
var profilesEnumerator = profiles.EnumerateObject().GetEnumerator();
foreach (var expectedProfileName in expectedLaunchProfileNames)
{
Assert.True(profilesEnumerator.MoveNext());
var actualProfile = profilesEnumerator.Current;
// Launch profile names are case sensitive
Assert.Equal(expectedProfileName, actualProfile.Name, StringComparer.Ordinal);
if (actualProfile.Value.GetProperty("commandName").GetString() == "Project")
{
var applicationUrl = actualProfile.Value.GetProperty("applicationUrl");
if (string.Equals(expectedProfileName, "http", StringComparison.Ordinal))
{
Assert.DoesNotContain("https://", applicationUrl.GetString());
}
if (string.Equals(expectedProfileName, "https", StringComparison.Ordinal))
{
Assert.StartsWith("https://", applicationUrl.GetString());
}
}
}
// Check there are no more launch profiles defined
Assert.False(profilesEnumerator.MoveNext());
}
}
public async Task VerifyHasProperty(string propertyName, string expectedValue)
{
var projectFile = Directory.EnumerateFiles(TemplateOutputDir, "*proj").FirstOrDefault();
Assert.NotNull(projectFile);
var projectFileContents = await File.ReadAllTextAsync(projectFile);
Assert.Contains($"<{propertyName}>{expectedValue}</{propertyName}>", projectFileContents);
}
public void SetCurrentRuntimeIdentifier()
{
RuntimeIdentifier = GetRuntimeIdentifier();
static string GetRuntimeIdentifier()
{
// we need to use the "portable" RID (win-x64), not the actual RID (win10-x64)
return $"{GetOS()}-{GetArchitecture()}";
}
static string GetOS()
{
if (OperatingSystem.IsWindows())
{
return "win";
}
if (OperatingSystem.IsLinux())
{
return "linux";
}
if (OperatingSystem.IsMacOS())
{
return "osx";
}
throw new NotSupportedException();
}
static string GetArchitecture()
{
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
return "x86";
case Architecture.X64:
return "x64";
case Architecture.Arm:
return "arm";
case Architecture.Arm64:
return "arm64";
default:
throw new NotSupportedException();
}
}
}
public string ReadFile(string path)
{
AssertFileExists(path, shouldExist: true);
return File.ReadAllText(Path.Combine(TemplateOutputDir, path));
}
internal async Task RunDotNetNewRawAsync(string arguments)
{
var result = ProcessEx.Run(
Output,
AppContext.BaseDirectory,
DotNetMuxer.MuxerPathOrDefault(),
arguments +
$" --debug:disable-sdk-templates --debug:custom-hive \"{TemplatePackageInstaller.CustomHivePath}\"" +
$" -o {TemplateOutputDir}");
await result.Exited;
Assert.True(result.ExitCode == 0, result.GetFormattedOutput());
}
public void Dispose()
{
var doNotCleanUpTemplates = typeof(ProjectFactoryFixture).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Single(attribute => attribute.Key == "DoNotCleanUpTemplates")
.Value;
if (string.Equals(doNotCleanUpTemplates, "false", StringComparison.OrdinalIgnoreCase))
{
DeleteOutputDirectory();
}
}
public void DeleteOutputDirectory()
{
const int NumAttempts = 10;
for (var numAttemptsRemaining = NumAttempts; numAttemptsRemaining > 0; numAttemptsRemaining--)
{
try
{
Directory.Delete(TemplateOutputDir, true);
return;
}
catch (Exception ex)
{
if (numAttemptsRemaining > 1)
{
DiagnosticsMessageSink.OnMessage(new DiagnosticMessage($"Failed to delete directory {TemplateOutputDir} because of error {ex.Message}. Will try again {numAttemptsRemaining - 1} more time(s)."));
Thread.Sleep(3000);
}
else
{
DiagnosticsMessageSink.OnMessage(new DiagnosticMessage($"Giving up trying to delete directory {TemplateOutputDir} after {NumAttempts} attempts. Most recent error was: {ex.StackTrace}"));
}
}
}
}
private sealed class OrderedLock
{
private bool _nodeLockTaken;
private bool _dotNetLockTaken;
public OrderedLock(ProcessLock nodeLock, ProcessLock dotnetLock)
{
NodeLock = nodeLock;
DotnetLock = dotnetLock;
}
public ProcessLock NodeLock { get; }
public ProcessLock DotnetLock { get; }
public async Task WaitAsync()
{
if (NodeLock == null)
{
await DotnetLock.WaitAsync();
_dotNetLockTaken = true;
return;
}
try
{
// We want to take the NPM lock first as is going to be the busiest one, and we want other threads to be
// able to run dotnet new while we are waiting for another thread to finish running NPM.
await NodeLock.WaitAsync();
_nodeLockTaken = true;
await DotnetLock.WaitAsync();
_dotNetLockTaken = true;
}
catch
{
if (_nodeLockTaken)
{
NodeLock.Release();
_nodeLockTaken = false;
}
throw;
}
}
public void Release()
{
try
{
if (_dotNetLockTaken)
{
DotnetLock.Release();
_dotNetLockTaken = false;
}
}
finally
{
if (_nodeLockTaken)
{
NodeLock.Release();
_nodeLockTaken = false;
}
}
}
}
private void CaptureBinLogOnFailure(ProcessEx result)
{
if (result.ExitCode != 0 && !string.IsNullOrEmpty(ArtifactsLogDir))
{
var sourceFile = Path.Combine(TemplateOutputDir, "msbuild.binlog");
Assert.True(File.Exists(sourceFile), $"Log for '{ProjectName}' not found in '{sourceFile}'. Execution output: {result.Output}");
var destination = Path.Combine(ArtifactsLogDir, ProjectName + ".binlog");
File.Move(sourceFile, destination, overwrite: true); // binlog will exist on retries
}
}
public override string ToString() => $"{ProjectName}: {TemplateOutputDir}";
private static string GetAssemblyMetadata(string key)
{
var attribute = typeof(Project).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.FirstOrDefault(a => a.Key == key);
if (attribute is null)
{
throw new ArgumentException($"AssemblyMetadataAttribute with key {key} was not found.");
}
return attribute.Value;
}
}