123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- using System.Text.RegularExpressions;
- using Microsoft.AspNetCore.StaticWebAssets.Tasks;
- namespace Microsoft.NET.Sdk.Razor.Tests
- {
- public class ScopedCssIntegrationTest : AspNetSdkBaselineTest
- {
- public ScopedCssIntegrationTest(ITestOutputHelper log) : base(log, GenerateBaselines) { }
- [Fact]
- public void Build_NoOps_WhenScopedCssIsDisabled()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute("/p:ScopedCssEnabled=false").Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "ComponentApp.styles.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "FetchData.razor.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void Build_NoOps_ForMvcApp_WhenScopedCssIsDisabled()
- {
- var testAsset = "RazorSimpleMvc";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute("/p:ScopedCssEnabled=false").Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Index.cshtml.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Contact.cshtml.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "SimpleMvc.styles.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "About.cshtml.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void CanDisableDefaultDiscoveryConvention()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute("/p:EnableDefaultScopedCssItems=false").Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "ComponentApp.styles.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "FetchData.razor.rz.scp.css")).Should().NotExist();
- }
- [CoreMSBuildOnlyFact]
- public void CanOverrideScopeIdentifiers()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset)
- .WithProjectChanges(project =>
- {
- var ns = project.Root.Name.Namespace;
- var itemGroup = new XElement(ns + "ItemGroup");
- var element = new XElement("ScopedCssInput", new XAttribute("Include", @"Styles\Pages\Counter.css"));
- element.Add(new XElement("RazorComponent", @"Components\Pages\Counter.razor"));
- element.Add(new XElement("CssScope", "b-overriden"));
- itemGroup.Add(element);
- project.Root.Add(itemGroup);
- });
- var stylesFolder = Path.Combine(projectDirectory.Path, "Styles", "Pages");
- Directory.CreateDirectory(stylesFolder);
- var styles = Path.Combine(stylesFolder, "Counter.css");
- File.Move(Path.Combine(projectDirectory.Path, "Components", "Pages", "Counter.razor.css"), styles);
- var build = new BuildCommand(projectDirectory);
- build.Execute("/p:EnableDefaultScopedCssItems=false", "/p:EmitCompilerGeneratedFiles=true").Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- var scoped = Path.Combine(intermediateOutputPath, "scopedcss", "Styles", "Pages", "Counter.rz.scp.css");
- new FileInfo(scoped).Should().Exist();
- new FileInfo(scoped).Should().Contain("b-overriden");
- var generated = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs");
- new FileInfo(generated).Should().Exist();
- new FileInfo(generated).Should().Contain("b-overriden");
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void Build_GeneratesTransformedFilesAndBundle_ForComponentsWithScopedCss()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "ComponentApp.styles.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "projectbundle", "ComponentApp.bundle.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "FetchData.razor.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void Build_GeneratesTransformedFilesAndBundle_ForViewsWithScopedCss()
- {
- var testAsset = "RazorSimpleMvc";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Index.cshtml.rz.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Contact.cshtml.rz.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "SimpleMvc.styles.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "projectbundle", "SimpleMvc.bundle.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "About.cshtml.rz.scp.css")).Should().Exist();
- }
- [Fact]
- public void Build_ScopedCssFiles_ContainsUniqueScopesPerFile()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- var generatedCounter = Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css");
- new FileInfo(generatedCounter).Should().Exist();
- var generatedIndex = Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css");
- new FileInfo(generatedIndex).Should().Exist();
- var counterContent = File.ReadAllText(generatedCounter);
- var indexContent = File.ReadAllText(generatedIndex);
- var counterScopeMatch = Regex.Match(counterContent, ".*button\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(counterScopeMatch.Success, "Couldn't find a scope id in the generated Counter scoped css file.");
- var counterScopeId = counterScopeMatch.Groups[1].Captures[0].Value;
- var indexScopeMatch = Regex.Match(indexContent, ".*h1\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(indexScopeMatch.Success, "Couldn't find a scope id in the generated Index scoped css file.");
- var indexScopeId = indexScopeMatch.Groups[1].Captures[0].Value;
- Assert.NotEqual(counterScopeId, indexScopeId);
- }
- [Fact]
- public void Build_ScopedCssViews_ContainsUniqueScopesPerView()
- {
- var testAsset = "RazorSimpleMvc";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- var generatedIndex = Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Index.cshtml.rz.scp.css");
- new FileInfo(generatedIndex).Should().Exist();
- var generatedAbout = Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "About.cshtml.rz.scp.css");
- new FileInfo(generatedAbout).Should().Exist();
- var generatedContact = Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Contact.cshtml.rz.scp.css");
- new FileInfo(generatedContact).Should().Exist();
- var indexContent = File.ReadAllText(generatedIndex);
- var aboutContent = File.ReadAllText(generatedAbout);
- var contactContent = File.ReadAllText(generatedContact);
- var indexScopeMatch = Regex.Match(indexContent, ".*p\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(indexScopeMatch.Success, "Couldn't find a scope id in the generated Index scoped css file.");
- var indexScopeId = indexScopeMatch.Groups[1].Captures[0].Value;
- var aboutScopeMatch = Regex.Match(aboutContent, ".*h2\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(aboutScopeMatch.Success, "Couldn't find a scope id in the generated About scoped css file.");
- var aboutScopeId = aboutScopeMatch.Groups[1].Captures[0].Value;
- var contactScopeMatch = Regex.Match(contactContent, ".*a\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(contactScopeMatch.Success, "Couldn't find a scope id in the generated Contact scoped css file.");
- var contactScopeId = contactScopeMatch.Groups[1].Captures[0].Value;
- Assert.NotEqual(indexScopeId, aboutScopeId);
- Assert.NotEqual(indexScopeId, contactScopeId);
- Assert.NotEqual(aboutScopeId, contactScopeId);
- }
- [Fact]
- public void Build_WorksWhenViewsAndComponentsArePartOfTheSameProject_ContainsUniqueScopesPerFile()
- {
- var testAsset = "RazorMvcWithComponents";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- var generatedIndex = Path.Combine(intermediateOutputPath, "scopedcss", "Views", "Home", "Index.cshtml.rz.scp.css");
- new FileInfo(generatedIndex).Should().Exist();
- var generatedCounter = Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Counter.razor.rz.scp.css");
- new FileInfo(generatedCounter).Should().Exist();
- var indexContent = File.ReadAllText(generatedIndex);
- var counterContent = File.ReadAllText(generatedCounter);
- var indexScopeMatch = Regex.Match(indexContent, ".*p\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(indexScopeMatch.Success, "Couldn't find a scope id in the generated Index scoped css file.");
- var indexScopeId = indexScopeMatch.Groups[1].Captures[0].Value;
- var counterScopeMatch = Regex.Match(counterContent, ".*div\\[(.*)\\].*", RegexOptions.Multiline | RegexOptions.IgnoreCase);
- Assert.True(counterScopeMatch.Success, "Couldn't find a scope id in the generated Counter scoped css file.");
- var counterScopeId = counterScopeMatch.Groups[1].Captures[0].Value;
- Assert.NotEqual(indexScopeId, counterScopeId);
- }
- [Fact]
- public void Publish_PublishesBundleToTheRightLocation()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var publish = new PublishCommand(projectDirectory);
- publish.WithWorkingDirectory(projectDirectory.TestRoot);
- publish.Execute().Should().Pass();
- var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "ComponentApp.styles.css")).Should().Exist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void Publish_NoBuild_PublishesBundleToTheRightLocation()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.WithWorkingDirectory(projectDirectory.Path);
- var buildResult = build.Execute();
- buildResult.Should().Pass();
- var publish = new PublishCommand(projectDirectory);
- publish.Execute("/p:NoBuild=true").Should().Pass();
- var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "ComponentApp.styles.css")).Should().Exist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- }
- [Fact]
- public void Publish_DoesNotPublishAnyFile_WhenThereAreNoScopedCssFiles()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- File.Delete(Path.Combine(projectDirectory.Path, "Components", "Pages", "Counter.razor.css"));
- File.Delete(Path.Combine(projectDirectory.Path, "Components", "Pages", "Index.razor.css"));
- var publish = new PublishCommand(Log, projectDirectory.TestRoot);
- publish.Execute().Should().Pass();
- var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "_framework", "scoped.styles.css")).Should().NotExist();
- }
- [Fact]
- public void Publish_Publishes_IndividualScopedCssFiles_WhenNoBundlingIsEnabled()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var publish = new PublishCommand(projectDirectory);
- publish.WithWorkingDirectory(projectDirectory.TestRoot);
- publish.Execute("/p:DisableScopedCssBundling=true").Should().Pass();
- var publishOutputPath = publish.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "_content", "ComponentApp", "ComponentApp.styles.css")).Should().NotExist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "Components", "Pages", "Index.razor.rz.scp.css")).Should().Exist();
- new FileInfo(Path.Combine(publishOutputPath, "wwwroot", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().Exist();
- }
- [CoreMSBuildOnlyFact]
- public void Build_RemovingScopedCssAndBuilding_UpdatesGeneratedCodeAndBundle()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(projectDirectory);
- build.Execute("/p:EmitCompilerGeneratedFiles=true").Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().Exist();
- var generatedBundle = Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "ComponentApp.styles.css");
- new FileInfo(generatedBundle).Should().Exist();
- var generatedProjectBundle = Path.Combine(intermediateOutputPath, "scopedcss", "projectbundle", "ComponentApp.bundle.scp.css");
- new FileInfo(generatedProjectBundle).Should().Exist();
- var generatedCounter = Path.Combine(intermediateOutputPath, "generated", "Microsoft.CodeAnalysis.Razor.Compiler", "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", "Components_Pages_Counter_razor.g.cs");
- new FileInfo(generatedCounter).Should().Exist();
- var componentThumbprint = FileThumbPrint.Create(generatedCounter);
- var bundleThumbprint = FileThumbPrint.Create(generatedBundle);
- File.Delete(Path.Combine(projectDirectory.Path, "Components", "Pages", "Counter.razor.css"));
- build = new BuildCommand(projectDirectory);
- build.Execute("/p:EmitCompilerGeneratedFiles=true").Should().Pass();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(generatedCounter).Should().Exist();
- var newComponentThumbprint = FileThumbPrint.Create(generatedCounter);
- var newBundleThumbprint = FileThumbPrint.Create(generatedBundle);
- Assert.NotEqual(componentThumbprint, newComponentThumbprint);
- Assert.NotEqual(bundleThumbprint, newBundleThumbprint);
- }
- [Fact]
- public void Does_Nothing_WhenThereAreNoScopedCssFiles()
- {
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- File.Delete(Path.Combine(projectDirectory.Path, "Components", "Pages", "Counter.razor.css"));
- File.Delete(Path.Combine(projectDirectory.Path, "Components", "Pages", "Index.razor.css"));
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Counter.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "Components", "Pages", "Index.razor.rz.scp.css")).Should().NotExist();
- new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "_framework", "scoped.styles.css")).Should().NotExist();
- }
- [Fact]
- public void Build_ScopedCssTransformation_AndBundling_IsIncremental()
- {
- // Arrange
- var thumbprintLookup = new Dictionary<string, FileThumbPrint>();
- var testAsset = "RazorComponentApp";
- var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
- // Act & Assert 1
- var build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = Path.Combine(build.GetBaseIntermediateDirectory().ToString(), "Debug", DefaultTfm);
- var directoryPath = Path.Combine(intermediateOutputPath, "scopedcss");
- var files = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
- foreach (var file in files)
- {
- var thumbprint = FileThumbPrint.Create(file);
- thumbprintLookup[file] = thumbprint;
- }
- // Act & Assert 2
- for (var i = 0; i < 2; i++)
- {
- build = new BuildCommand(projectDirectory);
- build.Execute().Should().Pass();
- foreach (var file in files)
- {
- var thumbprint = FileThumbPrint.Create(file);
- Assert.Equal(thumbprintLookup[file], thumbprint);
- }
- }
- }
- [Fact]
- public void BuildProjectWithReferences_CorrectlyBundlesScopedCssFiles()
- {
- var testAsset = "RazorAppWithPackageAndP2PReference";
- ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
- build.WithWorkingDirectory(ProjectDirectory.TestRoot);
- build.Execute("/bl").Should().Pass();
- var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
- var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- // GenerateStaticWebAssetsManifest should copy the file to the output folder.
- var finalPath = Path.Combine(outputPath, "AppWithPackageAndP2PReference.staticwebassets.runtime.json");
- new FileInfo(finalPath).Should().Exist();
- var buildManifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(Path.Combine(intermediateOutputPath, "staticwebassets.build.json")));
- AssertManifest(
- buildManifest,
- LoadBuildManifest());
- AssertBuildAssets(
- buildManifest,
- outputPath,
- intermediateOutputPath);
- var appBundle = new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "AppWithPackageAndP2PReference.styles.css"));
- appBundle.Should().Exist();
- appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
- appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
- }
- [Fact]
- public void ScopedCss_IsBackwardsCompatible_WithPreviousVersions()
- {
- var testAsset = "RazorAppWithPackageAndP2PReference";
- ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
- .WithProjectChanges((project, document) =>
- {
- if (Path.GetFileName(project) == "AnotherClassLib.csproj")
- {
- document.Descendants("TargetFramework").Single().ReplaceNodes("net5.0");
- }
- if (Path.GetFileName(project) == "ClassLibrary.csproj")
- {
- document.Descendants("TargetFramework").Single().ReplaceNodes("net5.0");
- }
- });
- var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
- build.WithWorkingDirectory(ProjectDirectory.TestRoot);
- build.Execute("/bl").Should().Pass();
- var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
- var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- // GenerateStaticWebAssetsManifest should copy the file to the output folder.
- var finalPath = Path.Combine(outputPath, "AppWithPackageAndP2PReference.staticwebassets.runtime.json");
- new FileInfo(finalPath).Should().Exist();
- var manifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(Path.Combine(intermediateOutputPath, "staticwebassets.build.json")));
- AssertManifest(
- manifest,
- LoadBuildManifest());
- AssertBuildAssets(
- manifest,
- outputPath,
- intermediateOutputPath);
- var appBundle = new FileInfo(Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "AppWithPackageAndP2PReference.styles.css"));
- appBundle.Should().Exist();
- appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
- appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
- }
- [Fact]
- public void ScopedCss_PublishIsBackwardsCompatible_WithPreviousVersions()
- {
- var testAsset = "RazorAppWithPackageAndP2PReference";
- ProjectDirectory = CreateAspNetSdkTestAsset(testAsset)
- .WithProjectChanges((project, document) =>
- {
- if (Path.GetFileName(project) == "AnotherClassLib.csproj")
- {
- document.Descendants("TargetFramework").Single().ReplaceNodes("net5.0");
- }
- if (Path.GetFileName(project) == "ClassLibrary.csproj")
- {
- document.Descendants("TargetFramework").Single().ReplaceNodes("net5.0");
- }
- });
- var build = new PublishCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
- build.WithWorkingDirectory(ProjectDirectory.TestRoot);
- build.Execute().Should().Pass();
- var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
- var outputPath = build.GetOutputDirectory(DefaultTfm, "Debug").ToString();
- var finalPath = Path.Combine(intermediateOutputPath, "staticwebassets.publish.json");
- new FileInfo(finalPath).Should().Exist();
- var publishManifest = StaticWebAssetsManifest.FromJsonBytes(File.ReadAllBytes(Path.Combine(intermediateOutputPath, "staticwebassets.publish.json")));
- AssertManifest(
- publishManifest,
- LoadPublishManifest());
- AssertPublishAssets(
- publishManifest,
- outputPath,
- intermediateOutputPath);
- var appBundle = new FileInfo(Path.Combine(outputPath, "wwwroot", "AppWithPackageAndP2PReference.styles.css"));
- appBundle.Should().Exist();
- appBundle.Should().Contain("_content/ClassLibrary/ClassLibrary.bundle.scp.css");
- appBundle.Should().Contain("_content/PackageLibraryDirectDependency/PackageLibraryDirectDependency.bundle.scp.css");
- }
- // This test verifies if the targets that VS calls to update scoped css works to update these files
- [Fact]
- public void RegeneratingScopedCss_ForProject()
- {
- // Arrange
- var testAsset = "RazorComponentApp";
- ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var build = new BuildCommand(ProjectDirectory);
- build.Execute().Should().Pass();
- var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
- var bundlePath = Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "ComponentApp.styles.css");
- new FileInfo(bundlePath).Should().Exist();
- // Make an edit
- var scopedCssFile = Path.Combine(ProjectDirectory.TestRoot, "Components", "Pages", "Index.razor.css");
- File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(new[] { "body { background-color: orangered; }" }));
- build = new BuildCommand(ProjectDirectory);
- build.Execute("/t:UpdateStaticWebAssetsDesignTime").Should().Pass();
- var fileInfo = new FileInfo(bundlePath);
- fileInfo.Should().Exist();
- // Verify the generated file contains newly added css
- fileInfo.ReadAllText().Should().Contain("background-color: orangered");
- }
- // Regression test for https://github.com/dotnet/aspnetcore/issues/37592
- [Fact]
- public void RegeneratingScopedCss_ForProjectWithReferences()
- {
- var testAsset = "RazorAppWithPackageAndP2PReference";
- ProjectDirectory = CreateAspNetSdkTestAsset(testAsset);
- var scopedCssFile = Path.Combine(ProjectDirectory.Path, "AppWithPackageAndP2PReference", "Index.razor.css");
- File.WriteAllText(scopedCssFile, "/* Empty css */");
- File.WriteAllText(Path.Combine(ProjectDirectory.Path, "AppWithPackageAndP2PReference", "Index.razor"), "This is a test razor component.");
- var build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
- build.Execute().Should().Pass();
- var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
- var bundlePath = Path.Combine(intermediateOutputPath, "scopedcss", "bundle", "AppWithPackageAndP2PReference.styles.css");
- new FileInfo(bundlePath).Should().Exist();
- // Make an edit to a scoped css file
- File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(new[] { "body { background-color: orangered; }" }));
- build = new BuildCommand(ProjectDirectory, "AppWithPackageAndP2PReference");
- build.Execute("/t:UpdateStaticWebAssetsDesignTime").Should().Pass();
- var fileInfo = new FileInfo(bundlePath);
- fileInfo.Should().Exist();
- // Verify the generated file contains newly added css
- var text = fileInfo.ReadAllText();
- text.Should().Contain("background-color: orangered");
- text.Should().Contain("@import '_content/ClassLibrary/ClassLibrary.bundle.scp.css");
- }
- }
- }
|