Skip to content

Commit

Permalink
[.NET Analyzers] Fix Template special case (#7683)
Browse files Browse the repository at this point in the history
* [.NET Analyzers] Fix Template special case

The focus of these changes is to move the template
special case logic from an equality check to a
`StartsWith`, matching the behavior of the other
checks.
  • Loading branch information
jsquire authored Feb 14, 2024
1 parent d219dc6 commit 25f60c3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@ public class Program { }
}

[Fact]
public async Task AZC0001ProducedForSubNamespacesOfAzureTemplate()
public async Task AZC0001ProducedForInvalidNamespaceWithValidRoot()
{
const string code = @"
namespace Azure.Template.RandomNamespace
namespace Azure.StorageBadNamespace
{
public class Program { }
}";

var diagnostic = Verifier.Diagnostic("AZC0001")
.WithMessage(string.Format(this.message, "Azure.StorageBadNamespace"))
.WithSpan(2, 17, 2, 36);

await Verifier.VerifyAnalyzerAsync(code, diagnostic);
}

[Fact]
public async Task AZC0001ProducedForInvalidSubNamespaceWithValidRoot()
{
const string code = @"
namespace Azure.StorageBadNamespace.Child
{
public class Program { }
}";

var diagnostic = Verifier.Diagnostic("AZC0001")
.WithMessage(string.Format(this.message, "Azure.Template.RandomNamespace"))
.WithSpan(2, 26, 2, 41);
.WithMessage(string.Format(this.message, "Azure.StorageBadNamespace.Child"))
.WithSpan(2, 37, 2, 42);

await Verifier.VerifyAnalyzerAsync(code, diagnostic);
}
Expand Down Expand Up @@ -79,6 +95,17 @@ internal class Program { }
public async Task AZC0001NotProducedForAllowedNamespaces()
{
const string code = @"
namespace Azure.Storage
{
public class Program { }
}";
await Verifier.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task AZC0001NotProducedForAllowedSubNamespaces()
{
const string code = @"
namespace Azure.Storage.Hello
{
public class Program { }
Expand All @@ -90,6 +117,18 @@ public class Program { }
public async Task AZC0001NotProducedForAzureCoreExpressions()
{
const string code = @"
namespace Azure.Core.Expressions
{
public class Program { }
}";

await Verifier.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task AZC0001NotProducedForAzureCoreExpressionsSubNamespace()
{
const string code = @"
namespace Azure.Core.Expressions.Foobar
{
public class Program { }
Expand All @@ -99,10 +138,34 @@ public class Program { }
}

[Fact]
public async Task AZC0001NotProducedForAzureTemplate()
public async Task AZC0001NotProducedForSubNamespacesOfAzureTemplate()
{
const string code = @"
namespace Azure.Template.RandomNamespace
{
public class Program { }
}";

await Verifier.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task AZC0001NotProducedForAzureTemplateRoot()
{
const string code = @"
namespace Azure.Template
{
public class Program { }
}";

await Verifier.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task AZC0001NotProducedForAzureTemplateSubNamespace()
{
const string code = @"
namespace Azure.Template.Models
{
public class Program { }
}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ public override void Analyze(ISymbolAnalysisContext context)
var displayString = namespaceSymbol.ToDisplayString();
foreach (var prefix in AllowedNamespacePrefix)
{
if (displayString.StartsWith(prefix))
// Both the namespace itself or a sub-namespace are valid.
if (displayString == prefix || displayString.StartsWith(prefix + "."))
{
return;
}

// "Azure.Template" is not an approved namespace prefix, but we have a project template by that name
// to help customers get started. We do not want our template to include a suppression for this
// descriptor out of the box, so we need to treat it as a special case.
if (displayString == "Azure.Template")
if (displayString == "Azure.Template" || displayString.StartsWith("Azure.Template."))
{
return;
}
Expand Down

0 comments on commit 25f60c3

Please sign in to comment.