diff --git a/tools/azure-rest-api-specs-examples-automation/directory/examples_dir.py b/tools/azure-rest-api-specs-examples-automation/directory/examples_dir.py index ef37271d598..2ee702f436a 100644 --- a/tools/azure-rest-api-specs-examples-automation/directory/examples_dir.py +++ b/tools/azure-rest-api-specs-examples-automation/directory/examples_dir.py @@ -20,7 +20,9 @@ def try_find_resource_manager_example( if tsp_dir: # find example under directory # e.g. example_path = "specification/mongocluster/DocumentDB.MongoCluster.Management/examples/2024-03-01-preview/MongoClusters_ListConnectionStrings.json" - example_paths = glob.glob(f"{path.join(specs_path, tsp_dir)}/**/{example_filename}", recursive=True) + example_paths = glob.glob( + f"{path.join(specs_path, tsp_dir)}/**/examples/{example_dir}/{example_filename}", recursive=True + ) if len(example_paths) > 0: example_path = example_paths[0] @@ -46,5 +48,7 @@ def try_find_resource_manager_example( if len(candidate_resource_manager_filename) > 0: example_path, _ = path.split(candidate_resource_manager_filename[0]) example_dir = path.relpath(example_path, specs_path).replace("\\", "/") + else: + raise RuntimeError(f"tsp-location.yaml not found in SDK package folder {sdk_package_path}") return example_dir diff --git a/tools/azure-rest-api-specs-examples-automation/directory/test_examples_dir.py b/tools/azure-rest-api-specs-examples-automation/directory/test_examples_dir.py index b0f1049916b..ab67cc31c99 100644 --- a/tools/azure-rest-api-specs-examples-automation/directory/test_examples_dir.py +++ b/tools/azure-rest-api-specs-examples-automation/directory/test_examples_dir.py @@ -5,21 +5,20 @@ from examples_dir import try_find_resource_manager_example -def create_mock_test_folder() -> tempfile.TemporaryDirectory: - tsp_location_file_content = """directory: specification/mongocluster/DocumentDB.MongoCluster.Management -commit: 7ed015e3dd1b8b1b0e71c9b5e6b6c5ccb8968b3a +tsp_location_file_content = """directory: specification/mongocluster/DocumentDB.MongoCluster.Management +commit: 07bdede4651ce2ea0e4039d76e81a69df23a3d6e repo: Azure/azure-rest-api-specs additionalDirectories: null """ - json_example_file_content = """{ +json_example_file_content = """{ "operationId": "MongoClusters_ListConnectionStrings", "title": "List the available connection strings for the Mongo Cluster resource.", "parameters": { "subscriptionId": "ffffffff-ffff-ffff-ffff-ffffffffffff", "resourceGroupName": "TestGroup", "mongoClusterName": "myMongoCluster", - "api-version": "2024-03-01-preview" + "api-version": "2024-07-01" }, "responses": { "200": { @@ -36,6 +35,8 @@ def create_mock_test_folder() -> tempfile.TemporaryDirectory: } """ + +def create_mock_test_folder() -> tempfile.TemporaryDirectory: tmp_path = path.abspath(".") tmp_dir = tempfile.TemporaryDirectory(dir=tmp_path) try: @@ -44,6 +45,7 @@ def create_mock_test_folder() -> tempfile.TemporaryDirectory: with open(path.join(sdk_path, "tsp-location.yaml"), "w+", encoding="utf-8") as file: file.write(tsp_location_file_content) + # api-version 2024-03-01-preview specs_path = path.join( tmp_dir.name, "azure-rest-api-specs/specification/mongocluster/DocumentDB.MongoCluster.Management/examples/2024-03-01-preview", @@ -59,6 +61,23 @@ def create_mock_test_folder() -> tempfile.TemporaryDirectory: makedirs(specs_path) with open(path.join(specs_path, "MongoClusters_ListConnectionStrings.json"), "w+", encoding="utf-8") as file: file.write(json_example_file_content) + + # api-version 2024-07-01 + specs_path = path.join( + tmp_dir.name, + "azure-rest-api-specs/specification/mongocluster/DocumentDB.MongoCluster.Management/examples/2024-07-01", + ) + makedirs(specs_path) + with open(path.join(specs_path, "MongoClusters_ListConnectionStrings.json"), "w+", encoding="utf-8") as file: + file.write(json_example_file_content) + + specs_path = path.join( + tmp_dir.name, + "azure-rest-api-specs/specification/mongocluster/resource-manager/Microsoft.DocumentDB/preview/2024-07-01/examples", + ) + makedirs(specs_path) + with open(path.join(specs_path, "MongoClusters_ListConnectionStrings.json"), "w+", encoding="utf-8") as file: + file.write(json_example_file_content) except Exception as error: tmp_dir.cleanup() raise error @@ -73,15 +92,25 @@ def test_find_resource_manager_example_typespec(self): example_dir = try_find_resource_manager_example( path.join(tmp_dir_name, "azure-rest-api-specs"), path.join(tmp_dir_name, "azure-sdk-for-java/sdk/mongocluster/azure-resourcemanager-mongocluster"), - "2024-03-01-preview", + "2024-07-01", "MongoClusters_ListConnectionStrings.json", ) self.assertEqual( - "specification/mongocluster/resource-manager/Microsoft.DocumentDB/preview/2024-03-01-preview/examples", + "specification/mongocluster/resource-manager/Microsoft.DocumentDB/preview/2024-07-01/examples", example_dir, ) + def test_find_resource_manager_example_typespec_no_tsp_location(self): + mock_path = path.abspath(".") + with self.assertRaises(RuntimeError) as context: + example_dir = try_find_resource_manager_example( + path.join(mock_path, "azure-rest-api-specs"), + path.join(mock_path, "azure-sdk-for-java/sdk/mongocluster/azure-resourcemanager-mongocluster"), + "2024-07-01", + "MongoClusters_ListConnectionStrings.json", + ) + def test_find_resource_manager_example_swagger(self): example_dir = try_find_resource_manager_example( "c:/github/azure-rest-api-specs",