Skip to content

Commit 167df65

Browse files
committed
Pipe through SuppressUniqueIds to remove checksums from test baselines
1 parent f43572a commit 167df65

File tree

12 files changed

+54
-39
lines changed

12 files changed

+54
-39
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultCodeRenderingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public DefaultCodeRenderingContext(
7373
}
7474

7575
Items[NewLineString] = codeDocument.Items[NewLineString];
76-
Items[SuppressUniqueIds] = codeDocument.Items[SuppressUniqueIds];
76+
Items[SuppressUniqueIds] = codeDocument.Items[SuppressUniqueIds] ?? options.SuppressUniqueIds;
7777

7878
_scopes = new List<ScopeInternal>();
7979
_scopes.Add(new ScopeInternal(nodeWriter));

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public DefaultRazorCodeGenerationOptions(
1818
bool suppressNullabilityEnforcement,
1919
bool omitMinimizedComponentAttributeValues,
2020
bool supportLocalizedComponentNames,
21-
bool useEnhancedLinePragma)
21+
bool useEnhancedLinePragma,
22+
string suppressUniqueIds)
2223
{
2324
IndentWithTabs = indentWithTabs;
2425
IndentSize = indentSize;
@@ -31,6 +32,7 @@ public DefaultRazorCodeGenerationOptions(
3132
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
3233
SupportLocalizedComponentNames = supportLocalizedComponentNames;
3334
UseEnhancedLinePragma = useEnhancedLinePragma;
35+
SuppressUniqueIds = suppressUniqueIds;
3436
}
3537

3638
public override bool DesignTime { get; }

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public override RazorCodeGenerationOptions Build()
6060
SuppressNullabilityEnforcement,
6161
OmitMinimizedComponentAttributeValues,
6262
SupportLocalizedComponentNames,
63-
UseEnhancedLinePragma)
63+
UseEnhancedLinePragma,
64+
SuppressUniqueIds)
6465
{
6566
SuppressMetadataSourceChecksumAttributes = SuppressMetadataSourceChecksumAttributes,
6667
};

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static RazorCodeGenerationOptions CreateDefault()
2222
suppressNullabilityEnforcement: false,
2323
omitMinimizedComponentAttributeValues: false,
2424
supportLocalizedComponentNames: false,
25-
useEnhancedLinePragma: true);
25+
useEnhancedLinePragma: true,
26+
suppressUniqueIds: null);
2627
}
2728

2829
public static RazorCodeGenerationOptions CreateDesignTimeDefault()
@@ -38,7 +39,8 @@ public static RazorCodeGenerationOptions CreateDesignTimeDefault()
3839
suppressNullabilityEnforcement: false,
3940
omitMinimizedComponentAttributeValues: false,
4041
supportLocalizedComponentNames: false,
41-
useEnhancedLinePragma: true);
42+
useEnhancedLinePragma: true,
43+
suppressUniqueIds: null);
4244
}
4345

4446
public static RazorCodeGenerationOptions Create(Action<RazorCodeGenerationOptionsBuilder> configure)
@@ -146,4 +148,9 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenera
146148
/// Gets a value that determines if enhanced line pragmas are to be utilized.
147149
/// </summary>
148150
public virtual bool UseEnhancedLinePragma { get; }
151+
152+
/// <summary>
153+
/// Gets a value that suppresses unique ids for testing purposes.
154+
/// </summary>
155+
internal string SuppressUniqueIds { get; private protected init; }
149156
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public abstract class RazorCodeGenerationOptionsBuilder
8585
/// </summary>
8686
public virtual bool UseEnhancedLinePragma { get; set; }
8787

88+
/// <summary>
89+
/// Gets or sets a value that determines if unique ids are suppressed for testing.
90+
/// </summary>
91+
internal string SuppressUniqueIds { get; set; }
92+
8893
public abstract RazorCodeGenerationOptions Build();
8994

9095
public virtual void SetDesignTime(bool designTime)

src/Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators/RazorSourceGenerationOptions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// Copyright (c) .NET Foundation. All rights reserved.
33
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
44

5-
using System;
65
using Microsoft.AspNetCore.Razor.Language;
76
using Microsoft.CodeAnalysis.CSharp;
87

