Repository navigation
Expand file tree
/
Copy pathAspNetSdkTest.cs
More file actions
206 lines (179 loc) · 8.36 KB
/
Copy pathAspNetSdkTest.cs
File metadata and controls
206 lines (179 loc) · 8.36 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
// 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.CompilerServices;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.NET.TestFramework;
[TestCategory("AspNetCore")]
public abstract class AspNetSdkTest : SdkTest
{
public readonly string? DefaultTfm;
public const string WasmBootConfigFileName = "dotnet.js";
#if !GENERATE_MSBUILD_LOGS
public static bool GenerateMSbuildLogs = true;
#else
public static bool GenerateMSbuildLogs = bool.TryParse(Environment.GetEnvironmentVariable("ASPNETCORE_GENERATE_BINLOG"), out var result) && result || Debugger.IsAttached;
#endif
private bool _generateMSbuildLogs = GenerateMSbuildLogs;
protected AspNetSdkTest()
{
var assembly = GetType().Assembly;
var testAssemblyMetadata = assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
DefaultTfm = testAssemblyMetadata.SingleOrDefault(a => a.Key == "AspNetTestTfm")?.Value;
}
public TestAsset CreateAspNetSdkTestAsset(
string testAsset,
[CallerMemberName] string callerName = "",
string subdirectory = "",
string? overrideTfm = null,
string? identifier = null,
[CallerFilePath] string? callerFilePath = null)
{
var projectDirectory = TestAssetsManager
.CopyTestAsset(testAsset, callingMethod: callerName, callerFilePath: callerFilePath, testAssetSubdirectory: subdirectory, identifier: identifier)
.WithSource()
.WithProjectChanges(project =>
{
var targetFramework = project.Descendants()
.SingleOrDefault(e => e.Name.LocalName == "TargetFramework");
if (targetFramework?.Value == "$(AspNetTestTfm)")
{
targetFramework.Value = overrideTfm ?? DefaultTfm ?? string.Empty;
targetFramework.AddAfterSelf(new XElement("StaticWebAssetsFingerprintContent", "false"));
targetFramework.AddAfterSelf(new XElement("AttachWeakETagToCompressedAssetsDuringDevelopment", "false"));
if (DefaultTfm is not null)
{
targetFramework.AddAfterSelf(new XElement("UseMonoRuntime",
new XAttribute("Condition", $"'$(TargetFramework)' == '{DefaultTfm}'"), "false"));
}
}
var targetFrameworks = project.Descendants()
.SingleOrDefault(e => e.Name.LocalName == "TargetFrameworks");
if (targetFrameworks != null)
{
targetFrameworks.Value = targetFrameworks.Value.Replace("$(AspNetTestTfm)", overrideTfm ?? DefaultTfm ?? string.Empty);
targetFrameworks.AddAfterSelf(new XElement("StaticWebAssetsFingerprintContent", "false"));
targetFrameworks.AddAfterSelf(new XElement("AttachWeakETagToCompressedAssetsDuringDevelopment", "false"));
if (DefaultTfm is not null)
{
targetFrameworks.AddAfterSelf(new XElement("UseMonoRuntime",
new XAttribute("Condition", $"'$(TargetFramework)' == '{DefaultTfm}'"), "false"));
}
}
});
foreach (string assetPath in Directory.EnumerateFiles(Path.Combine(TestAssetsManager.TestAssetsRoot, "WasmOverride")))
File.Copy(assetPath, Path.Combine(projectDirectory.Path, Path.GetFileName(assetPath)));
return projectDirectory;
}
public TestAsset CreateMultitargetAspNetSdkTestAsset(
string testAsset,
[CallerMemberName] string callerName = "",
string subdirectory = "",
string? overrideTfm = null,
string? identifier = null,
[CallerFilePath] string? callerFilePath = null)
{
var projectDirectory = TestAssetsManager
.CopyTestAsset(testAsset, callingMethod: callerName, callerFilePath: callerFilePath, testAssetSubdirectory: subdirectory, identifier: identifier)
.WithSource()
.WithProjectChanges(project =>
{
var targetFramework = project.Descendants()
.Single(e => e.Name.LocalName == "TargetFrameworks");
targetFramework.Value = targetFramework.Value.Replace("$(AspNetTestTfm)", overrideTfm ?? DefaultTfm ?? string.Empty);
if (DefaultTfm is not null)
{
targetFramework.AddAfterSelf(new XElement("UseMonoRuntime",
new XAttribute("Condition", $"'$(TargetFramework)' == '{DefaultTfm}'"), "false"));
}
});
return projectDirectory;
}
protected virtual RestoreCommand CreateRestoreCommand(TestAsset asset, string? relativePathToProject = null)
{
var restore = new RestoreCommand(asset, relativePathToProject);
restore.WithWorkingDirectory(asset.TestRoot);
ApplyDefaults(restore);
return restore;
}
protected virtual BuildCommand CreateBuildCommand(TestAsset asset, string? relativePathToProject = null)
{
var build = new BuildCommand(asset, relativePathToProject);
build.WithWorkingDirectory(asset.TestRoot);
ApplyDefaults(build);
return build;
}
protected virtual RebuildCommand CreateRebuildCommand(TestAsset asset, string? relativePathToProject = null)
{
var rebuild = new RebuildCommand(Log, asset.Path, relativePathToProject);
rebuild.WithWorkingDirectory(asset.TestRoot);
ApplyDefaults(rebuild);
return rebuild;
}
protected virtual PackCommand CreatePackCommand(TestAsset asset, string? relativePathToProject = null)
{
var pack = new PackCommand(asset, relativePathToProject);
pack.WithWorkingDirectory(asset.TestRoot);
ApplyDefaults(pack);
return pack;
}
protected virtual PublishCommand CreatePublishCommand(TestAsset asset, string? relativePathToProject = null)
{
var publish = new PublishCommand(asset, relativePathToProject);
publish.WithWorkingDirectory(asset.TestRoot);
ApplyDefaults(publish);
return publish;
}
protected virtual CommandResult ExecuteCommand(TestCommand command, params string[] arguments)
{
ValidateIndividualArgumentsContainNoSpaces(arguments);
if (_generateMSbuildLogs)
{
var i = 0;
for (i = 0; command.WorkingDirectory is not null && File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { }
var log = $"msbuild{i}.binlog";
return command.Execute([$"/bl:{log}", .. arguments]);
}
else
{
return command.Execute(arguments);
}
}
protected virtual CommandResult ExecuteCommandWithoutRestore(MSBuildCommand command, params string[] arguments)
{
ValidateIndividualArgumentsContainNoSpaces(arguments);
if (_generateMSbuildLogs)
{
var i = 0;
for (i = 0; command.WorkingDirectory is not null && File.Exists(Path.Combine(command.WorkingDirectory, $"msbuild{i}.binlog")) && i < 20; i++) { }
var log = $"msbuild{i}.binlog";
return command.ExecuteWithoutRestore([$"/bl:{log}", .. arguments]);
}
else
{
return command.ExecuteWithoutRestore(arguments);
}
}
private void ValidateIndividualArgumentsContainNoSpaces(string[] arguments)
{
foreach (var argument in arguments)
{
if (argument.Contains(' '))
{
throw new ArgumentException($"Individual arguments should not contain spaces to avoid quoting issues when passing to msbuild, pass them as separate array elements instead. Argument: {argument}");
}
}
}
private void ApplyDefaults(MSBuildCommand command)
{
if (GetNuGetCachePath() is { } cache)
{
command.WithEnvironmentVariable("NUGET_PACKAGES", cache);
command.WithEnvironmentVariable("AspNetNugetIsolationPath", cache);
command.WithEnvironmentVariable("RestorePackagesPath", cache);
}
}
protected virtual string? GetNuGetCachePath() => null;
}