-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AspNetSdkBaselineTest.cs
285 lines (243 loc) · 12.5 KB
/
AspNetSdkBaselineTest.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
// 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.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using FluentAssertions;
using Microsoft.AspNetCore.StaticWebAssets.Tasks;
using Microsoft.NET.TestFramework;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.NET.Sdk.Razor.Tests
{
[Trait("AspNetCore", "BaselineTest")]
public class AspNetSdkBaselineTest : AspNetSdkTest
{
private static readonly JsonSerializerOptions BaselineSerializationOptions = new() { WriteIndented = true };
private readonly StaticWebAssetsBaselineComparer _comparer;
private readonly StaticWebAssetsBaselineFactory _baselineFactory;
private string _baselinesFolder;
#if GENERATE_SWA_BASELINES
public static bool GenerateBaselines = true;
#else
public static bool GenerateBaselines = bool.TryParse(Environment.GetEnvironmentVariable("ASPNETCORE_TEST_BASELINES"), out var result) && result;
#endif
private bool _generateBaselines = GenerateBaselines;
public AspNetSdkBaselineTest(ITestOutputHelper log) : base(log)
{
TestAssembly = Assembly.GetCallingAssembly();
var testAssemblyMetadata = TestAssembly.GetCustomAttributes<AssemblyMetadataAttribute>();
RuntimeVersion = testAssemblyMetadata.SingleOrDefault(a => a.Key == "NetCoreAppRuntimePackageVersion").Value;
DefaultPackageVersion = testAssemblyMetadata.SingleOrDefault(a => a.Key == "DefaultTestBaselinePackageVersion").Value;
_comparer = CreateBaselineComparer();
_baselineFactory = CreateBaselineFactory();
}
public AspNetSdkBaselineTest(ITestOutputHelper log, bool generateBaselines) : this(log)
{
_generateBaselines = generateBaselines;
_comparer = CreateBaselineComparer();
}
public TestAsset ProjectDirectory { get; set; }
public string RuntimeVersion { get; set; }
public string DefaultPackageVersion { get; set; }
public string BaselinesFolder =>
_baselinesFolder ??= ComputeBaselineFolder();
protected Assembly TestAssembly { get; }
protected virtual StaticWebAssetsBaselineComparer CreateBaselineComparer()
{
return StaticWebAssetsBaselineComparer.Instance;
}
private StaticWebAssetsBaselineFactory CreateBaselineFactory()
{
return StaticWebAssetsBaselineFactory.Instance;
}
protected virtual string ComputeBaselineFolder() =>
Path.Combine(TestContext.GetRepoRoot() ?? AppContext.BaseDirectory, "src", "Tests", "Microsoft.NET.Sdk.Razor.Tests", "StaticWebAssetsBaselines");
protected virtual string EmbeddedResourcePrefix => string.Join('.', "Microsoft.NET.Sdk.Razor.Tests", "StaticWebAssetsBaselines");
public StaticWebAssetsManifest LoadBuildManifest(string suffix = "", [CallerMemberName] string name = "")
{
if (_generateBaselines)
{
return default;
}
else
{
using var stream = GetManifestEmbeddedResource(suffix, name, "Build");
var manifest = StaticWebAssetsManifest.FromStream(stream);
return manifest;
}
}
public StaticWebAssetsManifest LoadPublishManifest(string suffix = "", [CallerMemberName] string name = "")
{
if (_generateBaselines)
{
return default;
}
else
{
using var stream = GetManifestEmbeddedResource(suffix, name, "Publish");
var manifest = StaticWebAssetsManifest.FromStream(stream);
return manifest;
}
}
protected void AssertBuildAssets(
StaticWebAssetsManifest manifest,
string outputFolder,
string intermediateOutputPath,
string suffix = "",
[CallerMemberName] string name = "")
{
var fileEnumerationOptions = new EnumerationOptions { RecurseSubdirectories = true };
var wwwRootFolder = Path.Combine(outputFolder, "wwwroot");
var wwwRootFiles = Directory.Exists(wwwRootFolder) ?
Directory.GetFiles(wwwRootFolder, "*", fileEnumerationOptions) :
Array.Empty<string>();
var computedFiles = manifest.Assets
.Where(a => a.SourceType is StaticWebAsset.SourceTypes.Computed &&
a.AssetKind is not StaticWebAsset.AssetKinds.Publish);
// We keep track of assets that need to be copied to the output folder.
// In addition to that, we copy assets that are defined somewhere different
// from their content root folder when the content root does not match the output folder.
// We do this to allow copying things like Publish assets to temporary locations during the
// build process if they are later on going to be transformed.
var copyToOutputDirectoryFiles = manifest.Assets
.Where(a => a.ShouldCopyToOutputDirectory())
.Select(a => Path.GetFullPath(Path.Combine(outputFolder, "wwwroot", a.RelativePath)))
.Concat(manifest.Assets
.Where(a => !a.HasContentRoot(Path.Combine(outputFolder, "wwwroot")) && File.Exists(a.Identity) && !File.Exists(Path.Combine(a.ContentRoot, a.RelativePath)))
.Select(a => Path.GetFullPath(Path.Combine(a.ContentRoot, a.RelativePath))))
.ToArray();
var existingFiles = _baselineFactory.TemplatizeExpectedFiles(
wwwRootFiles
.Concat(computedFiles.Select(a => a.Identity))
.Concat(copyToOutputDirectoryFiles)
.Distinct()
.OrderBy(f => f, StringComparer.Ordinal)
.ToArray(),
TestContext.Current.NuGetCachePath,
ProjectDirectory.TestRoot,
intermediateOutputPath,
outputFolder);
if (!_generateBaselines)
{
var expected = LoadExpectedFilesBaseline(manifest.ManifestType, suffix, name)
.OrderBy(f => f, StringComparer.Ordinal);
existingFiles.Should().BeEquivalentTo(expected);
}
else
{
File.WriteAllText(
GetExpectedFilesPath(suffix, name, manifest.ManifestType),
JsonSerializer.Serialize(existingFiles, BaselineSerializationOptions));
}
}
protected void AssertPublishAssets(
StaticWebAssetsManifest manifest,
string publishFolder,
string intermediateOutputPath,
string suffix = "",
[CallerMemberName] string name = "")
{
var fileEnumerationOptions = new EnumerationOptions { RecurseSubdirectories = true };
string wwwRootFolder = Path.Combine(publishFolder, "wwwroot");
var wwwRootFiles = Directory.Exists(wwwRootFolder) ?
Directory.GetFiles(wwwRootFolder, "*", fileEnumerationOptions)
.Select(f => _baselineFactory.TemplatizeFilePath(f, null, null, intermediateOutputPath, publishFolder)) :
Array.Empty<string>();
// Computed publish assets must exist on disk (we do this check to quickly identify when something is not being
// generated vs when its being copied to the wrong place)
var computedFiles = manifest.Assets
.Where(a => a.SourceType is StaticWebAsset.SourceTypes.Computed &&
a.AssetKind is not StaticWebAsset.AssetKinds.Build);
// For assets that are copied to the publish folder, the path is always based on
// the wwwroot folder, the relative path and the base path for project or package
// assets.
var copyToPublishDirectoryFiles = manifest.Assets
.Where(a => !string.Equals(a.SourceId, manifest.Source, StringComparison.Ordinal) ||
!string.Equals(a.AssetMode, StaticWebAsset.AssetModes.Reference))
.Select(a => Path.Combine(wwwRootFolder, a.ComputeTargetPath("", Path.DirectorySeparatorChar)));
var existingFiles = _baselineFactory.TemplatizeExpectedFiles(
wwwRootFiles
.Concat(computedFiles.Select(a => a.Identity))
.Concat(copyToPublishDirectoryFiles)
.Distinct()
.OrderBy(f => f, StringComparer.Ordinal)
.ToArray(),
TestContext.Current.NuGetCachePath,
ProjectDirectory.TestRoot,
intermediateOutputPath,
publishFolder);
if (!_generateBaselines)
{
var expected = LoadExpectedFilesBaseline(manifest.ManifestType, suffix, name);
existingFiles.Should().BeEquivalentTo(expected);
}
else
{
File.WriteAllText(
GetExpectedFilesPath(suffix, name, manifest.ManifestType),
JsonSerializer.Serialize(existingFiles, BaselineSerializationOptions));
}
}
public string[] LoadExpectedFilesBaseline(
string type,
string suffix,
string name)
{
if (!_generateBaselines)
{
using var filesBaselineStream = GetExpectedFilesEmbeddedResource(suffix, name, type);
return JsonSerializer.Deserialize<string[]>(filesBaselineStream);
}
else
{
return Array.Empty<string>();
}
}
internal void AssertManifest(
StaticWebAssetsManifest manifest,
StaticWebAssetsManifest expected,
string suffix = "",
[CallerMemberName] string name = "")
{
if (!_generateBaselines)
{
// We are going to compare the generated manifest with the current manifest.
// For that, we "templatize" the current manifest to avoid issues with hashes, versions, etc.
_baselineFactory.ToTemplate(
manifest,
ProjectDirectory.Path,
TestContext.Current.NuGetCachePath,
RuntimeVersion,
DefaultTfm,
DefaultPackageVersion);
_comparer.AssertManifest(expected, manifest);
}
else
{
var template = Templatize(manifest, ProjectDirectory.Path, TestContext.Current.NuGetCachePath);
if (!Directory.Exists(Path.Combine(BaselinesFolder)))
{
Directory.CreateDirectory(Path.Combine(BaselinesFolder));
}
File.WriteAllText(GetManifestPath(suffix, name, manifest.ManifestType), template);
}
}
private string GetManifestPath(string suffix, string name, string manifestType)
=> Path.Combine(BaselinesFolder, $"{name}{(!string.IsNullOrEmpty(suffix) ? $"_{suffix}" : "")}.{manifestType}.staticwebassets.json");
private Stream GetManifestEmbeddedResource(string suffix, string name, string manifestType)
=> TestAssembly.GetManifestResourceStream(string.Join('.', EmbeddedResourcePrefix, $"{name}{(!string.IsNullOrEmpty(suffix) ? $"_{suffix}" : "")}.{manifestType}.staticwebassets.json"));
private string GetExpectedFilesPath(string suffix, string name, string manifestType)
=> Path.Combine(BaselinesFolder, $"{name}{(!string.IsNullOrEmpty(suffix) ? $"_{suffix}" : "")}.{manifestType}.files.json");
private Stream GetExpectedFilesEmbeddedResource(string suffix, string name, string manifestType)
=> TestAssembly.GetManifestResourceStream(string.Join('.', EmbeddedResourcePrefix, $"{name}{(!string.IsNullOrEmpty(suffix) ? $"_{suffix}" : "")}.{manifestType}.files.json"));
private string Templatize(StaticWebAssetsManifest manifest, string projectRoot, string restorePath)
{
_baselineFactory.ToTemplate(manifest, projectRoot, restorePath, RuntimeVersion, DefaultTfm, DefaultPackageVersion);
return JsonSerializer.Serialize(manifest, BaselineSerializationOptions);
}
}
}