dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
512 stars 257 forks source link

New Version of the AnalyzerTestFramework on Nuget #940

Open jmarolf opened 2 years ago

jmarolf commented 2 years ago

Discussed in https://github.com/dotnet/roslyn/discussions/58345

Originally posted by **Herrmel** December 15, 2021 Currently the AnalyzerTestFramework (Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest & Co) is a little bit outdated on Nuget. I've seen ReferenceAssemblies.Net.Net60 is already existing in the Repo but not yet on Nuget. That one I could fix myself but I would like to try the new SourceGeneratorAPI but the current Tests only allow the old Api (ISourceGenerator instead IIncrementalSourceGenerator). I'm not shure wether this is already implemented or not. Can I expect to get an update of the Package on Nuget soon?
AraHaan commented 2 years ago

This is not implemented yet due to the testing apis using older versions of the roslyn packages.

Alternatively one could add new libraries for testing incremental generators:

As for integrating with the existing testers new packages for those can be added as well which depends on these additional projects, but have these depend on roslyn 4.0.1 (so it does not break those who test source generator v1 apis).

I even have a local implementation of the above too that I will post below that can be pull requested in by someone here (all you have to do is place them in the required namespaces):

public class CSharpIncrementalGeneratorTest<TSourceGenerator, TVerifier> : SourceGeneratorTest<TVerifier>
    where TSourceGenerator : IIncrementalGenerator, new()
    where TVerifier : IVerifier, new()
{
    protected override IEnumerable<ISourceGenerator> GetSourceGenerators()
        => new[] { new TSourceGenerator().AsSourceGenerator() };

    protected override string DefaultFileExt => "cs";

    public override string Language => LanguageNames.CSharp;

    protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators)
    {
        return CSharpGeneratorDriver.Create(
            sourceGenerators,
            project.AnalyzerOptions.AdditionalFiles,
            (CSharpParseOptions)project.ParseOptions!,
            project.AnalyzerOptions.AnalyzerConfigOptionsProvider);
    }

    protected override CompilationOptions CreateCompilationOptions()
        => new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);

    protected override ParseOptions CreateParseOptions()
        => new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Diagnose);
}

public class CSharpIncrementalGeneratorVerifier<TSourceGenerator, TVerifier> : IncrementalGeneratorVerifier<TSourceGenerator, CSharpIncrementalGeneratorTest<TSourceGenerator, TVerifier>, TVerifier>
    where TSourceGenerator : IIncrementalGenerator, new()
    where TVerifier : IVerifier, new()
{
}

/// <summary>A default verifier for incremental source generators.</summary>
/// <typeparam name="TSourceGenerator">The <see cref="T:Microsoft.CodeAnalysis.IIncrementalGenerator" /> to test.</typeparam>
/// <typeparam name="TTest">The test implementation to use.</typeparam>
/// <typeparam name="TVerifier">The type of verifier to use.</typeparam>
public class IncrementalGeneratorVerifier<TSourceGenerator, TTest, TVerifier>
    where TSourceGenerator : IIncrementalGenerator, new()
    where TTest : SourceGeneratorTest<TVerifier>, new()
    where TVerifier : IVerifier, new()
{
}
stan-sz commented 1 year ago

In the topic of Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest - when can we expect new version published?

sharwell commented 1 year ago

@stan-sz My recommendation would be to reference it from the feed where each new build is published. This is what we do for our own open-source projects. There's a bunch of manual overhead for attempting to prepare a NuGet release from this repository, so they tend to be infrequent (it's more complicated than other projects for release due to the way it's tied in with the internal signed build pipelines, and there isn't any overlap between the people who can publish it to NuGet and the people who work in this repository regularly).