Skip to content

Commit 2068b9f

Browse files
committed
Add back-compat tests
1 parent 5bb7e6c commit 2068b9f

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed

src/Tests/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/BlazorWasmStaticWebAssetsIntegrationTest.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,103 @@ public void StaticWebAssets_Publish_Hosted_Works()
146146
publishPath,
147147
intermediateOutputPath);
148148
}
149+
150+
[Fact]
151+
public void StaticWebAssets_BackCompatibilityBuild_Hosted_Works()
152+
{
153+
// Arrange
154+
var testAppName = "BlazorHosted";
155+
ProjectDirectory = CreateAspNetSdkTestAsset(testAppName);
156+
157+
ProjectDirectory.WithProjectChanges((project, document) =>
158+
{
159+
if (Path.GetFileNameWithoutExtension(project) == "blazorwasm")
160+
{
161+
document.Descendants("TargetFramework").Single().ReplaceNodes("net5");
162+
}
163+
if (Path.GetFileNameWithoutExtension(project) == "RazorClassLibrary")
164+
{
165+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
166+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
167+
}
168+
if (Path.GetFileNameWithoutExtension(project) == "classlibrarywithsatelliteassemblies")
169+
{
170+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
171+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
172+
}
173+
});
174+
175+
var build = new BuildCommand(ProjectDirectory, "blazorhosted");
176+
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
177+
var buildResult = build.Execute("/bl");
178+
buildResult.Should().Pass();
179+
180+
var outputPath = build.GetOutputDirectory(DefaultTfm).ToString();
181+
var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
182+
183+
// GenerateStaticWebAssetsManifest should generate the manifest file.
184+
var path = Path.Combine(intermediateOutputPath, "StaticWebAssets.build.json");
185+
new FileInfo(path).Should().Exist();
186+
var manifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path));
187+
AssertManifest(manifest, LoadBuildManifest());
188+
189+
// GenerateStaticWebAssetsManifest should copy the file to the output folder.
190+
var finalPath = Path.Combine(outputPath, "blazorhosted.staticwebassets.json");
191+
new FileInfo(finalPath).Should().Exist();
192+
193+
AssertBuildAssets(
194+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path)),
195+
outputPath,
196+
intermediateOutputPath);
197+
}
198+
199+
[Fact]
200+
public void StaticWebAssets_BackCompatibilityPublish_Hosted_Works()
201+
{
202+
// Arrange
203+
var testAppName = "BlazorHosted";
204+
ProjectDirectory = CreateAspNetSdkTestAsset(testAppName);
205+
206+
ProjectDirectory.WithProjectChanges((project, document) =>
207+
{
208+
if (Path.GetFileNameWithoutExtension(project) == "blazorwasm")
209+
{
210+
document.Descendants("TargetFramework").Single().ReplaceNodes("net5");
211+
}
212+
if (Path.GetFileNameWithoutExtension(project) == "RazorClassLibrary")
213+
{
214+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
215+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
216+
}
217+
if (Path.GetFileNameWithoutExtension(project) == "classlibrarywithsatelliteassemblies")
218+
{
219+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
220+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
221+
}
222+
});
223+
224+
// Check that static web assets is correctly configured by setting up a css file to triger css isolation.
225+
// The list of publish files should not include bundle.scp.css and should include blazorwasm.styles.css
226+
File.WriteAllText(Path.Combine(ProjectDirectory.TestRoot, "blazorwasm", "App.razor.css"), "h1 { font-size: 16px; }");
227+
228+
var publish = new PublishCommand(ProjectDirectory, "blazorhosted");
229+
publish.WithWorkingDirectory(ProjectDirectory.TestRoot);
230+
var publishResult = publish.Execute("/bl");
231+
publishResult.Should().Pass();
232+
233+
var publishPath = publish.GetOutputDirectory(DefaultTfm).ToString();
234+
var intermediateOutputPath = publish.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
235+
236+
// GenerateStaticWebAssetsManifest should generate the manifest file.
237+
var path = Path.Combine(intermediateOutputPath, "StaticWebAssets.publish.json");
238+
new FileInfo(path).Should().Exist();
239+
var manifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path));
240+
AssertManifest(manifest, LoadPublishManifest());
241+
242+
AssertPublishAssets(
243+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path)),
244+
publishPath,
245+
intermediateOutputPath);
246+
}
149247
}
150248
}

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,94 @@ public void BuildProjectWithReferences_CorrectlyBundlesScopedCssFiles()
440440
appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
441441
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
442442
}
443+
444+
[Fact]
445+
public void ScopedCss_IsBackwardsCompatible_WithPreviousVersions()
446+
{
447+
var testAsset = "RazorAppWithPackageAndP2PReference";
448+
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
449+
.WithProjectChanges((project, document) =>
450+
{
451+
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
452+
{
453+
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.1");
454+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
455+
}
456+
if (Path.GetFileName(project) == "ClassLibrary.csproj")
457+
{
458+
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.0");
459+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
460+
}
461+
});
462+
463+
var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
464+
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
465+
build.Execute("/bl").Should().Pass();
466+
467+
var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
468+
var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
469+
470+
// GenerateStaticWebAssetsManifest should copy the file to the output folder.
471+
var finalPath = Path.Combine(outputPath, "AppWithPackageAndP2PReference.staticwebassets.json");
472+
new FileInfo(finalPath).Should().Exist();
473+
AssertManifest(
474+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
475+
LoadBuildManifest());
476+
477+
AssertBuildAssets(
478+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
479+
outputPath,
480+
intermediateOutputPath);
481+
482+
var appBundle = new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "AppWithPackageAndP2PReference.styles.css"));
483+
appBundle.Should().Exist();
484+
485+
appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
486+
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
487+
}
488+
489+
[Fact]
490+
public void ScopedCss_PublishIsBackwardsCompatible_WithPreviousVersions()
491+
{
492+
var testAsset = "RazorAppWithPackageAndP2PReference";
493+
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
494+
.WithProjectChanges((project, document) =>
495+
{
496+
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
497+
{
498+
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.1");
499+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
500+
}
501+
if (Path.GetFileName(project) == "ClassLibrary.csproj")
502+
{
503+
document.Descendants("TargetFramework").Single().ReplaceNodes("netcoreapp3.0");
504+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
505+
}
506+
});
507+
508+
var build = new PublishCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
509+
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
510+
build.Execute("/bl").Should().Pass();
511+
512+
var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
513+
var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
514+
515+
var finalPath = Path.Combine(intermediateOutputPath, "StaticWebAssets.publish.json");
516+
new FileInfo(finalPath).Should().Exist();
517+
AssertManifest(
518+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
519+
LoadPublishManifest());
520+
521+
AssertBuildAssets(
522+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
523+
outputPath,
524+
intermediateOutputPath);
525+
526+
var appBundle = new FileInfo(Path.Combine(outputPath, "wwwroot", "AppWithPackageAndP2PReference.styles.css"));
527+
appBundle.Should().Exist();
528+
529+
appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
530+
appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
531+
}
443532
}
444533
}

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
using Microsoft.NET.TestFramework.Commands;
1010
using Xunit;
1111
using Xunit.Abstractions;
12+
using System.Linq;
13+
using System.Diagnostics;
14+
using System.Xml.Linq;
15+
using NuGet.Packaging;
16+
using System;
1217

