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

[Question] How to pass AnalyzerConfigOptions when unit testing #736

Closed
j0shuams opened this issue Feb 22, 2021 · 5 comments
Closed

[Question] How to pass AnalyzerConfigOptions when unit testing #736

j0shuams opened this issue Feb 22, 2021 · 5 comments
Labels
Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing question

Comments

@j0shuams
Copy link

When working on a component of a SourceGenerator, I was writing unit tests to check for diagnostics getting raised on the compilation context created by a source code string. We have a downstream check that checks a compilation's AnalyzerConfigOptions for a specified assembly name. When you create a compilation context in a unit test, there's no project file to specify a property AssemblyName, so this check returns null and causes issues.

Here is where I do this and would like to pass some kind of optionsProvider containing a mapping for "AssemblyName" to something like "TestNamespace", and any other properties we might need.

Please advise on how I would be able to do this. Let me know if you need more information.

I'm using these packages:

Microsoft.CodeAnalysis.CSharp.Workspaces, Version="3.8.0-4.20472.6"
Microsoft.CodeAnalysis.Analyzers, Version="3.3.0"

@jmarolf
Copy link
Contributor

jmarolf commented Feb 22, 2021

I would recommend using Microsoft.CodeAnalysis.Testing from here. There should be properties on the ProjectState or SolutionState to add your own custom AnalyzerConfigOptions @sharwell is that correct?

@Youssef1313
Copy link
Member

@jmarolf Does the testing library work for source generators? I thought it didn't.

@sharwell
Copy link
Member

sharwell commented Feb 23, 2021

There should be properties on the ProjectState or SolutionState to add your own custom AnalyzerConfigOptions @sharwell is that correct?

It wasn't correct, but I just added support in #734.

Does the testing library work for source generators?

@Youssef1313 Yes, you can use Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit (or equivalent for other language/frameworks).

@sharwell sharwell transferred this issue from dotnet/roslyn-analyzers Feb 23, 2021
@sharwell sharwell added Area-MS.CA.Testing Microsoft.CodeAnalysis.Testing question labels Feb 23, 2021
@Youssef1313
Copy link
Member

@Youssef1313 Yes, you can use Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit (or equivalent for other language/frameworks).

This is great! May you update the README for usage examples?

@sharwell
Copy link
Member

Filed #737 to add the packages to the list, but haven't added examples yet

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 question
Projects
None yet
Development

No branches or pull requests

4 participants