Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions tools/azure-rest-api-specs-examples-automation/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
23 changes: 20 additions & 3 deletions tools/azure-rest-api-specs-examples-automation/dotnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.

// <auto-generated/>

#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)