33
44namespace 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}
0 commit comments