98
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
109
{
11-
internal sealed class RazorSourceGenerationOptions : IEquatable<RazorSourceGenerationOptions>
10+
internal sealed record RazorSourceGenerationOptions
1211
{
1312
public string RootNamespace { get; set; } = "ASP";
1413

@@ -29,17 +28,10 @@ internal sealed class RazorSourceGenerationOptions : IEquatable<RazorSourceGener
2928
/// </summary>
3029
public bool SupportLocalizedComponentNames { get; set; } = false;
3130

32-
public bool Equals(RazorSourceGenerationOptions other)
33-
{
34-
return
35-
RootNamespace == other.RootNamespace &&
36-
Configuration.Equals(other.Configuration) &&
37-
GenerateMetadataSourceChecksumAttributes == other.GenerateMetadataSourceChecksumAttributes &&
38-
CSharpLanguageVersion == other.CSharpLanguageVersion &&
39-
SupportLocalizedComponentNames == other.SupportLocalizedComponentNames;
40-
}
41-
42-
public override bool Equals(object obj) => obj is RazorSourceGenerationOptions other && Equals(other);
31+
/// <summary>
32+
/// Gets the flag that should be set on code documents to replace unique ids for testing purposes
33+
/// </summary>
34+
internal string? TestSuppressUniqueIds { get; set; }
4335

4436
public override int GetHashCode() => Configuration.GetHashCode();
4537
}

src/Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators/RazorSourceGenerator.Helpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private static RazorProjectEngine GetGenerationProjectEngine(
105105
{
106106
options.SuppressMetadataSourceChecksumAttributes = !razorSourceGeneratorOptions.GenerateMetadataSourceChecksumAttributes;
107107
options.SupportLocalizedComponentNames = razorSourceGeneratorOptions.SupportLocalizedComponentNames;
108+
options.SuppressUniqueIds = razorSourceGeneratorOptions.TestSuppressUniqueIds;
108109
}));
109110

110111
b.Features.Add(new StaticTagHelperFeature { TagHelpers = tagHelpers });

src/Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators/RazorSourceGenerator.RazorProviders.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private static (RazorSourceGenerationOptions?, Diagnostic?) ComputeRazorSourceGe
4545
globalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
4646
globalOptions.TryGetValue("build_property.SupportLocalizedComponentNames", out var supportLocalizedComponentNames);
4747
globalOptions.TryGetValue("build_property.GenerateRazorMetadataSourceChecksumAttributes", out var generateMetadataSourceChecksumAttributes);
48+
globalOptions.TryGetValue("build_property.TestingSuppressUniqueIds", out var suppressUniqueIds);
4849

4950
var razorLanguageVersion = RazorLanguageVersion.Latest;
5051
Diagnostic? diagnostic = null;
@@ -58,14 +59,15 @@ private static (RazorSourceGenerationOptions?, Diagnostic?) ComputeRazorSourceGe
5859
}
5960

6061
var razorConfiguration = RazorConfiguration.Create(razorLanguageVersion, configurationName ?? "default", System.Linq.Enumerable.Empty<RazorExtension>(), true);
61-
62+
6263
var razorSourceGenerationOptions = new RazorSourceGenerationOptions()
6364
{
6465
Configuration = razorConfiguration,
6566
GenerateMetadataSourceChecksumAttributes = generateMetadataSourceChecksumAttributes == "true",
6667
RootNamespace = rootNamespace ?? "ASP",
6768
SupportLocalizedComponentNames = supportLocalizedComponentNames == "true",
6869
CSharpLanguageVersion = ((CSharpParseOptions)parseOptions).LanguageVersion,
70+
TestSuppressUniqueIds = suppressUniqueIds,
6971
};
7072

7173
return (razorSourceGenerationOptions, diagnostic);

src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#nullable enable
@@ -2330,10 +2330,11 @@ public string Invoke(string firstName)
23302330

