Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert SourceGenerator_RazorFiles_Works to Microsoft.CodeAnalysis.Testing #8356

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sharwell
Copy link
Member

This is a proof of concept for discussion.


var result = RunGenerator(compilation!, ref driver)
.VerifyPageOutput(
@"#pragma checksum ""Pages/Index.razor"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""6b5db227a6aa2228c777b0771108b184b1fc5df3""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 the page output was moved to an embedded resource, which is generated by modifying the #define at the top of the test file and otherwise loaded from disk for the test

@@ -0,0 +1,22 @@
#pragma checksum "/0/Pages/Index.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "6b5db227a6aa2228c777b0771108b184b1fc5df3"
Copy link
Member Author

@sharwell sharwell Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This line is the only change to this file relative to the original string. The change presumably occurred because AdditionalFiles now includes an absolute path to the razor source file. It's not clear to me whether a full build with the source generator running will use an absolute path here or a relative path here. If it's a relative path, I'm not sure where it's derived from.

Comment on lines +2499 to +2500
// Don't resolve any reference assemblies from NuGet
ReferenceAssemblies = new ReferenceAssemblies("custom");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This line ensures that we don't hit NuGet for anything during testing (unless a specific test overrides this property to a different value).

Comment on lines +2502 to +2519
foreach (var defaultCompileLibrary in DependencyContext.Load(typeof(RazorSourceGeneratorTests).Assembly)!.CompileLibraries)
{
foreach (var resolveReferencePath in defaultCompileLibrary.ResolveReferencePaths(new AppLocalResolver()))
{
TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(resolveReferencePath));
}
}

// The deps file in the project is incorrect and does not contain "compile" nodes for some references.
// However these binaries are always present in the bin output. As a "temporary" workaround, we'll add
// every dll file that's present in the test's build output as a metadatareference.
foreach (var assembly in Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll"))
{
if (!TestState.AdditionalReferences.Any(c => string.Equals(Path.GetFileNameWithoutExtension(c.Display), Path.GetFileNameWithoutExtension(assembly), StringComparison.OrdinalIgnoreCase)))
{
TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(assembly));
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This manually adds MetadataReference items for all the items that were being added to the old tests. The whole block was copied from CreateBaseProject().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant