Skip to content

Commit 33faf3c

Browse files
committed
Add a SA0002 test for an exception not related to JSON deserialization
1 parent 91d82e3 commit 33faf3c

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33

44
namespace StyleCop.Analyzers.Test.SpecialRules
55
{
6+
extern alias system;
7+
8+
using System;
69
using System.Collections.Generic;
10+
using System.Collections.Immutable;
711
using System.Threading;
812
using System.Threading.Tasks;
913
using Analyzers.Settings;
1014
using Analyzers.SpecialRules;
15+
using Microsoft.CodeAnalysis;
1116
using Microsoft.CodeAnalysis.Diagnostics;
17+
using Microsoft.CodeAnalysis.Text;
1218
using TestHelper;
1319
using Xunit;
1420

21+
using ExcludeFromCodeCoverageAttribute = system::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute;
22+
1523
/// <summary>
1624
/// Unit tests for <see cref="SA0002InvalidSettingsFile"/>.
1725
/// </summary>
@@ -111,6 +119,21 @@ public async Task TestEmptySettingsAsync()
111119
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
112120
}
113121

122+
[Fact]
123+
public void TestUnexpectedExceptionNotCaught()
124+
{
125+
var analysisContext = new AnalysisContextMissingOptions();
126+
var analyzer = new SA0002InvalidSettingsFile();
127+
analyzer.Initialize(analysisContext);
128+
Assert.NotNull(analysisContext.CompilationAction);
129+
130+
var additionalFiles = ImmutableArray.Create<AdditionalText>(new InvalidAdditionalText());
131+
Assert.Null(additionalFiles[0].Path);
132+
Assert.Null(additionalFiles[0].GetText(CancellationToken.None));
133+
var context = new CompilationAnalysisContext(compilation: null, options: new AnalyzerOptions(additionalFiles), reportDiagnostic: null, isSupportedDiagnostic: null, cancellationToken: CancellationToken.None);
134+
Assert.Throws<NullReferenceException>(() => analysisContext.CompilationAction(context));
135+
}
136+
114137
/// <inheritdoc/>
115138
protected override string GetSettings()
116139
{
@@ -122,5 +145,73 @@ protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers(
122145
{
123146
yield return new SA0002InvalidSettingsFile();
124147
}
148+
149+
private class InvalidAdditionalText : AdditionalText
150+
{
151+
public override string Path => null;
152+
153+
public override SourceText GetText(CancellationToken cancellationToken) => null;
154+
}
155+
156+
/// <summary>
157+
/// This analysis context is used for testing the specific case where an exception occurs while evaluating the
158+
/// stylecop.json settings file, but it is not one of the JSON deserialization exceptions caused by this
159+
/// library's code.
160+
/// </summary>
161+
private class AnalysisContextMissingOptions : AnalysisContext
162+
{
163+
public Action<CompilationAnalysisContext> CompilationAction
164+
{
165+
get;
166+
private set;
167+
}
168+
169+
[ExcludeFromCodeCoverage]
170+
public override void RegisterCodeBlockAction(Action<CodeBlockAnalysisContext> action)
171+
{
172+
throw new NotImplementedException();
173+
}
174+
175+
[ExcludeFromCodeCoverage]
176+
public override void RegisterCodeBlockStartAction<TLanguageKindEnum>(Action<CodeBlockStartAnalysisContext<TLanguageKindEnum>> action)
177+
{
178+
throw new NotImplementedException();
179+
}
180+
181+
public override void RegisterCompilationAction(Action<CompilationAnalysisContext> action)
182+
{
183+
this.CompilationAction = action;
184+
}
185+
186+
[ExcludeFromCodeCoverage]
187+
public override void RegisterCompilationStartAction(Action<CompilationStartAnalysisContext> action)
188+
{
189+
throw new NotImplementedException();
190+
}
191+
192+
[ExcludeFromCodeCoverage]
193+
public override void RegisterSemanticModelAction(Action<SemanticModelAnalysisContext> action)
194+
{
195+
throw new NotImplementedException();
196+
}
197+
198+
[ExcludeFromCodeCoverage]
199+
public override void RegisterSymbolAction(Action<SymbolAnalysisContext> action, ImmutableArray<SymbolKind> symbolKinds)
200+
{
201+
throw new NotImplementedException();
202+
}
203+
204+
[ExcludeFromCodeCoverage]
205+
public override void RegisterSyntaxNodeAction<TLanguageKindEnum>(Action<SyntaxNodeAnalysisContext> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
206+
{
207+
throw new NotImplementedException();
208+
}
209+
210+
[ExcludeFromCodeCoverage]
211+
public override void RegisterSyntaxTreeAction(Action<SyntaxTreeAnalysisContext> action)
212+
{
213+
throw new NotImplementedException();
214+
}
215+
}
125216
}
126217
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.2.1\lib\net45\Microsoft.CodeAnalysis.Workspaces.Desktop.dll</HintPath>
6565
<Private>True</Private>
6666
</Reference>
67-
<Reference Include="System" />
67+
<Reference Include="System">
68+
<Aliases>global,system</Aliases>
69+
</Reference>
6870
<Reference Include="System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6971
<HintPath>..\..\packages\System.Collections.Immutable.1.1.37\lib\dotnet\System.Collections.Immutable.dll</HintPath>
7072
<Private>True</Private>

0 commit comments

Comments
 (0)