diff --git a/tools/azure-rest-api-specs-examples-automation/ci.yml b/tools/azure-rest-api-specs-examples-automation/ci.yml index a1b67023724..a369c639726 100644 --- a/tools/azure-rest-api-specs-examples-automation/ci.yml +++ b/tools/azure-rest-api-specs-examples-automation/ci.yml @@ -38,6 +38,11 @@ stages: - task: GoTool@0 inputs: version: $(GoVersion) + + - script: | + pip install parameterized + displayName: 'Install python unittest dependencies' + workingDirectory: ./tools/azure-rest-api-specs-examples-automation - script: | python -m unittest discover . diff --git a/tools/azure-rest-api-specs-examples-automation/dotnet/main.py b/tools/azure-rest-api-specs-examples-automation/dotnet/main.py index 8807bf748a3..20a3fd38360 100644 --- a/tools/azure-rest-api-specs-examples-automation/dotnet/main.py +++ b/tools/azure-rest-api-specs-examples-automation/dotnet/main.py @@ -81,15 +81,32 @@ def get_dotnet_example_method(lines: List[str], start: int) -> DotNetExampleMeth def get_dotnet_using_statements(lines: List[str]) -> List[str]: - lines_using_statements = [] + lines_using_statements = [ + # these are some using statements that every sample program should use. + "using Azure;\n", + "using Azure.ResourceManager;\n" + ] for line in lines: if line.startswith('using '): lines_using_statements.append(line) - elif line.startswith('namespace ') and not line.rstrip().endswith(".Samples"): + elif line.startswith('namespace '): + # remove the prefix first namespace = line[len('namespace '):].strip() + # remove the '.Samples' suffix if any + if namespace.endswith('.Samples'): + namespace = namespace[:-len('.Samples')] lines_using_statements.append(f'using {namespace};\n') break - return lines_using_statements + return deduplicate_list(lines_using_statements) + +def deduplicate_list(list: List[str]) -> List[str]: + seen = set() + result: List[str] = [] + for item in list: + if item not in seen: + seen.add(item) + result.append(item) + return result def break_down_aggregated_dotnet_example(lines: List[str]) -> AggregatedDotNetExample: diff --git a/tools/azure-rest-api-specs-examples-automation/dotnet/test_main.py b/tools/azure-rest-api-specs-examples-automation/dotnet/test_main.py index 8e83b8fe8ce..5c940a5f443 100644 --- a/tools/azure-rest-api-specs-examples-automation/dotnet/test_main.py +++ b/tools/azure-rest-api-specs-examples-automation/dotnet/test_main.py @@ -1,6 +1,6 @@ import unittest -from main import break_down_aggregated_dotnet_example, format_dotnet - +import parameterized +from main import break_down_aggregated_dotnet_example, format_dotnet, get_dotnet_using_statements file_content = '''// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. @@ -338,3 +338,34 @@ def test_break_down_aggregated_dotnet_example(self): example_lines = examples.class_opening + format_dotnet(dotnet_example_method.content) example_content = ''.join(example_lines) self.assertIsNotNone(example_content) + + @parameterized.parameterized.expand( + [ + ('''// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Identity; +using Azure.ResourceManager.Compute.Models; +using Azure.ResourceManager.Resources; +using Azure.ResourceManager.Resources.Models; + +namespace Azure.ResourceManager.Compute.Samples +{ +}''') + ] + ) + def test_example_usings(self, content: str): + lines = content.splitlines(keepends=True) + usings = get_dotnet_using_statements(lines) + + self.assertIn('using Azure;\n', usings) + self.assertIn('using Azure.Core;\n', usings) + self.assertIn('using Azure.ResourceManager;\n', usings) + self.assertIn('using Azure.ResourceManager.Compute;\n', usings)