Skip to content

Commit 57b5a97

Browse files
authored
[release/6.0.1xx] Backport Add Blazor SWA Baseline Mismatched Assets Logging (#28894)
* Add Blazor SWA Baseline Mismatched Assets Logging (#27542) * Add Blazor AssetDifferencesDetails * Update AspNetSdkBaselineTest.cs For https://github.com/dotnet/sdk/issues/27346 * Spacing * Create blazor-swa-baseline-generation.md * PR Feedback * Remove instructions add script reference * Property level comparison * Added instructions * Update AspNetSdkBaselineTest.cs * Update AspNetSdkBaselineTest.cs (cherry picked from commit bacc17c) * Create update-test-baselines.ps1
1 parent bb0e04c commit 57b5a97

File tree

2 files changed

+153
-2
lines changed

2 files changed

+153
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$RepoRoot= Resolve-Path "$PSScriptRoot/../.."
2+
3+
$TestProjects = "Microsoft.NET.Sdk.Razor.Tests", "Microsoft.NET.Sdk.BlazorWebAssembly.Tests" |
4+
ForEach-Object { Join-Path -Path "$RepoRoot/src/Tests/" -ChildPath $_ };
5+
6+
$TestProjects | ForEach-Object { dotnet test --no-build -l "console;verbosity=normal" $_ -e ASPNETCORE_TEST_BASELINES=true --filter AspNetCore=BaselineTest }

src/Tests/Microsoft.NET.Sdk.Razor.Tests/AspNetSdkBaselineTest.cs

Lines changed: 147 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ namespace Microsoft.NET.Sdk.Razor.Tests
2121
public class AspNetSdkBaselineTest : AspNetSdkTest
2222
{
2323
private static readonly JsonSerializerOptions BaselineSerializationOptions = new() { WriteIndented = true };
24+
private static readonly string BaselineGenerationInstructions =
25+
@"If the difference in baselines is expected, please re-generate the baselines.
26+
Note, baseline generation must be done on a Windows device.
27+
Start by ensuring you're dogfooding the SDK from the current branch (dotnet --version should be '*.0.0-dev').
28+
If you're not on the dogfood sdk, from the root of the repository run:
29+
1. dotnet clean
30+
2. .\restore.cmd
31+
3. .\build.cmd
32+
4. .\eng\dogfood.cmd
33+
34+
Then, using the dogfood SDK run the .\src\RazorSdk\update-test-baselines.ps1 script.";
35+
2436
protected static readonly string DotNetJSHashRegexPattern = "\\.[a-z0-9]{10}\\.js";
2537
protected static readonly string DotNetJSHashTemplate = ".[[hash]].js";
2638

@@ -352,12 +364,144 @@ internal void AssertManifest(
352364
manifest.BasePath.Should().Be(expected.BasePath);
353365
manifest.Mode.Should().Be(expected.Mode);
354366
manifest.ManifestType.Should().Be(expected.ManifestType);
367+
355368
manifest.ReferencedProjectsConfiguration.OrderBy(cm => cm.Identity)
356369
.Should()
357370
.BeEquivalentTo(expected.ReferencedProjectsConfiguration.OrderBy(cm => cm.Identity));
358371
manifest.DiscoveryPatterns.OrderBy(dp => dp.Name).ShouldBeEquivalentTo(expected.DiscoveryPatterns.OrderBy(dp => dp.Name));
359-
manifest.Assets.OrderBy(a => a.BasePath).ThenBy(a => a.RelativePath).ThenBy(a => a.AssetKind)
360-
.ShouldBeEquivalentTo(expected.Assets.OrderBy(a => a.BasePath).ThenBy(a => a.RelativePath).ThenBy(a => a.AssetKind));
372+
373+
var manifestAssets = manifest.Assets.OrderBy(a => a.BasePath).ThenBy(a => a.RelativePath).ThenBy(a => a.AssetKind);
374+
var expectedAssets = expected.Assets.OrderBy(a => a.BasePath).ThenBy(a => a.RelativePath).ThenBy(a => a.AssetKind);
375+
376+
// If there's a mismatch in the number of assets, just print the strict difference in the asset `Identity`
377+
if (manifestAssets.Count() != expectedAssets.Count())
378+
{
379+
ThrowAssetCountMismatchError(manifestAssets, expectedAssets);
380+
}
381+
382+
// Otherwise, do a property level comparison of all assets
383+
var manifestAssetsEnumerator = manifestAssets.GetEnumerator();
384+
var expectedAssetsEnumerator = expectedAssets.GetEnumerator();
385+
386+
var differences = new List<string>();
387+
388+
do
389+
{
390+
var manifestAsset = manifestAssetsEnumerator.Current;
391+
var expectedAsset = expectedAssetsEnumerator.Current;
392+
393+
if (manifestAsset is null && expectedAsset is null)
394+
{
395+
continue;
396+
}
397+
398+
var assetDifferences = new List<string>();
399+
400+
if (manifestAsset.Identity != expectedAsset.Identity)
401+
{
402+
assetDifferences.Add($"Expected manifest Identity of {expectedAsset.Identity} but found {manifestAsset.Identity}.");
403+
}
404+
if (manifestAsset.SourceType != expectedAsset.SourceType)
405+
{
406+
assetDifferences.Add($"Expected manifest SourceType of {expectedAsset.SourceType} but found {manifestAsset.SourceType}.");
407+
}
408+
if (manifestAsset.SourceId != expectedAsset.SourceId)
409+
{
410+
assetDifferences.Add($"Expected manifest SourceId of {expectedAsset.SourceId} but found {manifestAsset.SourceId}.");
411+
}
412+
if (manifestAsset.ContentRoot != expectedAsset.ContentRoot)
413+
{
414+
assetDifferences.Add($"Expected manifest ContentRoot of {expectedAsset.ContentRoot} but found {manifestAsset.ContentRoot}.");
415+
}
416+
if (manifestAsset.BasePath != expectedAsset.BasePath)
417+
{
418+
assetDifferences.Add($"Expected manifest BasePath of {expectedAsset.BasePath} but found {manifestAsset.BasePath}.");
419+
}
420+
if (manifestAsset.RelativePath != expectedAsset.RelativePath)
421+
{
422+
assetDifferences.Add($"Expected manifest RelativePath of {expectedAsset.RelativePath} but found {manifestAsset.RelativePath}.");
423+
}
424+
if (manifestAsset.AssetKind != expectedAsset.AssetKind)
425+
{
426+
assetDifferences.Add($"Expected manifest AssetKind of {expectedAsset.AssetKind} but found {manifestAsset.AssetKind}.");
427+
}
428+
if (manifestAsset.AssetMode != expectedAsset.AssetMode)
429+
{
430+
assetDifferences.Add($"Expected manifest AssetMode of {expectedAsset.AssetMode} but found {manifestAsset.AssetMode}.");
431+
}
432+
if (manifestAsset.AssetRole != expectedAsset.AssetRole)
433+
{
434+
assetDifferences.Add($"Expected manifest AssetRole of {expectedAsset.AssetRole} but found {manifestAsset.AssetRole}.");
435+
}
436+
if (manifestAsset.RelatedAsset != expectedAsset.RelatedAsset)
437+
{
438+
assetDifferences.Add($"Expected manifest RelatedAsset of {expectedAsset.RelatedAsset} but found {manifestAsset.RelatedAsset}.");
439+
}
440+
if (manifestAsset.AssetTraitName != expectedAsset.AssetTraitName)
441+
{
442+
assetDifferences.Add($"Expected manifest AssetTraitName of {expectedAsset.AssetTraitName} but found {manifestAsset.AssetTraitName}.");
443+
}
444+
if (manifestAsset.AssetTraitValue != expectedAsset.AssetTraitValue)
445+
{
446+
assetDifferences.Add($"Expected manifest AssetTraitValue of {expectedAsset.AssetTraitValue} but found {manifestAsset.AssetTraitValue}.");
447+
}
448+
if (manifestAsset.CopyToOutputDirectory != expectedAsset.CopyToOutputDirectory)
449+
{
450+
assetDifferences.Add($"Expected manifest CopyToOutputDirectory of {expectedAsset.CopyToOutputDirectory} but found {manifestAsset.CopyToOutputDirectory}.");
451+
}
452+
if (manifestAsset.CopyToPublishDirectory != expectedAsset.CopyToPublishDirectory)
453+
{
454+
assetDifferences.Add($"Expected manifest CopyToPublishDirectory of {expectedAsset.CopyToPublishDirectory} but found {manifestAsset.CopyToPublishDirectory}.");
455+
}
456+
if (manifestAsset.OriginalItemSpec != expectedAsset.OriginalItemSpec)
457+
{
458+
assetDifferences.Add($"Expected manifest OriginalItemSpec of {expectedAsset.OriginalItemSpec} but found {manifestAsset.OriginalItemSpec}.");
459+
}
460+
461+
if (assetDifferences.Any())
462+
{
463+
differences.Add(@$"
464+
==================================================
465+
466+
For {expectedAsset.Identity}:
467+
468+
{string.Join(Environment.NewLine, assetDifferences)}
469+
470+
==================================================");
471+
}
472+
473+
} while (manifestAssetsEnumerator.MoveNext() && expectedAssetsEnumerator.MoveNext());
474+
475+
differences.Should().BeEmpty(
476+
@$" the generated manifest should match the expected baseline.
477+
478+
{BaselineGenerationInstructions}
479+
480+
");
481+
482+
static void ThrowAssetCountMismatchError(IEnumerable<StaticWebAsset> manifestAssets, IEnumerable<StaticWebAsset> expectedAssets)
483+
{
484+
var missingAssets = expectedAssets.Except(manifestAssets);
485+
var unexpectedAssets = manifestAssets.Except(expectedAssets);
486+
487+
var differences = new List<string>();
488+
489+
if (missingAssets.Any())
490+
{
491+
differences.Add($@"The following expected assets weren't found in the manifest:
492+
{string.Join($"{Environment.NewLine}\t", missingAssets.Select(a => a.Identity))}");
493+
}
494+
495+
if (unexpectedAssets.Any())
496+
{
497+
differences.Add($@"The following additional unexpected assets were found in the manifest:
498+
{string.Join($"{Environment.NewLine}\t", unexpectedAssets.Select(a => a.Identity))}");
499+
}
500+
501+
throw new Exception($@"{string.Join(Environment.NewLine, differences)}
502+
503+
{BaselineGenerationInstructions}");
504+
}
361505
}
362506
else
363507
{
@@ -528,3 +672,4 @@ string TemplatizeRestorePath(string restorePath, string property)
528672
}
529673
}
530674
}
675+

0 commit comments

Comments
 (0)