Skip to content

Commit f819f76

Browse files
File scoped namespace (#643)
Co-authored-by: Daniel Marbach <[email protected]>
1 parent cba2a43 commit f819f76

File tree

51 files changed

+2435
-2486
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2435
-2486
lines changed

src/NServiceBus.AzureFunctions.Worker.Analyzer.Tests/AnalyzerTestFixture.cs

Lines changed: 151 additions & 152 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests
2-
{
3-
using System.Threading.Tasks;
4-
using NUnit.Framework;
5-
using static AzureFunctionsDiagnostics;
1+
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests;
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
using static AzureFunctionsDiagnostics;
66

7-
[TestFixture]
8-
public class ConfigurationAnalyzerTests : AnalyzerTestFixture<ConfigurationAnalyzer>
7+
[TestFixture]
8+
public class ConfigurationAnalyzerTests : AnalyzerTestFixture<ConfigurationAnalyzer>
9+
{
10+
[TestCase("DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)", DefineCriticalErrorActionNotAllowedId)]
11+
[TestCase("LimitMessageProcessingConcurrencyTo(5)", LimitMessageProcessingToNotAllowedId)]
12+
[TestCase("MakeInstanceUniquelyAddressable(null)", MakeInstanceUniquelyAddressableNotAllowedId)]
13+
[TestCase("OverrideLocalAddress(null)", OverrideLocalAddressNotAllowedId)]
14+
[TestCase("PurgeOnStartup(true)", PurgeOnStartupNotAllowedId)]
15+
[TestCase("SetDiagnosticsPath(null)", SetDiagnosticsPathNotAllowedId)]
16+
[TestCase("UseTransport(new AzureServiceBusTransport(null, default(TopicTopology)))", UseTransportNotAllowedId)]
17+
public Task DiagnosticIsReportedForEndpointConfiguration(string configuration, string diagnosticId)
918
{
10-
[TestCase("DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)", DefineCriticalErrorActionNotAllowedId)]
11-
[TestCase("LimitMessageProcessingConcurrencyTo(5)", LimitMessageProcessingToNotAllowedId)]
12-
[TestCase("MakeInstanceUniquelyAddressable(null)", MakeInstanceUniquelyAddressableNotAllowedId)]
13-
[TestCase("OverrideLocalAddress(null)", OverrideLocalAddressNotAllowedId)]
14-
[TestCase("PurgeOnStartup(true)", PurgeOnStartupNotAllowedId)]
15-
[TestCase("SetDiagnosticsPath(null)", SetDiagnosticsPathNotAllowedId)]
16-
[TestCase("UseTransport(new AzureServiceBusTransport(null, default(TopicTopology)))", UseTransportNotAllowedId)]
17-
public Task DiagnosticIsReportedForEndpointConfiguration(string configuration, string diagnosticId)
18-
{
19-
var source =
20-
$@"using NServiceBus;
19+
var source =
20+
$@"using NServiceBus;
2121
using System;
2222
using System.Threading.Tasks;
2323
class Foo
@@ -31,20 +31,20 @@ void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
3131
}}
3232
}}";
3333

34-
return Assert(diagnosticId, source);
35-
}
34+
return Assert(diagnosticId, source);
35+
}
3636

37-
[TestCase("DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)", DefineCriticalErrorActionNotAllowedId)]
38-
[TestCase("LimitMessageProcessingConcurrencyTo(5)", LimitMessageProcessingToNotAllowedId)]
39-
[TestCase("MakeInstanceUniquelyAddressable(null)", MakeInstanceUniquelyAddressableNotAllowedId)]
40-
[TestCase("OverrideLocalAddress(null)", OverrideLocalAddressNotAllowedId)]
41-
[TestCase("PurgeOnStartup(true)", PurgeOnStartupNotAllowedId)]
42-
[TestCase("SetDiagnosticsPath(null)", SetDiagnosticsPathNotAllowedId)]
43-
[TestCase("UseTransport(new AzureServiceBusTransport(null, default(TopicTopology)))", UseTransportNotAllowedId)]
44-
public Task DiagnosticIsNotReportedForOtherEndpointConfiguration(string configuration, string diagnosticId)
45-
{
46-
var source =
47-
$@"using NServiceBus;
37+
[TestCase("DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)", DefineCriticalErrorActionNotAllowedId)]
38+
[TestCase("LimitMessageProcessingConcurrencyTo(5)", LimitMessageProcessingToNotAllowedId)]
39+
[TestCase("MakeInstanceUniquelyAddressable(null)", MakeInstanceUniquelyAddressableNotAllowedId)]
40+
[TestCase("OverrideLocalAddress(null)", OverrideLocalAddressNotAllowedId)]
41+
[TestCase("PurgeOnStartup(true)", PurgeOnStartupNotAllowedId)]
42+
[TestCase("SetDiagnosticsPath(null)", SetDiagnosticsPathNotAllowedId)]
43+
[TestCase("UseTransport(new AzureServiceBusTransport(null, default(TopicTopology)))", UseTransportNotAllowedId)]
44+
public Task DiagnosticIsNotReportedForOtherEndpointConfiguration(string configuration, string diagnosticId)
45+
{
46+
var source =
47+
$@"using NServiceBus;
4848
using System;
4949
using System.Threading;
5050
using System.Threading.Tasks;
@@ -68,7 +68,6 @@ void Bar(SomeOtherClass endpointConfig)
6868
}}
6969
}}";
7070

