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

Test fails when SourceGeneratorTest<TVerifier>.TestState.Sources depends on generated source code #762

Closed
kzrnm opened this issue Mar 14, 2021 · 4 comments
Labels
Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing

Comments

@kzrnm
Copy link
Contributor

kzrnm commented Mar 14, 2021

Steps to Reproduce:

c2ab239

SourceGeneratorTest.TestState.Sources depends on generated source code.

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.CodeAnalysis.Testing.TestGenerators
{
    public class AddSimpleAttribute : ISourceGenerator
    {
        public void Initialize(GeneratorInitializationContext context)
        {
        }

        public virtual void Execute(GeneratorExecutionContext context)
        {
            context.AddSource("SimpleAttribute", "using System;[AttributeUsage(AttributeTargets.All)] internal sealed class SimpleAttribute : Attribute { }");
        }
    }
}
await new CSharpSourceGeneratorTest<AddSimpleAttribute>
{
    TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
    TestState =
    {
        Sources =
        {
            @"[Simple]class S {}",
        },
        GeneratedSources =
        {
            (typeof(AddSimpleAttribute), "SimpleAttribute.cs", "using System;[AttributeUsage(AttributeTargets.All)] internal sealed class SimpleAttribute : Attribute { }"),
        },
    },
}.RunAsync();

Expected Behavior:

Success

Actual Behavior:

System.InvalidOperationException : Context: Diagnostics of test state
Mismatch between number of diagnostics returned, expected "0" actual "2"

Adding diagnostics to ExpectedDiagnostics will solve the problem, but it is different from the actual compilation result.

@sharwell sharwell added the Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing label Apr 29, 2021
@sharwell
Copy link
Member

This should have been fixed by #776. Can you try updating the library and check again?

@kzrnm
Copy link
Contributor Author

kzrnm commented Apr 29, 2021

Great! I want this behavior!

@kzrnm kzrnm closed this as completed Apr 29, 2021
@sharwell
Copy link
Member

@naminodarie Note that this line completely disables validation for your source generator:

TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,

You'll want to remove this line and just use the default behaviors.

@kzrnm
Copy link
Contributor Author

kzrnm commented Apr 30, 2021

@sharwell Thank you for your advice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing
Projects
None yet
Development

No branches or pull requests

2 participants