Skip to content

Commit ecb4434

Browse files
authored
Avoid referencing a test assembly in compilation (#11005)
Related to #10343.
1 parent 80cd097 commit ecb4434

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
1111
using Microsoft.AspNetCore.Razor.Test.Common;
1212
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CSharp;
14+
using Microsoft.CodeAnalysis.Test.Utilities;
1315
using Roslyn.Test.Utilities;
1416

1517
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests;
@@ -22,8 +24,15 @@ public CodeGenerationIntegrationTest(bool designTime = false)
2224
: base(layer: TestProject.Layer.Compiler)
2325
{
2426
this.designTime = designTime;
25-
BaseCompilation = BaseCompilation.AddReferences(
26-
MetadataReference.CreateFromFile(typeof(TestTagHelperDescriptors).Assembly.Location));
27+
var testTagHelpers = CSharpCompilation.Create(
28+
assemblyName: "Microsoft.AspNetCore.Razor.Language.Test",
29+
syntaxTrees:
30+
[
31+
CSharpSyntaxTree.ParseText(TestTagHelperDescriptors.Code),
32+
],
33+
references: ReferenceUtil.AspNetLatestAll,
34+
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
35+
BaseCompilation = BaseCompilation.AddReferences(testTagHelpers.VerifyDiagnostics().EmitToImageReference());
2736
}
2837

2938
[IntegrationTestFact]

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/TestTagHelperDescriptors.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public static IEnumerable<TagHelperDescriptor> EnumTagHelperDescriptors
215215
.Name("catch-all")
216216
.Metadata(PropertyName("CatchAll"))
217217
.AsEnum()
218-
.TypeName($"{typeof(TestTagHelperDescriptors).FullName}.{nameof(MyEnum)}"),
218+
.TypeName("Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestTagHelperDescriptors.MyEnum"),
219219
}),
220220
CreateTagHelperDescriptor(
221221
tagName: "input",
@@ -227,7 +227,7 @@ public static IEnumerable<TagHelperDescriptor> EnumTagHelperDescriptors
227227
.Name("value")
228228
.Metadata(PropertyName("Value"))
229229
.AsEnum()
230-
.TypeName($"{typeof(TestTagHelperDescriptors).FullName}.{nameof(MyEnum)}"),
230+
.TypeName("Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestTagHelperDescriptors.MyEnum"),
231231
}),
232232
};
233233
}
@@ -644,9 +644,17 @@ private class TestType
644644
public string BoundProperty { get; set; }
645645
}
646646

647-
public enum MyEnum
648-
{
649-
MyValue,
650-
MySecondValue
651-
}
647+
public static readonly string Code = """
648+
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
649+
{
650+
public class TestTagHelperDescriptors
651+
{
652+
public enum MyEnum
653+
{
654+
MyValue,
655+
MySecondValue
656+
}
657+
}
658+
}
659+
""";
652660
}

0 commit comments

Comments
 (0)