StaticWebAssetsCrossTargetingTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Licensed to the .NET Foundation under one or more agreements.
  4. // The .NET Foundation licenses this file to you under the MIT license.
  5. using Microsoft.AspNetCore.StaticWebAssets.Tasks;
  6. namespace Microsoft.NET.Sdk.Razor.Tests
  7. {
  8. public class StaticWebAssetsCrossTargetingTests : AspNetSdkBaselineTest
  9. {
  10. public StaticWebAssetsCrossTargetingTests(ITestOutputHelper log) : base(log, GenerateBaselines) { }
  11. // Build Standalone project
  12. [Fact]
  13. public void Build_CrosstargetingTests_CanIncludeBrowserAssets()
  14. {
  15. var expectedManifest = LoadBuildManifest();
  16. var testAsset = "RazorComponentAppMultitarget";
  17. ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
  18. ProjectDirectory.WithProjectChanges(d =>
  19. {
  20. d.Root.Element("PropertyGroup").Add(
  21. XElement.Parse("""<StaticWebAssetBasePath>/</StaticWebAssetBasePath>"""));
  22. d.Root.LastNode.AddBeforeSelf(
  23. XElement.Parse("""
  24. <ItemGroup>
  25. <StaticWebAssetsEmbeddedConfiguration
  26. Include="$(TargetFramework)"
  27. Condition="'$([MSBuild]::GetTargetPlatformIdentifier($(TargetFramework)))' == '' And $([MSBuild]::VersionGreaterThanOrEquals(8.0, $([MSBuild]::GetTargetFrameworkVersion($(TargetFramework)))))">
  28. <Platform>browser</Platform>
  29. </StaticWebAssetsEmbeddedConfiguration>
  30. </ItemGroup>
  31. """));
  32. });
  33. var wwwroot = Directory.CreateDirectory(Path.Combine(ProjectDirectory.TestRoot, "wwwroot"));
  34. File.WriteAllText(Path.Combine(wwwroot.FullName, "test.js"), "console.log('hello')");
  35. var build = new BuildCommand(ProjectDirectory);
  36. build.WithWorkingDirectory(ProjectDirectory.TestRoot);
  37. build.Execute("/bl").Should().Pass();
  38. var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
  39. var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
  40. // GenerateStaticWebAssetsManifest should generate the manifest file.
  41. var path = Path.Combine(intermediateOutputPath, "staticwebassets.build.json");
  42. new FileInfo(path).Should().Exist();
  43. var manifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path));
  44. AssertManifest(manifest, expectedManifest);
  45. AssertBuildAssets(manifest, outputPath, intermediateOutputPath);
  46. // GenerateStaticWebAssetsManifest should copy the file to the output folder.
  47. var finalPath = Path.Combine(outputPath, "RazorComponentAppMultitarget.staticwebassets.runtime.json");
  48. new FileInfo(finalPath).Should().Exist();
  49. }
  50. [Fact]
  51. public void Publish_CrosstargetingTests_CanIncludeBrowserAssets()
  52. {
  53. var testAsset = "RazorComponentAppMultitarget";
  54. ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
  55. ProjectDirectory.WithProjectChanges(d =>
  56. {
  57. d.Root.Element("PropertyGroup").Add(
  58. XElement.Parse("""<StaticWebAssetBasePath>/</StaticWebAssetBasePath>"""));
  59. d.Root.LastNode.AddBeforeSelf(
  60. XElement.Parse("""
  61. <ItemGroup>
  62. <StaticWebAssetsEmbeddedConfiguration
  63. Include="$(TargetFramework)"
  64. Condition="'$([MSBuild]::GetTargetPlatformIdentifier($(TargetFramework)))' == '' And $([MSBuild]::VersionGreaterThanOrEquals(8.0, $([MSBuild]::GetTargetFrameworkVersion($(TargetFramework)))))">
  65. <Platform>browser</Platform>
  66. </StaticWebAssetsEmbeddedConfiguration>
  67. </ItemGroup>
  68. """));
  69. });
  70. var wwwroot = Directory.CreateDirectory(Path.Combine(ProjectDirectory.TestRoot, "wwwroot"));
  71. File.WriteAllText(Path.Combine(wwwroot.FullName, "test.js"), "console.log('hello')");
  72. var restore = new RestoreCommand(ProjectDirectory);
  73. restore.WithWorkingDirectory(ProjectDirectory.TestRoot);
  74. restore.Execute().Should().Pass();
  75. var publish = new PublishCommand(ProjectDirectory);
  76. publish.WithWorkingDirectory(ProjectDirectory.TestRoot);
  77. publish.ExecuteWithoutRestore("/bl", "/p:TargetFramework=net9.0").Should().Pass();
  78. var publishPath = publish.GetOutputDirectory(DefaultTfm).ToString();
  79. var intermediateOutputPath = publish.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
  80. // GenerateStaticWebAssetsManifest should generate the manifest file.
  81. var path = Path.Combine(intermediateOutputPath, "staticwebassets.publish.json");
  82. new FileInfo(path).Should().Exist();
  83. var manifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path));
  84. AssertManifest(manifest, LoadPublishManifest());
  85. AssertPublishAssets(
  86. manifest,
  87. publishPath,
  88. intermediateOutputPath);
  89. }
  90. }
  91. }