Skip to content

Commit 5f36715

Browse files
fix the using issue in dotnet examples (#8664)
1 parent 483f731 commit 5f36715

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

tools/azure-rest-api-specs-examples-automation/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ stages:
3838
- task: GoTool@0
3939
inputs:
4040
version: $(GoVersion)
41+
42+
- script: |
43+
pip install parameterized
44+
displayName: 'Install python unittest dependencies'
45+
workingDirectory: ./tools/azure-rest-api-specs-examples-automation
4146
4247
- script: |
4348
python -m unittest discover .

tools/azure-rest-api-specs-examples-automation/dotnet/main.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,32 @@ def get_dotnet_example_method(lines: List[str], start: int) -> DotNetExampleMeth
8181

8282

8383
def get_dotnet_using_statements(lines: List[str]) -> List[str]:
84-
lines_using_statements = []
84+
lines_using_statements = [
85+
# these are some using statements that every sample program should use.
86+
"using Azure;\n",
87+
"using Azure.ResourceManager;\n"
88+
]
8589
for line in lines:
8690
if line.startswith('using '):
8791
lines_using_statements.append(line)
88-
elif line.startswith('namespace ') and not line.rstrip().endswith(".Samples"):
92+
elif line.startswith('namespace '):
93+
# remove the prefix first
8994
namespace = line[len('namespace '):].strip()
95+
# remove the '.Samples' suffix if any
96+
if namespace.endswith('.Samples'):
97+
namespace = namespace[:-len('.Samples')]
9098
lines_using_statements.append(f'using {namespace};\n')
9199
break
92-
return lines_using_statements
100+
return deduplicate_list(lines_using_statements)
101+
102+
def deduplicate_list(list: List[str]) -> List[str]:
103+
seen = set()
104+
result: List[str] = []
105+
for item in list:
106+
if item not in seen:
107+
seen.add(item)
108+
result.append(item)
109+
return result
93110

94111

95112
def break_down_aggregated_dotnet_example(lines: List[str]) -> AggregatedDotNetExample:

tools/azure-rest-api-specs-examples-automation/dotnet/test_main.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
2-
from main import break_down_aggregated_dotnet_example, format_dotnet
3-
2+
import parameterized
3+
from main import break_down_aggregated_dotnet_example, format_dotnet, get_dotnet_using_statements
44

55
file_content = '''// Copyright (c) Microsoft Corporation. All rights reserved.
66
// Licensed under the MIT License.
@@ -338,3 +338,34 @@ def test_break_down_aggregated_dotnet_example(self):
338338
example_lines = examples.class_opening + format_dotnet(dotnet_example_method.content)
339339
example_content = ''.join(example_lines)
340340
self.assertIsNotNone(example_content)
341+
342+
@parameterized.parameterized.expand(
343+
[
344+
('''// Copyright (c) Microsoft Corporation. All rights reserved.
345+
// Licensed under the MIT License.
346+
347+
// <auto-generated/>
348+
349+
#nullable disable
350+
351+
using System;
352+
using System.Threading.Tasks;
353+
using Azure.Core;
354+
using Azure.Identity;
355+
using Azure.ResourceManager.Compute.Models;
356+
using Azure.ResourceManager.Resources;
357+
using Azure.ResourceManager.Resources.Models;
358+
359+
namespace Azure.ResourceManager.Compute.Samples
360+
{
361+
}''')
362+
]
363+
)
364+
def test_example_usings(self, content: str):
365+
lines = content.splitlines(keepends=True)
366+
usings = get_dotnet_using_statements(lines)
367+
368+
self.assertIn('using Azure;\n', usings)
369+
self.assertIn('using Azure.Core;\n', usings)
370+
self.assertIn('using Azure.ResourceManager;\n', usings)
371+
self.assertIn('using Azure.ResourceManager.Compute;\n', usings)

0 commit comments

Comments
 (0)