Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(analyzer): add rules regarding model name suffixes #6916

Merged
merged 21 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</PackageReference>
<PackageReference Include="System.Reflection.Metadata" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('net'))">
<PackageReference Include="System.Interactive.Async" Version="4.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,17 @@
using VerifyCS = Azure.ClientSdk.Analyzers.Tests.AzureAnalyzerVerifier<
Azure.ClientSdk.Analyzers.ModelName.GeneralSuffixAnalyzer>;

namespace Azure.ClientSdk.Analyzers.Test.ModelName
namespace Azure.ClientSdk.Analyzers.Tests.ModelName
{
public class AZC0030Tests
{
[Fact]
public async Task ClassNotUnderModelsNamespaceIsNotChecked()
{
var test = @"namespace Azure.ResourceManager;

class MonitorResult
{
}";
await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task OnlyModelsNamespaceIsChecked()
{
var test = @"namespace Azure.ResourceManager.AModels;

class MonitorResult
{
}";
await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task EnumIsNotChecked()
{
var test = @"namespace Azure.ResourceManager.Models;

enum MonitorResult
{
}";
await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task GoodSuffix()
{
var test = @"namespace Azure.ResourceManager.Models;
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Models;

class MonitorContent
public class MonitorContent
{
}";
await VerifyCS.VerifyAnalyzerAsync(test);
Expand All @@ -56,33 +24,44 @@ class MonitorContent
[Fact]
public async Task ParametersSuffix()
{
var test = @"namespace Azure.ResourceManager.Models
var test = @"using System.Text.Json;
namespace Azure.ResourceManager
{
public class ResponseParameters
{
public static ResponseParameters DeserializeResponseParameters(JsonElement element)
{
return null;
}
}
}";
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(3, 18, 3, 36).WithArguments("ResponseParameters", "Parameters", "'ResponseContent' or 'ResponsePatch'");
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(4, 18, 4, 36).WithArguments("ResponseParameters", "Parameters", "'ResponseContent' or 'ResponsePatch'");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task ResultSuffix()
public async Task RequestSuffix()
{
var test = @"namespace Azure.ResourceManager.Models
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Models
{
public class NetworkRequest
public class DiskOption
{
public static DiskOption DeserializeDiskOption(JsonElement element)
{
return null;
}
}
}";
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(3, 18, 3, 32).WithArguments("NetworkRequest", "Request", "'NetworkContent'");
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(4, 18, 4, 28).WithArguments("DiskOption", "Option", "'DiskConfig'");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task OptionSuffixWithNestedNameSpace()
{
var test = @"namespace Azure.ResourceManager.Models
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Models
{
namespace SubTest
{
Expand All @@ -91,23 +70,28 @@ public class DiskOption
}
}
}";
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(5, 22, 5, 32).WithArguments("DiskOption", "Option", "'DiskConfig'");
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(6, 22, 6, 32).WithArguments("DiskOption", "Option", "'DiskConfig'");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task ResponsesSuffix()
{
var test = @"namespace Azure.ResourceManager.Models
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Models
{
namespace SubTest
{
public class CreationResponses
{
public static CreationResponses DeserializeCreationResponses(JsonElement element)
{
return null;
}
}
}
}";
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(5, 22, 5, 39).WithArguments("CreationResponses", "Responses", "'CreationResults'");
var expected = VerifyCS.Diagnostic(GeneralSuffixAnalyzer.DiagnosticId).WithSpan(6, 22, 6, 39).WithArguments("CreationResponses", "Responses", "'CreationResults'");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AZC0031Tests
[Fact]
public async Task ModelWithDefinitionSuffix()
{
var test = @"
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Network.Models
{
public partial class AadAuthenticationDefinition
Expand All @@ -26,7 +26,7 @@ public partial class AadAuthenticationDefinition
[Fact]
public async Task ArmResourceIsNotChecked()
{
var test = @"
var test = @"using System.Text.Json;
using Azure.ResourceManager;
namespace Azure.ResourceManager
{
Expand All @@ -37,6 +37,10 @@ namespace Azure.ResourceManager.Network.Models
{
public partial class AadAuthenticationDefinition: ArmResource
{
public static AadAuthenticationDefinition DeserializeAadAuthenticationDefinition(JsonElement element)
{
return null;
}
}
}";
await VerifyCS.VerifyAnalyzerAsync(test);
Expand All @@ -45,7 +49,7 @@ public partial class AadAuthenticationDefinition: ArmResource
[Fact]
public async Task NotCheckIfRemovingSuffixIsAnotherType()
{
var test = @"
var test = @"using System.Text.Json;
using Azure.ResourceManager;
namespace Azure.ResourceManager.Network.Models
{
Expand All @@ -54,6 +58,10 @@ public class AadAuthentication {

public partial class AadAuthenticationDefinition
{
public static AadAuthenticationDefinition DeserializeAadAuthenticationDefinition(JsonElement element)
{
return null;
}
}
}";
await VerifyCS.VerifyAnalyzerAsync(test);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,18 @@ namespace Azure.ClientSdk.Analyzers.Tests.ModelName
{
public class AZC0032Tests
{
[Fact]
public async Task ClassUnderNonModelsNamespaceIsNotChecked()
{
var test = @"
namespace Azure.ResourceManager.Network.Temp
{
public partial class AadAuthenticationData
{
}
}";
await VerifyCS.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task ModelClassWithDataSuffix()
{
var test = @"
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Network.Models
{
public partial class AadAuthenticationData
{
public static AadAuthenticationData DeserializeAadAuthenticationData(JsonElement element)
{
return null;
}
}
}";
var expected = VerifyCS.Diagnostic(DataSuffixAnalyzer.DiagnosticId).WithSpan(4, 26, 4, 47).WithArguments("AadAuthenticationData", "Data");
Expand All @@ -39,14 +30,14 @@ public partial class AadAuthenticationData
[Fact]
public async Task ResourceDataClassesAreNotChecked()
{
var test = @"
var test = @"using System.Text.Json;
using Azure.ResourceManager.Models;
namespace Azure.ResourceManager.Models
{
public class ResourceData {
}
}
namespace Azure.ResourceManager.Network.Models
namespace Azure.ResourceManager.Models.Network
{
public partial class AadAuthenticationData: ResourceData
{
Expand All @@ -58,7 +49,7 @@ public partial class AadAuthenticationData: ResourceData
[Fact]
public async Task TrackedResourceDataClassesAreNotChecked()
{
var test = @"
var test = @"using System.Text.Json;
using Azure.ResourceManager.Models;
namespace Azure.ResourceManager.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
using VerifyCS = Azure.ClientSdk.Analyzers.Tests.AzureAnalyzerVerifier<
Azure.ClientSdk.Analyzers.ModelName.OperationSuffixAnalyzer>;

namespace Azure.ClientSdk.Analyzers.Test.ModelName
namespace Azure.ClientSdk.Analyzers.Tests.ModelName
{
public class AZC0033Tests
{
[Fact]
public async Task OperationClassIsNotChecked()
{
var test = @"
var test = @"using System.Text.Json;

using Azure;
using Azure.ResourceManager;
namespace Azure
Expand All @@ -36,16 +37,16 @@ public class ArmOperation<T> : Operation<T>
}
namespace Azure.ResourceManager.Network.Models
{
internal class DnsOperation : Operation
public class DnsOperation : Operation
{
}
internal class DnsArmOperation : ArmOperation
public class DnsArmOperation : ArmOperation
{
}
internal class DnsOperation<T> : Operation<T>
public class DnsOperation<T> : Operation<T>
{
}
internal class DnsArmOperation<T> : ArmOperation<T>
public class DnsArmOperation<T> : ArmOperation<T>
{
}
}";
Expand All @@ -55,19 +56,27 @@ internal class DnsArmOperation<T> : ArmOperation<T>
[Fact]
public async Task OperationSuffix()
{
var test = @"
namespace Azure.ResourceManager.Network.Models
var test = @"using System.Text.Json;
namespace Azure.ResourceManager.Network
{
public class DnsOperation
{
public static DnsOperation DeserializeDnsOperation(JsonElement element)
{
return null;
}
}
public class DnsArmOperation<T>
{
public static DnsArmOperation<T> DeserializeDnsArmOperation(JsonElement element)
{
return null;
}
}
}";
DiagnosticResult[] expected = {
VerifyCS.Diagnostic(OperationSuffixAnalyzer.DiagnosticId).WithSpan(4, 18, 4, 30).WithArguments("DnsOperation", "Operation", "DnsData", "DnsInfo"),
VerifyCS.Diagnostic(OperationSuffixAnalyzer.DiagnosticId).WithSpan(7, 18, 7, 33).WithArguments("DnsArmOperation", "Operation", "DnsArmData", "DnsArmInfo")
VerifyCS.Diagnostic(OperationSuffixAnalyzer.DiagnosticId).WithSpan(11, 18, 11, 33).WithArguments("DnsArmOperation", "Operation", "DnsArmData", "DnsArmInfo")
};
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
Expand Down
Loading