71-
return Assert(diagnosticId, source);
72-
}
71+
return Assert(diagnosticId, source);
7372
}
7473
}
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests
2-
{
3-
using System.Threading.Tasks;
4-
using Microsoft.CodeAnalysis.CSharp;
5-
using NUnit.Framework;
6-
using static AzureFunctionsDiagnostics;
1+
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests;
2+
3+
using System.Threading.Tasks;
4+
using Microsoft.CodeAnalysis.CSharp;
5+
using NUnit.Framework;
6+
using static AzureFunctionsDiagnostics;
77

8-
[TestFixture]
9-
public class ConfigurationAnalyzerTestsCSharp8 : AnalyzerTestFixture<ConfigurationAnalyzer>
8+
[TestFixture]
9+
public class ConfigurationAnalyzerTestsCSharp8 : AnalyzerTestFixture<ConfigurationAnalyzer>
10+
{
11+
// HINT: In C# 7 this call is ambiguous with the LearningTransport version as the compiler cannot differentiate method calls via generic type constraints
12+
[TestCase("UseTransport<AzureServiceBusTransport>(null)", UseTransportNotAllowedId)]
13+
public Task DiagnosticIsReportedForEndpointConfiguration(string configuration, string diagnosticId)
1014
{
11-
// HINT: In C# 7 this call is ambiguous with the LearningTransport version as the compiler cannot differentiate method calls via generic type constraints
12-
[TestCase("UseTransport<AzureServiceBusTransport>(null)", UseTransportNotAllowedId)]
13-
public Task DiagnosticIsReportedForEndpointConfiguration(string configuration, string diagnosticId)
14-
{
15-
var source =
16-
$@"using NServiceBus;
15+
var source =
16+
$@"using NServiceBus;
1717
using System;
1818
using System.Threading.Tasks;
1919
class Foo
@@ -27,8 +27,7 @@ void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
2727
}}
2828
}}";
2929

