From b9927d9032a519a5c893a7b208387817bc025a9b Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 14 Oct 2024 13:30:35 +0800 Subject: [PATCH 1/4] raise error if automation cannot find tsp-location for typespec --- .../directory/examples_dir.py | 2 ++ .../directory/test_examples_dir.py | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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..f1f73f51fb3 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 @@ -46,5 +46,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..fad3386ffef 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,14 +5,13 @@ 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 +tsp_location_file_content = """directory: specification/mongocluster/DocumentDB.MongoCluster.Management commit: 7ed015e3dd1b8b1b0e71c9b5e6b6c5ccb8968b3a 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": { @@ -36,6 +35,7 @@ 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: @@ -82,6 +82,16 @@ def test_find_resource_manager_example_typespec(self): 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-03-01-preview", + "MongoClusters_ListConnectionStrings.json", + ) + def test_find_resource_manager_example_swagger(self): example_dir = try_find_resource_manager_example( "c:/github/azure-rest-api-specs", From e1e607367b4de2ab91d8f5e6f78edbdb5732eff3 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 14 Oct 2024 13:48:18 +0800 Subject: [PATCH 2/4] bug fix, picked example in wrong api-version folder --- .../directory/examples_dir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f1f73f51fb3..01d30540af6 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,7 @@ 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] From 78e918626d248314685b48f71c19b1429c0eecca Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 14 Oct 2024 13:51:40 +0800 Subject: [PATCH 3/4] test case for the fix --- .../directory/test_examples_dir.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) 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 fad3386ffef..869bfd6faae 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 @@ -6,7 +6,7 @@ tsp_location_file_content = """directory: specification/mongocluster/DocumentDB.MongoCluster.Management -commit: 7ed015e3dd1b8b1b0e71c9b5e6b6c5ccb8968b3a +commit: 07bdede4651ce2ea0e4039d76e81a69df23a3d6e repo: Azure/azure-rest-api-specs additionalDirectories: null """ @@ -18,7 +18,7 @@ "subscriptionId": "ffffffff-ffff-ffff-ffff-ffffffffffff", "resourceGroupName": "TestGroup", "mongoClusterName": "myMongoCluster", - "api-version": "2024-03-01-preview" + "api-version": "2024-07-01" }, "responses": { "200": { @@ -44,6 +44,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 +60,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,12 +91,12 @@ 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, ) @@ -88,7 +106,7 @@ def test_find_resource_manager_example_typespec_no_tsp_location(self): 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-03-01-preview", + "2024-07-01", "MongoClusters_ListConnectionStrings.json", ) From e8651b39e93bded4ceb72db230a487474128a233 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Mon, 14 Oct 2024 13:54:52 +0800 Subject: [PATCH 4/4] format --- .../directory/examples_dir.py | 4 +++- .../directory/test_examples_dir.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) 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 01d30540af6..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)}/**/examples/{example_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] 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 869bfd6faae..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 @@ -35,6 +35,7 @@ } """ + def create_mock_test_folder() -> tempfile.TemporaryDirectory: tmp_path = path.abspath(".") tmp_dir = tempfile.TemporaryDirectory(dir=tmp_path)