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

Disable SA0002 (Invalid settings file) for empty projects #3456

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ public async Task TestMissingSettingsAsync()
await VerifyCSharpDiagnosticAsync(TestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3453, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3453")]
public async Task TestNoSourceFilesAsync()
{
string emptySettings = @"{ ""settings"": { } }";
await new CSharpTest
{
TestState =
{
Sources = { string.Empty },
AdditionalFiles = { ("stylecop.json", emptySettings) },
AdditionalProjects =
{
["EmptyProjectWithSettings"] =
{
// The main test state doesn't allow empty Sources, so we use a second project to validate
// the completely empty case.
AdditionalFiles = { ("stylecop.json", emptySettings) },
},
},
},
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestValidSettingsAsync()
{
Expand Down Expand Up @@ -329,18 +353,33 @@ public async Task TestEmptySettingsAsync()
}

[Fact]
public void TestUnexpectedExceptionNotCaught()
public async Task TestUnexpectedExceptionNotCaughtAsync()
{
var analysisContext = new AnalysisContextMissingOptions();
var analyzer = new SA0002InvalidSettingsFile();
analyzer.Initialize(analysisContext);
Assert.NotNull(analysisContext.CompilationAction);

var additionalFiles = ImmutableArray.Create<AdditionalText>(new InvalidAdditionalText());
Assert.Null(additionalFiles[0].Path);
Assert.Null(additionalFiles[0].GetText(CancellationToken.None));
var context = new CompilationAnalysisContext(compilation: null, options: new AnalyzerOptions(additionalFiles), reportDiagnostic: null, isSupportedDiagnostic: null, cancellationToken: CancellationToken.None);
Assert.Throws<ArgumentNullException>(() => analysisContext.CompilationAction(context));
await new CSharpTest
{
TestSources = { string.Empty },
SolutionTransforms =
{
// Run additional validation here with access to a Compilation
(solution, projectId) =>
{
var compilation = solution.GetProject(projectId).GetCompilationAsync(CancellationToken.None).GetAwaiter().GetResult();

var analysisContext = new AnalysisContextMissingOptions();
var analyzer = new SA0002InvalidSettingsFile();
analyzer.Initialize(analysisContext);
Assert.NotNull(analysisContext.CompilationAction);

var additionalFiles = ImmutableArray.Create<AdditionalText>(new InvalidAdditionalText());
Assert.Null(additionalFiles[0].Path);
Assert.Null(additionalFiles[0].GetText(CancellationToken.None));
var context = new CompilationAnalysisContext(compilation, options: new AnalyzerOptions(additionalFiles), reportDiagnostic: null, isSupportedDiagnostic: null, cancellationToken: CancellationToken.None);
Assert.Throws<ArgumentNullException>(() => analysisContext.CompilationAction(context));

return solution;
},
},
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

private class InvalidAdditionalText : AdditionalText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ public override void Initialize(AnalysisContext context)

private static void HandleCompilation(CompilationAnalysisContext context)
{
var firstSyntaxTree = context.Compilation.SyntaxTrees.FirstOrDefault();
if (firstSyntaxTree is null)
{
return;
}

try
{
SettingsHelper.GetStyleCopSettings(context.Options, context.Compilation?.SyntaxTrees.FirstOrDefault(), DeserializationFailureBehavior.ThrowException, context.CancellationToken);
SettingsHelper.GetStyleCopSettings(context.Options, firstSyntaxTree, DeserializationFailureBehavior.ThrowException, context.CancellationToken);
}
catch (Exception ex) when (ex is JsonParseException || ex is InvalidSettingsException)
{
Expand Down