Skip to content

Commit 09075ff

Browse files
authored
Merge pull request #2186 from pdelvo/settingsnre
Fixes that the settings system crashes on an empty or missing settings file
2 parents 4457e16 + a279d6a commit 09075ff

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ public async Task VerifyInvalidJsonBehaviorAsync()
152152
Assert.Equal("Copyright (c) PlaceholderCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused"));
153153
}
154154

155+
[Fact]
156+
public async Task VerifyEmptyOrMissingFileAsync()
157+
{
158+
var settings = string.Empty;
159+
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
160+
161+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
162+
163+
// The result is the same as the default settings.
164+
Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName);
165+
Assert.Equal("Copyright (c) PlaceholderCompany. All rights reserved.", styleCopSettings.DocumentationRules.GetCopyrightText("unused"));
166+
}
167+
155168
private static async Task<SyntaxTreeAnalysisContext> CreateAnalysisContextAsync(string stylecopJSON)
156169
{
157170
var projectId = ProjectId.CreateNewId();

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ public async Task TestInvalidSettingsAsync()
5858
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
5959
}
6060

61+
[Fact]
62+
public async Task TestEmptySettingsAsync()
63+
{
64+
// The test infrastructure will not add a settings file to the compilation if GetSettings returns null or an empty string.
65+
// This is why we set settings to a simple whitespace character.
66+
this.settings = " ";
67+
68+
// This diagnostic is reported without a location
69+
DiagnosticResult expected = this.CSharpDiagnostic();
70+
71+
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
72+
}
73+
6174
/// <inheritdoc/>
6275
protected override string GetSettings()
6376
{

StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ private static StyleCopSettings GetStyleCopSettings(SourceText text, Deserializa
127127
try
128128
{
129129
var root = JsonConvert.DeserializeObject<SettingsFile>(text.ToString());
130+
131+
if (root == null)
132+
{
133+
throw new JsonException($"Settings file was missing or empty.");
134+
}
135+
130136
return root.Settings;
131137
}
132138
catch (JsonException) when (failureBehavior == DeserializationFailureBehavior.ReturnDefaultSettings)
@@ -160,6 +166,12 @@ private static StyleCopSettings GetStyleCopSettings(ImmutableArray<AdditionalTex
160166
{
161167
SourceText additionalTextContent = additionalFile.GetText(cancellationToken);
162168
var root = JsonConvert.DeserializeObject<SettingsFile>(additionalTextContent.ToString());
169+
170+
if (root == null)
171+
{
172+
throw new JsonException($"Settings file at '{Path.GetFileName(additionalFile.Path)}' was missing or empty.");
173+
}
174+
163175
return root.Settings;
164176
}
165177
}

0 commit comments

Comments
 (0)