|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.StaticWebAssets.Tasks; |
| 5 | +using Microsoft.Build.Framework; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +namespace Microsoft.NET.Sdk.Razor.Tests; |
| 9 | + |
| 10 | +public class DiscoverPrecompressedAssetsTest |
| 11 | +{ |
| 12 | + public string ItemSpec { get; } |
| 13 | + |
| 14 | + public string OriginalItemSpec { get; } |
| 15 | + |
| 16 | + public string OutputBasePath { get; } |
| 17 | + |
| 18 | + public DiscoverPrecompressedAssetsTest() |
| 19 | + { |
| 20 | + OutputBasePath = Path.Combine(TestContext.Current.TestExecutionDirectory, nameof(ResolveCompressedAssetsTest)); |
| 21 | + ItemSpec = Path.Combine(OutputBasePath, Guid.NewGuid().ToString("N") + ".tmp"); |
| 22 | + OriginalItemSpec = Path.Combine(OutputBasePath, Guid.NewGuid().ToString("N") + ".tmp"); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void DiscoversPrecompressedAssetsCorrectly() |
| 27 | + { |
| 28 | + var errorMessages = new List<string>(); |
| 29 | + var buildEngine = new Mock<IBuildEngine>(); |
| 30 | + buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())) |
| 31 | + .Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message)); |
| 32 | + |
| 33 | + var uncompressedCandidate = new StaticWebAsset |
| 34 | + { |
| 35 | + Identity = Path.Combine(Environment.CurrentDirectory, "wwwroot", "js", "site.js"), |
| 36 | + RelativePath = "js/site#[.{fingerprint}]?.js", |
| 37 | + BasePath = "_content/Test", |
| 38 | + AssetMode = StaticWebAsset.AssetModes.All, |
| 39 | + AssetKind = StaticWebAsset.AssetKinds.All, |
| 40 | + AssetMergeSource = string.Empty, |
| 41 | + SourceId = "Test", |
| 42 | + CopyToOutputDirectory = StaticWebAsset.AssetCopyOptions.Never, |
| 43 | + Fingerprint = "uncompressed", |
| 44 | + RelatedAsset = string.Empty, |
| 45 | + ContentRoot = Path.Combine(Environment.CurrentDirectory,"wwwroot"), |
| 46 | + SourceType = StaticWebAsset.SourceTypes.Discovered, |
| 47 | + Integrity = "uncompressed-integrity", |
| 48 | + AssetRole = StaticWebAsset.AssetRoles.Primary, |
| 49 | + AssetMergeBehavior = string.Empty, |
| 50 | + AssetTraitValue = string.Empty, |
| 51 | + AssetTraitName = string.Empty, |
| 52 | + OriginalItemSpec = Path.Combine("wwwroot", "js", "site.js"), |
| 53 | + CopyToPublishDirectory = StaticWebAsset.AssetCopyOptions.PreserveNewest |
| 54 | + }; |
| 55 | + |
| 56 | + var compressedCandidate = new StaticWebAsset |
| 57 | + { |
| 58 | + Identity = Path.Combine(Environment.CurrentDirectory, "wwwroot", "js", "site.js.gz"), |
| 59 | + RelativePath = "js/site.js#[.{fingerprint}]?.gz", |
| 60 | + BasePath = "_content/Test", |
| 61 | + AssetMode = StaticWebAsset.AssetModes.All, |
| 62 | + AssetKind = StaticWebAsset.AssetKinds.All, |
| 63 | + AssetMergeSource = string.Empty, |
| 64 | + SourceId = "Test", |
| 65 | + CopyToOutputDirectory = StaticWebAsset.AssetCopyOptions.Never, |
| 66 | + Fingerprint = "compressed", |
| 67 | + RelatedAsset = string.Empty, |
| 68 | + ContentRoot = Path.Combine(Environment.CurrentDirectory, "wwwroot"), |
| 69 | + SourceType = StaticWebAsset.SourceTypes.Discovered, |
| 70 | + Integrity = "compressed-integrity", |
| 71 | + AssetRole = StaticWebAsset.AssetRoles.Primary, |
| 72 | + AssetMergeBehavior = string.Empty, |
| 73 | + AssetTraitValue = string.Empty, |
| 74 | + AssetTraitName = string.Empty, |
| 75 | + OriginalItemSpec = Path.Combine("wwwroot", "js", "site.js.gz"), |
| 76 | + CopyToPublishDirectory = StaticWebAsset.AssetCopyOptions.PreserveNewest |
| 77 | + }; |
| 78 | + |
| 79 | + var task = new DiscoverPrecompressedAssets |
| 80 | + { |
| 81 | + CandidateAssets = [uncompressedCandidate.ToTaskItem(), compressedCandidate.ToTaskItem()], |
| 82 | + BuildEngine = buildEngine.Object |
| 83 | + }; |
| 84 | + |
| 85 | + var result = task.Execute(); |
| 86 | + |
| 87 | + result.Should().BeTrue(); |
| 88 | + task.DiscoveredCompressedAssets.Should().ContainSingle(); |
| 89 | + var asset = task.DiscoveredCompressedAssets[0]; |
| 90 | + asset.ItemSpec.Should().Be(compressedCandidate.Identity); |
| 91 | + asset.GetMetadata("RelatedAsset").Should().Be(uncompressedCandidate.Identity); |
| 92 | + asset.GetMetadata("OriginalItemSpec").Should().Be(uncompressedCandidate.Identity); |
| 93 | + asset.GetMetadata("RelativePath").Should().Be("js/site#[.{fingerprint=uncompressed}]?.js.gz"); |
| 94 | + asset.GetMetadata("AssetRole").Should().Be("Alternative"); |
| 95 | + asset.GetMetadata("AssetTraitName").Should().Be("Content-Encoding"); |
| 96 | + asset.GetMetadata("AssetTraitValue").Should().Be("gzip"); |
| 97 | + asset.GetMetadata("Fingerprint").Should().Be("compressed"); |
| 98 | + asset.GetMetadata("Integrity").Should().Be("compressed-integrity"); |
| 99 | + asset.GetMetadata("CopyToPublishDirectory").Should().Be("PreserveNewest"); |
| 100 | + asset.GetMetadata("CopyToOutputDirectory").Should().Be("Never"); |
| 101 | + asset.GetMetadata("AssetMergeSource").Should().Be(string.Empty); |
| 102 | + asset.GetMetadata("AssetMergeBehavior").Should().Be(string.Empty); |
| 103 | + asset.GetMetadata("AssetKind").Should().Be("All"); |
| 104 | + asset.GetMetadata("AssetMode").Should().Be("All"); |
| 105 | + asset.GetMetadata("SourceId").Should().Be("Test"); |
| 106 | + asset.GetMetadata("SourceType").Should().Be("Discovered"); |
| 107 | + asset.GetMetadata("ContentRoot").Should().Be(Path.Combine(Environment.CurrentDirectory, $"wwwroot{Path.DirectorySeparatorChar}")); |
| 108 | + } |
| 109 | +} |
0 commit comments