Reputation: 8074
I've created a Roslyn analyzer using the VS 2015 templates. I've got everything working, including the unit tests, given the diagnostics are enabled by default.
If I set the isEnabledByDefault
parameter on the DiagnosticDescriptor
to false
I can get everything working in Visual Studio by enabling the diagnostics using a .ruleset
file. However once the diagnostics are disabled by default the unit tests won't report their results anymore.
How can I enable these disabled-by-default diagnostics during unit tests? I'm prepared to alter the way the unit tests invoke the Roslyn Compilation/Analyzer/Diagnostic/etc. results, but I haven't found a way to specify the settings given the lack of documentation I've managed to scrape from various sources.
Upvotes: 3
Views: 462
Reputation: 1245
You need to override the diagnostic severity similar to the rule file,
the CompilationOptions.SpecificDiagnosticOptions
allow for that (a compilation has Options
that can override something like this).
I've successfully changed my analyzer to be disabled, and override the DiagnosticVerifier.Helper
(in Helpers
), in my pet project (see commit 8dfc02c
for how I did that). Basically, it boils down to:
private static Compilation OverrideDiagnosticSeverity(
Compilation compilation,
string diagnosticId,
ReportDiagnostic reportDiagnostic)
{
var compilationOptions = compilation.Options;
var specificDiagnosticOptions = compilationOptions.SpecificDiagnosticOptions;
specificDiagnosticOptions = specificDiagnosticOptions.Add(diagnosticId, reportDiagnostic);
var options = compilationOptions.WithSpecificDiagnosticOptions(specificDiagnosticOptions);
return compilation.WithOptions(options);
}
You might want to add a flag to VerifyCSharpDiagnostic
to do this on request of the unit test.
Upvotes: 5