diff options
author | Javier Calvarro Nelson <jacalvar@microsoft.com> | 2020-06-11 23:12:53 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-11 23:12:53 +0300 |
commit | 9d7018544adf4602b94a07b40b18f7366641973b (patch) | |
tree | c1f336c619e5ac178800da6ba5bc07eed4753e1c /src/Components | |
parent | ce533485f0ea6eab78893ca9a889b8f29c04658f (diff) |
[Blazor] Fix manifest integrity computation (#22771)
* [Blazor] Fix manifest integrity computation
* Add a couple of tests to validate incrementalism
Diffstat (limited to 'src/Components')
-rw-r--r-- | src/Components/WebAssembly/Build/src/targets/ServiceWorkerAssetsManifest.targets | 2 | ||||
-rw-r--r-- | src/Components/WebAssembly/Build/test/BuildIntegrationTests/PwaManifestTests.cs | 73 |
2 files changed, 74 insertions, 1 deletions
diff --git a/src/Components/WebAssembly/Build/src/targets/ServiceWorkerAssetsManifest.targets b/src/Components/WebAssembly/Build/src/targets/ServiceWorkerAssetsManifest.targets index 3a72b4e62a..c17edf60e0 100644 --- a/src/Components/WebAssembly/Build/src/targets/ServiceWorkerAssetsManifest.targets +++ b/src/Components/WebAssembly/Build/src/targets/ServiceWorkerAssetsManifest.targets @@ -163,7 +163,7 @@ <WriteLinesToFile File="$(_CombinedHashIntermediatePath)" - Lines="@(_ServiceWorkerAssetsManifestItemWithHash->'%(FileHash)')" + Lines="@(_ServiceWorkerAssetsManifestItemWithHash->'%(Integrity)')" WriteOnlyWhenDifferent="true" Overwrite="true" /> diff --git a/src/Components/WebAssembly/Build/test/BuildIntegrationTests/PwaManifestTests.cs b/src/Components/WebAssembly/Build/test/BuildIntegrationTests/PwaManifestTests.cs index a268e8f8ea..464d361e70 100644 --- a/src/Components/WebAssembly/Build/test/BuildIntegrationTests/PwaManifestTests.cs +++ b/src/Components/WebAssembly/Build/test/BuildIntegrationTests/PwaManifestTests.cs @@ -1,6 +1,7 @@ using System.IO; using System.Linq; using System.Text.Json; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Xunit; @@ -41,5 +42,77 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build var entries = assets.EnumerateArray().Select(e => e.GetProperty("url").GetString()).OrderBy(e => e).ToArray(); Assert.All(entries, e => expectedExtensions.Contains(Path.GetExtension(e))); } + + [Fact] + public async Task Publish_UpdatesServiceWorkerVersionHash_WhenSourcesChange() + { + // Arrange + using var project = ProjectDirectory.Create("standalone", additionalProjects: new[] { "razorclasslibrary" }); + var result = await MSBuildProcessManager.DotnetMSBuild(project, "Publish", args: "/bl:initial.binlog /p:ServiceWorkerAssetsManifest=service-worker-assets.js"); + + Assert.BuildPassed(result); + + var publishOutputDirectory = project.PublishOutputDirectory; + + var serviceWorkerFile = Assert.FileExists(result, publishOutputDirectory, "wwwroot", "serviceworkers", "my-service-worker.js"); + var version = File.ReadAllLines(serviceWorkerFile).Last(); + var match = Regex.Match(version, "\\/\\* Manifest version: (.{8}) \\*\\/"); + Assert.True(match.Success); + Assert.Equal(2, match.Groups.Count); + Assert.NotNull(match.Groups[1].Value); + var capture = match.Groups[1].Value; + + // Act + var cssFile = Path.Combine(project.DirectoryPath, "LinkToWebRoot", "css", "app.css"); + File.WriteAllText(cssFile, ".updated { }"); + + // Assert + var updatedResult = await MSBuildProcessManager.DotnetMSBuild(project, "Publish", args: "/bl:updated.binlog /p:ServiceWorkerAssetsManifest=service-worker-assets.js"); + + Assert.BuildPassed(result); + + var updatedVersion = File.ReadAllLines(serviceWorkerFile).Last(); + var updatedMatch = Regex.Match(updatedVersion, "\\/\\* Manifest version: (.{8}) \\*\\/"); + Assert.True(updatedMatch.Success); + Assert.Equal(2, updatedMatch.Groups.Count); + Assert.NotNull(updatedMatch.Groups[1].Value); + var updatedCapture = updatedMatch.Groups[1].Value; + + Assert.NotEqual(capture, updatedCapture); + } + + [Fact] + public async Task Publish_DeterministicAcrossBuilds_WhenNoSourcesChange() + { + // Arrange + using var project = ProjectDirectory.Create("standalone", additionalProjects: new[] { "razorclasslibrary" }); + var result = await MSBuildProcessManager.DotnetMSBuild(project, "Publish", args: "/bl:initial.binlog /p:ServiceWorkerAssetsManifest=service-worker-assets.js"); + + Assert.BuildPassed(result); + + var publishOutputDirectory = project.PublishOutputDirectory; + + var serviceWorkerFile = Assert.FileExists(result, publishOutputDirectory, "wwwroot", "serviceworkers", "my-service-worker.js"); + var version = File.ReadAllLines(serviceWorkerFile).Last(); + var match = Regex.Match(version, "\\/\\* Manifest version: (.{8}) \\*\\/"); + Assert.True(match.Success); + Assert.Equal(2, match.Groups.Count); + Assert.NotNull(match.Groups[1].Value); + var capture = match.Groups[1].Value; + + // Act && Assert + var updatedResult = await MSBuildProcessManager.DotnetMSBuild(project, "Publish", args: "/bl:updated.binlog /p:ServiceWorkerAssetsManifest=service-worker-assets.js"); + + Assert.BuildPassed(result); + + var updatedVersion = File.ReadAllLines(serviceWorkerFile).Last(); + var updatedMatch = Regex.Match(updatedVersion, "\\/\\* Manifest version: (.{8}) \\*\\/"); + Assert.True(updatedMatch.Success); + Assert.Equal(2, updatedMatch.Groups.Count); + Assert.NotNull(updatedMatch.Groups[1].Value); + var updatedCapture = updatedMatch.Groups[1].Value; + + Assert.Equal(capture, updatedCapture); + } } } |