66import shutil
77import subprocess
88import sys
9- import tempfile
109from pathlib import Path
1110from typing import Any , Dict
1211
12+ import boto3
13+
14+ from samtranslator .translator .arn_generator import ArnGenerator
15+ from samtranslator .translator .managed_policy_translator import ManagedPolicyLoader
16+ from samtranslator .translator .transform import transform
17+ from samtranslator .yaml_helper import yaml_parse
18+
1319SCRIPT_DIR = os .path .dirname (os .path .realpath (__file__ ))
1420TRANSFORM_TEST_DIR = os .path .join (SCRIPT_DIR , ".." , "tests" , "translator" )
1521
22+ iam_client = boto3 .client ("iam" )
23+
1624parser = argparse .ArgumentParser (description = __doc__ )
1725parser .add_argument (
1826 "--template-file" ,
@@ -40,7 +48,7 @@ def read_json_file(file_path: str) -> Dict[str, Any]:
4048
4149def write_json_file (obj : Dict [str , Any ], file_path : str ) -> None :
4250 with open (file_path , "w" , encoding = "utf-8" ) as f :
43- json .dump (obj , f , indent = 2 )
51+ json .dump (obj , f , indent = 2 , sort_keys = True )
4452
4553
4654def add_regional_endpoint_configuration_if_needed (template : Dict [str , Any ]) -> Dict [str , Any ]:
@@ -66,38 +74,28 @@ def replace_aws_partition(partition: str, file_path: str) -> None:
6674def generate_transform_test_output_files (input_file_path : str , file_basename : str ) -> None :
6775 output_file_option = file_basename + ".json"
6876
69- # run sam-translate.py and get the temporary output file
70- with tempfile .NamedTemporaryFile () as temp_output_file :
71- subprocess .run (
72- [
73- sys .executable ,
74- os .path .join (SCRIPT_DIR , "sam-translate.py" ),
75- "--template-file" ,
76- input_file_path ,
77- "--output-template" ,
78- temp_output_file .name ,
79- ],
80- check = True ,
81- )
77+ with open (os .path .join (input_file_path ), "r" ) as f :
78+ manifest = yaml_parse (f ) # type: ignore[no-untyped-call]
79+
80+ transform_test_output_paths = {
81+ "aws" : ("us-west-2" , os .path .join (TRANSFORM_TEST_DIR , "output" , output_file_option )),
82+ "aws-cn" : ("cn-north-1 " , os .path .join (TRANSFORM_TEST_DIR , "output/aws-cn/" , output_file_option )),
83+ "aws-us-gov" : ("us-gov-west-1" , os .path .join (TRANSFORM_TEST_DIR , "output/aws-us-gov/" , output_file_option )),
84+ }
8285
83- # copy the output files into correct directories
84- transform_test_output_path = os .path .join (TRANSFORM_TEST_DIR , "output" , output_file_option )
85- shutil .copyfile (temp_output_file .name , transform_test_output_path )
86+ for partition , (region , output_path ) in transform_test_output_paths .items ():
87+ # Set Boto Session Region to guarantee the same hash input as transform tests for API deployment id
88+ ArnGenerator .BOTO_SESSION_REGION_NAME = region
89+ output_fragment = transform (manifest , {}, ManagedPolicyLoader (iam_client ))
8690
87- regional_transform_test_output_paths = {
88- "aws-cn" : os .path .join (TRANSFORM_TEST_DIR , "output/aws-cn/" , output_file_option ),
89- "aws-us-gov" : os .path .join (TRANSFORM_TEST_DIR , "output/aws-us-gov/" , output_file_option ),
90- }
91+ if not CLI_OPTIONS .disable_api_configuration and partition != "aws" :
92+ output_fragment = add_regional_endpoint_configuration_if_needed (output_fragment )
9193
92- if not CLI_OPTIONS .disable_api_configuration :
93- template = read_json_file (temp_output_file .name )
94- template = add_regional_endpoint_configuration_if_needed (template )
95- write_json_file (template , temp_output_file .name )
94+ write_json_file (output_fragment , output_path )
9695
97- for partition , output_path in regional_transform_test_output_paths .items ():
98- shutil .copyfile (temp_output_file .name , output_path )
99- if not CLI_OPTIONS .disable_update_partition :
100- replace_aws_partition (partition , output_path )
96+ # Update arn partition if necessary
97+ if not CLI_OPTIONS .disable_update_partition :
98+ replace_aws_partition (partition , output_path )
10199
102100
103101def get_input_file_path () -> str :
@@ -118,6 +116,18 @@ def verify_input_template(input_file_path: str): # type: ignore[no-untyped-def]
118116 )
119117
120118
119+ def format_test_files () -> None :
120+ subprocess .run (
121+ [sys .executable , os .path .join (SCRIPT_DIR , "json-format.py" ), "--write" , "tests" ],
122+ check = True ,
123+ )
124+
125+ subprocess .run (
126+ [sys .executable , os .path .join (SCRIPT_DIR , "yaml-format.py" ), "--write" , "tests" ],
127+ check = True ,
128+ )
129+
130+
121131def main () -> None :
122132 input_file_path = get_input_file_path ()
123133 file_basename = Path (input_file_path ).stem
@@ -133,6 +143,8 @@ def main() -> None:
133143 "Generating transform test input and output files complete. \n \n Please check the generated output is as expected. This tool does not guarantee correct output."
134144 )
135145
146+ format_test_files ()
147+
136148
137149if __name__ == "__main__" :
138150 main ()
0 commit comments