30-
return Assert(diagnosticId, source);
31-
}
32-
protected override LanguageVersion AnalyzerLanguageVersion => LanguageVersion.CSharp8;
30+
return Assert(diagnosticId, source);
3331
}
32+
protected override LanguageVersion AnalyzerLanguageVersion => LanguageVersion.CSharp8;
3433
}
Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1-
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests
1+
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.Immutable;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using Microsoft.CodeAnalysis;
12+
using Microsoft.CodeAnalysis.Diagnostics;
13+
14+
static class CompilationExtensions
215
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Collections.Immutable;
6-
using System.Diagnostics;
7-
using System.IO;
8-
using System.Linq;
9-
using System.Threading;
10-
using System.Threading.Tasks;
11-
using Microsoft.CodeAnalysis;
12-
using Microsoft.CodeAnalysis.Diagnostics;
13-
14-
static class CompilationExtensions
16+
public static void Compile(this Compilation compilation, bool throwOnFailure = true)
1517
{
16-
public static void Compile(this Compilation compilation, bool throwOnFailure = true)
18+
using (var peStream = new MemoryStream())
1719
{
18-
using (var peStream = new MemoryStream())
19-
{
20-
var emitResult = compilation.Emit(peStream);
20+
var emitResult = compilation.Emit(peStream);
2121

22-
if (!emitResult.Success)
22+
if (!emitResult.Success)
23+
{
24+
if (throwOnFailure)
2325
{
24-
if (throwOnFailure)
25-
{
26-
throw new Exception("Compilation failed.");
27-
}
28-
else
29-
{
30-
Debug.WriteLine("Compilation failed.");
31-
}
26+
throw new Exception("Compilation failed.");
27+
}
28+
else
29+
{
30+
Debug.WriteLine("Compilation failed.");
3231
}
3332
}
3433
}
34+
}
3535

36-
public static async Task<IEnumerable<Diagnostic>> GetAnalyzerDiagnostics(this Compilation compilation, DiagnosticAnalyzer analyzer, CancellationToken cancellationToken = default)
37-
{
38-
var exceptions = new List<Exception>();
39-
40-
var analysisOptions = new CompilationWithAnalyzersOptions(
41-
new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty),
42-
(exception, _, __) => exceptions.Add(exception),
43-
concurrentAnalysis: false,
44-
logAnalyzerExecutionTime: false);
36+
public static async Task<IEnumerable<Diagnostic>> GetAnalyzerDiagnostics(this Compilation compilation, DiagnosticAnalyzer analyzer, CancellationToken cancellationToken = default)
37+
{
38+
var exceptions = new List<Exception>();
4539

46-
var diagnostics = await compilation
47-
.WithAnalyzers(ImmutableArray.Create(analyzer), analysisOptions)
48-
.GetAnalyzerDiagnosticsAsync(cancellationToken);
40+
var analysisOptions = new CompilationWithAnalyzersOptions(
41+
new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty),
42+
(exception, _, __) => exceptions.Add(exception),
43+
concurrentAnalysis: false,
44+
logAnalyzerExecutionTime: false);
4945

50-
if (exceptions.Any())
51-
{
52-
throw new AggregateException(exceptions);
53-
}
46+
var diagnostics = await compilation
47+
.WithAnalyzers(ImmutableArray.Create(analyzer), analysisOptions)
48+
.GetAnalyzerDiagnosticsAsync(cancellationToken);
5449

55-
return diagnostics
56-
.OrderBy(diagnostic => diagnostic.Location.SourceSpan)
57-
.ThenBy(diagnostic => diagnostic.Id);
50+
if (exceptions.Any())
51+
{
52+
throw new AggregateException(exceptions);
5853
}
54+
55+
return diagnostics
56+
.OrderBy(diagnostic => diagnostic.Location.SourceSpan)
57+
.ThenBy(diagnostic => diagnostic.Id);
5958
}
60-
}
59+
}
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
1-
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests
1+
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests;
2+
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CodeActions;
9+
using Microsoft.CodeAnalysis.CodeFixes;
10+
using Microsoft.CodeAnalysis.Formatting;
11+
using Microsoft.CodeAnalysis.Simplification;
12+
13+
static class DocumentExtensions
214
{
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
7-
using Microsoft.CodeAnalysis;
8-
using Microsoft.CodeAnalysis.CodeActions;
9-
using Microsoft.CodeAnalysis.CodeFixes;
10-
using Microsoft.CodeAnalysis.Formatting;
11-
using Microsoft.CodeAnalysis.Simplification;
15+
public static async Task<IEnumerable<Diagnostic>> GetCompilerDiagnostics(this Document document, CancellationToken cancellationToken = default) =>
16+
(await document.GetSemanticModelAsync(cancellationToken))
17+
.GetDiagnostics(cancellationToken: cancellationToken)
18+
.Where(diagnostic => diagnostic.Severity != DiagnosticSeverity.Hidden)
19+
.OrderBy(diagnostic => diagnostic.Location.SourceSpan)
20+
.ThenBy(diagnostic => diagnostic.Id);
1221

13-
static class DocumentExtensions
22+
public static async Task<(Document Document, CodeAction Action)[]> GetCodeActions(this Project project, CodeFixProvider codeFix, Diagnostic diagnostic, CancellationToken cancellationToken = default)
1423
{
15-
public static async Task<IEnumerable<Diagnostic>> GetCompilerDiagnostics(this Document document, CancellationToken cancellationToken = default) =>
16-
(await document.GetSemanticModelAsync(cancellationToken))
17-
.GetDiagnostics(cancellationToken: cancellationToken)
18-
.Where(diagnostic => diagnostic.Severity != DiagnosticSeverity.Hidden)
19-
.OrderBy(diagnostic => diagnostic.Location.SourceSpan)
20-
.ThenBy(diagnostic => diagnostic.Id);
21-
22-
public static async Task<(Document Document, CodeAction Action)[]> GetCodeActions(this Project project, CodeFixProvider codeFix, Diagnostic diagnostic, CancellationToken cancellationToken = default)
24+
var actions = new List<(Document, CodeAction)>();
25+
foreach (var document in project.Documents)
2326
{
24-
var actions = new List<(Document, CodeAction)>();
25-
foreach (var document in project.Documents)
26-
{
27-
var context = new CodeFixContext(document, diagnostic, (action, _) => actions.Add((document, action)), cancellationToken);
28-
await codeFix.RegisterCodeFixesAsync(context);
29-
}
30-
return actions.ToArray();
27+
var context = new CodeFixContext(document, diagnostic, (action, _) => actions.Add((document, action)), cancellationToken);
28+
await codeFix.RegisterCodeFixesAsync(context);
3129
}
30+
return actions.ToArray();
31+
}
3232

33-
public static async Task<Document> ApplyChanges(this Document document, CodeAction codeAction, CancellationToken cancellationToken = default)
34-
{
35-
var operations = await codeAction.GetOperationsAsync(cancellationToken);
36-
var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
37-
return solution.GetDocument(document.Id);
38-
}
33+
public static async Task<Document> ApplyChanges(this Document document, CodeAction codeAction, CancellationToken cancellationToken = default)
34+
{
35+
var operations = await codeAction.GetOperationsAsync(cancellationToken);
36+
var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
37+
return solution.GetDocument(document.Id);
38+
}
3939

40-
public static async Task<string> GetCode(this Document document, CancellationToken cancellationToken = default)
41-
{
42-
var simplifiedDoc = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken);
43-
var root = await simplifiedDoc.GetSyntaxRootAsync(cancellationToken);
44-
root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace, cancellationToken: cancellationToken);
45-
return root.GetText().ToString();
46-
}
40+
public static async Task<string> GetCode(this Document document, CancellationToken cancellationToken = default)
41+
{
42+
var simplifiedDoc = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken);
43+
var root = await simplifiedDoc.GetSyntaxRootAsync(cancellationToken);
44+
root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace, cancellationToken: cancellationToken);
45+
return root.GetText().ToString();
4746
}
48-
}
47+
}
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests
2-
{
3-
using System.Threading.Tasks;
4-
using NUnit.Framework;
5-
using static AzureFunctionsDiagnostics;
1+
namespace NServiceBus.AzureFunctions.Worker.Analyzer.Tests;
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
using static AzureFunctionsDiagnostics;
66

7-
[TestFixture]
8-
public class OptionsAnalyzerTests : AnalyzerTestFixture<ConfigurationAnalyzer>
7+
[TestFixture]
8+
public class OptionsAnalyzerTests : AnalyzerTestFixture<ConfigurationAnalyzer>
9+
{
10+
[TestCase("SendOptions", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
11+
[TestCase("SendOptions", "RouteToThisInstance", RouteToThisInstanceNotAllowedId)]
12+
[TestCase("ReplyOptions", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
13+
public Task DiagnosticIsReportedForOptions(string optionsType, string method, string diagnosticId)
914
{
10-
[TestCase("SendOptions", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
11-
[TestCase("SendOptions", "RouteToThisInstance", RouteToThisInstanceNotAllowedId)]
12-
[TestCase("ReplyOptions", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
13-
public Task DiagnosticIsReportedForOptions(string optionsType, string method, string diagnosticId)
14-
{
15-
var source =
16-
$@"using NServiceBus;
15+
var source =
16+
$@"using NServiceBus;
1717
class Foo
1818
{{
1919
void Bar({optionsType} options)
@@ -22,15 +22,15 @@ void Bar({optionsType} options)
2222
}}
2323
}}";
2424

25-
return Assert(diagnosticId, source);
26-
}
25+
return Assert(diagnosticId, source);
26+
}
2727

28-
[TestCase("SomeOtherClass", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
29-
[TestCase("SomeOtherClass", "RouteToThisInstance", RouteToThisInstanceNotAllowedId)]
30-
public Task DiagnosticIsNotReportedForOtherOptions(string optionsType, string method, string diagnosticId)
31-
{
32-
var source =
33-
$@"using NServiceBus;
28+
[TestCase("SomeOtherClass", "RouteReplyToThisInstance", RouteReplyToThisInstanceNotAllowedId)]
29+
[TestCase("SomeOtherClass", "RouteToThisInstance", RouteToThisInstanceNotAllowedId)]
30+
public Task DiagnosticIsNotReportedForOtherOptions(string optionsType, string method, string diagnosticId)
31+
{
32+
var source =
33+
$@"using NServiceBus;
3434
using System;
3535
using System.Threading.Tasks;
3636
@@ -49,7 +49,6 @@ void Bar({optionsType} options)
4949
}}
5050
}}";
5151

52-
return Assert(diagnosticId, source);
53-
}
52+
return Assert(diagnosticId, source);
5453
}
5554
}

0 commit comments

Comments
 (0)