23312331
// Assert
23322332
result.VerifyPageOutput(
2333-
@"#pragma checksum ""Views/Home/Index.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""209ff2a910aa467bb7942ed3e6cb586652327a44""
2333+
"""
2334+
#pragma checksum "Views/Home/Index.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "209ff2a910aa467bb7942ed3e6cb586652327a44"
23342335
// <auto-generated/>
23352336
#pragma warning disable 1591
2336-
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCoreGeneratedDocument.Views_Home_Index), @""mvc.1.0.view"", @""/Views/Home/Index.cshtml"")]
2337+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCoreGeneratedDocument.Views_Home_Index), @"mvc.1.0.view", @"/Views/Home/Index.cshtml")]
23372338
namespace AspNetCoreGeneratedDocument
23382339
{
23392340
#line hidden
@@ -2344,7 +2345,7 @@ namespace AspNetCoreGeneratedDocument
23442345
using global::Microsoft.AspNetCore.Mvc;
23452346
using global::Microsoft.AspNetCore.Mvc.Rendering;
23462347
using global::Microsoft.AspNetCore.Mvc.ViewFeatures;
2347-
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute(""Identifier"", ""/Views/Home/Index.cshtml"")]
2348+
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("Identifier", "/Views/Home/Index.cshtml")]
23482349
[global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute]
23492350
#nullable restore
23502351
internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
@@ -2374,16 +2375,16 @@ internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.
23742375
#pragma warning disable 1998
23752376
public async override global::System.Threading.Tasks.Task ExecuteAsync()
23762377
{
2377-
__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""vc:test"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, ""209ff2a910aa467bb7942ed3e6cb586652327a442643"", async() => {
2378+
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("vc:test", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "test", async() => {
23782379
}
23792380
);
23802381
__TestViewComponentTagHelper = CreateTagHelper<global::AspNetCoreGeneratedDocument.Views_Home_Index.__Generated__TestViewComponentTagHelper>();
23812382
__tagHelperExecutionContext.Add(__TestViewComponentTagHelper);
23822383
BeginWriteTagHelperAttribute();
2383-
WriteLiteral(""Jan"");
2384+
WriteLiteral("Jan");
23842385
__tagHelperStringValueBuffer = EndWriteTagHelperAttribute();
23852386
__TestViewComponentTagHelper.firstName = __tagHelperStringValueBuffer;
2386-
__tagHelperExecutionContext.AddTagHelperAttribute(""first-name"", __TestViewComponentTagHelper.firstName, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
2387+
__tagHelperExecutionContext.AddTagHelperAttribute("first-name", __TestViewComponentTagHelper.firstName, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
23872388
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
23882389
if (!__tagHelperExecutionContext.Output.IsContentModified)
23892390
{
@@ -2413,7 +2414,7 @@ internal sealed class Views_Home_Index : global::Microsoft.AspNetCore.Mvc.Razor.
24132414
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
24142415
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; } = default!;
24152416
#nullable disable
2416-
[Microsoft.AspNetCore.Razor.TagHelpers.HtmlTargetElementAttribute(""vc:test"")]
2417+
[Microsoft.AspNetCore.Razor.TagHelpers.HtmlTargetElementAttribute("vc:test")]
24172418
public class __Generated__TestViewComponentTagHelper : Microsoft.AspNetCore.Razor.TagHelpers.TagHelper
24182419
{
24192420
private readonly global::Microsoft.AspNetCore.Mvc.IViewComponentHelper __helper = null;
@@ -2427,14 +2428,14 @@ public __Generated__TestViewComponentTagHelper(global::Microsoft.AspNetCore.Mvc.
24272428
public override async global::System.Threading.Tasks.Task ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext __context, Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput __output)
24282429
{
24292430
(__helper as global::Microsoft.AspNetCore.Mvc.ViewFeatures.IViewContextAware)?.Contextualize(ViewContext);
2430-
var __helperContent = await __helper.InvokeAsync(""Test"", ProcessInvokeAsyncArgs(__context));
2431+
var __helperContent = await __helper.InvokeAsync("Test", ProcessInvokeAsyncArgs(__context));
24312432
__output.TagName = null;
24322433
__output.Content.SetHtmlContent(__helperContent);
24332434
}
24342435
private Dictionary<string, object> ProcessInvokeAsyncArgs(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext __context)
24352436
{
24362437
Dictionary<string, object> args = new Dictionary<string, object>();
2437-
if (__context.AllAttributes.ContainsName(""first-name""))
2438+
if (__context.AllAttributes.ContainsName("first-name"))
24382439
{
24392440
args[nameof(firstName)] = firstName;
24402441
}
@@ -2444,7 +2445,8 @@ private Dictionary<string, object> ProcessInvokeAsyncArgs(Microsoft.AspNetCore.R
24442445
}
24452446
}
24462447
#pragma warning restore 1591
2447-
");
2448+
2449+
""");
24482450
Assert.Empty(result.Diagnostics);
24492451
Assert.Single(result.GeneratedSources);
24502452
}
@@ -2615,7 +2617,8 @@ public class X {}
26152617
var driver = await GetDriverAsync(project);
26162618

26172619
var result = RunGenerator(compilation!, ref driver).VerifyPageOutput(
2618-
@"#pragma checksum ""Component.Razor"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""20b14071a74e1fd554d7b3dff6ff41722270ebee""
2620+
"""
2621+
#pragma checksum "Component.Razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "20b14071a74e1fd554d7b3dff6ff41722270ebee"
26192622
// <auto-generated/>
26202623
#pragma warning disable 1591
26212624
namespace MyApp
@@ -2631,11 +2634,11 @@ public partial class Component : global::Microsoft.AspNetCore.Components.Compone
26312634
#pragma warning disable 1998
26322635
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
26332636
{
2634-
__builder.AddMarkupContent(0, ""<h1>Hello world</h1>"");
2637+
__builder.AddMarkupContent(0, "<h1>Hello world</h1>");
26352638
}
26362639
#pragma warning restore 1998
26372640
#nullable restore
2638-
#line 4 ""Component.Razor""
2641+
#line 4 "Component.Razor"
26392642
26402643
public class X {}
26412644
@@ -2645,7 +2648,8 @@ public class X {}
26452648
}
26462649
}
26472650
#pragma warning restore 1591
2648-
");
2651+
2652+
""");
26492653

26502654
Assert.Empty(result.Diagnostics);
26512655
Assert.Single(result.GeneratedSources);

src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTestsBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#nullable enable
@@ -68,6 +68,7 @@ protected static async ValueTask<GeneratorDriver> GetDriverAsync(Project project
6868
optionsProvider.TestGlobalOptions["build_property.RootNamespace"] = "MyApp";
6969
optionsProvider.TestGlobalOptions["build_property.RazorLangVersion"] = "Latest";
7070
optionsProvider.TestGlobalOptions["build_property.GenerateRazorMetadataSourceChecksumAttributes"] = "false";
71+
optionsProvider.TestGlobalOptions["build_property.TestingSuppressUniqueIds"] = "test";
7172

7273
var additionalTexts = ImmutableArray<AdditionalText>.Empty;
7374

0 commit comments

Comments
 (0)