1318
namespace Microsoft.NET.Sdk.Razor.Tests
1419
{
@@ -194,6 +199,121 @@ public void BuildProjectWithReferences_GeneratesJsonManifestAndCopiesItToOutputF
194199
intermediateOutputPath);
195200
}
196201

202+
[Fact]
203+
public void BuildProjectWithReferences_WorksWithStaticWebAssetsV1ClassLibraries()
204+
{
205+
var testAsset = "RazorAppWithPackageAndP2PReference";
206+
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
207+
.WithProjectChanges((project, document) =>
208+
{
209+
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
210+
{
211+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
212+
document.Descendants("FrameworkReference").Single().Remove();
213+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
214+
}
215+
if (Path.GetFileName(project) == "ClassLibrary.csproj")
216+
{
217+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.0");
218+
document.Descendants("FrameworkReference").Single().Remove();
219+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
220+
}
221+
});
222+
223+
// We are deleting Views and Components because we are only interested in the static web assets behavior for this test
224+
// and this makes it easier to validate the test.
225+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "AnotherClassLib", "Views"), recursive: true);
226+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "ClassLibrary", "Views"), recursive: true);
227+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "ClassLibrary", "Components"), recursive: true);
228+
229+
var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
230+
build.WithWorkingDirectory(ProjectDirectory.TestRoot);
231+
build.Execute("/bl").Should().Pass();
232+
233+
var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
234+
var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
235+
236+
// GenerateStaticWebAssetsManifest should generate the manifest file.
237+
var path = Path.Combine(intermediateOutputPath, "StaticWebAssets.build.json");
238+
new FileInfo(path).Should().Exist();
239+
AssertManifest(
240+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path)),
241+
LoadBuildManifest());
242+
243+
// GenerateStaticWebAssetsManifest should copy the file to the output folder.
244+
var finalPath = Path.Combine(outputPath, "AppWithPackageAndP2PReference.staticwebassets.json");
245+
new FileInfo(finalPath).Should().Exist();
246+
AssertManifest(
247+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
248+
LoadBuildManifest());
249+
250+
AssertBuildAssets(
251+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(finalPath)),
252+
outputPath,
253+
intermediateOutputPath);
254+
}
255+
256+
[Fact]
257+
public void PublishProjectWithReferences_WorksWithStaticWebAssetsV1ClassLibraries()
258+
{
259+
var testAsset = "RazorAppWithPackageAndP2PReference";
260+
ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
261+
.WithProjectChanges((project, document) =>
262+
{
263+
if (Path.GetFileName(project) == "AnotherClassLib.csproj")
264+
{
265+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.1");
266+
document.Descendants("FrameworkReference").Single().Remove();
267+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
268+
}
269+
if (Path.GetFileName(project) == "ClassLibrary.csproj")
270+
{
271+
document.Descendants("TargetFramework").Single().ReplaceNodes("netstandard2.0");
272+
document.Descendants("FrameworkReference").Single().Remove();
273+
document.Descendants("PropertyGroup").First().Add(new XElement("RazorLangVersion", "3.0"));
274+
}
275+
});
276+
277+
// We are deleting Views and Components because we are only interested in the static web assets behavior for this test
278+
// and this makes it easier to validate the test.
279+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "AnotherClassLib", "Views"), recursive: true);
280+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "ClassLibrary", "Views"), recursive: true);
281+
Directory.Delete(Path.Combine(ProjectDirectory.TestRoot, "ClassLibrary", "Components"), recursive: true);
282+
283+
var restore = new RestoreCommand(Log, Path.Combine(ProjectDirectory.TestRoot, "AppWithPackageAndP2PReference"));
284+
restore.Execute().Should().Pass();
285+
286+
var publish = new PublishCommand(Log, Path.Combine(ProjectDirectory.TestRoot, "AppWithPackageAndP2PReference"));
287+
publish.WithWorkingDirectory(ProjectDirectory.Path);
288+
publish.Execute("/bl").Should().Pass();
289+
290+
var intermediateOutputPath = publish.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
291+
var publishPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
292+
293+
// GenerateStaticWebAssetsManifest should generate the manifest file.
294+
var path = Path.Combine(intermediateOutputPath, "StaticWebAssets.build.json");
295+
new FileInfo(path).Should().Exist();
296+
AssertManifest(
297+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(path)),
298+
LoadBuildManifest());
299+
300+
// GenerateStaticWebAssetsManifest should copy the file to the output folder.
301+
var finalPath = Path.Combine(publishPath, "AppWithPackageAndP2PReference.staticwebassets.json");
302+
new FileInfo(finalPath).Should().NotExist();
303+
304+
// GenerateStaticWebAssetsPublishManifest should generate the publish manifest file.
305+
var intermediatePublishManifestPath = Path.Combine(intermediateOutputPath, "StaticWebAssets.publish.json");
306+
new FileInfo(path).Should().Exist();
307+
AssertManifest(
308+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(intermediatePublishManifestPath)),
309+
LoadPublishManifest());
310+
311+
AssertPublishAssets(
312+
StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(intermediatePublishManifestPath)),
313+
publishPath,
314+
intermediateOutputPath);
315+
}
316+
197317
// Build no dependencies
198318
[Fact]
199319
public void BuildProjectWithReferences_NoDependencies_GeneratesJsonManifestAndCopiesItToOutputFolder()

0 commit comments

Comments
 (0)