22"""Automatically create transform tests input and output files given an input template."""
33import argparse
44import json
5- import os
65import shutil
76import subprocess
87import sys
1716from samtranslator .translator .transform import transform
1817from samtranslator .yaml_helper import yaml_parse
1918
20- SCRIPT_DIR = os . path . dirname ( os . path . realpath ( __file__ ))
21- TRANSFORM_TEST_DIR = os . path . join ( SCRIPT_DIR , ".." , "tests" , "translator" )
19+ SCRIPT_DIR = Path ( __file__ ). parent
20+ TRANSFORM_TEST_DIR = SCRIPT_DIR . parent / "tests" / "translator"
2221
2322iam_client = boto3 .client ("iam" )
2423
4241CLI_OPTIONS = parser .parse_args ()
4342
4443
45- def read_json_file (file_path : str ) -> Dict [str , Any ]:
46- template : Dict [str , Any ] = json .loads (Path ( file_path ) .read_text (encoding = "utf-8" ))
44+ def read_json_file (file_path : Path ) -> Dict [str , Any ]:
45+ template : Dict [str , Any ] = json .loads (file_path .read_text (encoding = "utf-8" ))
4746 return template
4847
4948
50- def write_json_file (obj : Dict [str , Any ], file_path : str ) -> None :
51- with open (file_path , "w" , encoding = "utf-8" ) as f :
49+ def write_json_file (obj : Dict [str , Any ], file_path : Path ) -> None :
50+ with file_path . open ("w" , encoding = "utf-8" ) as f :
5251 json .dump (obj , f , indent = 2 , sort_keys = True )
5352
5453
@@ -64,24 +63,23 @@ def add_regional_endpoint_configuration_if_needed(template: Dict[str, Any]) -> D
6463 return template
6564
6665
67- def replace_aws_partition (partition : str , file_path : str ) -> None :
66+ def replace_aws_partition (partition : str , file_path : Path ) -> None :
6867 template = read_json_file (file_path )
69- with open (file_path , "w" ) as file :
70- updated_template = json .loads (json .dumps (template ).replace ("arn:aws:" , f"arn:{ partition } :" ))
71- file .write (json .dumps (updated_template , indent = 2 ))
68+ updated_template = json .loads (json .dumps (template ).replace ("arn:aws:" , f"arn:{ partition } :" ))
69+ file_path .write_text (json .dumps (updated_template , indent = 2 ), encoding = "utf-8" )
7270 print (f"Transform Test output files generated { file_path } " )
7371
7472
75- def generate_transform_test_output_files (input_file_path : str , file_basename : str ) -> None :
73+ def generate_transform_test_output_files (input_file_path : Path , file_basename : str ) -> None :
7674 output_file_option = file_basename + ".json"
7775
78- with open (os . path . join ( input_file_path ) ) as f :
76+ with input_file_path . open (encoding = "utf-8" ) as f :
7977 manifest = yaml_parse (f ) # type: ignore[no-untyped-call]
8078
8179 transform_test_output_paths = {
82- "aws" : ("us-west-2" , os . path . join ( TRANSFORM_TEST_DIR , "output" , output_file_option ) ),
83- "aws-cn" : ("cn-north-1 " , os . path . join ( TRANSFORM_TEST_DIR , "output/ aws-cn/" , output_file_option ) ),
84- "aws-us-gov" : ("us-gov-west-1" , os . path . join ( TRANSFORM_TEST_DIR , "output/ aws-us-gov/" , output_file_option ) ),
80+ "aws" : ("us-west-2" , TRANSFORM_TEST_DIR / "output" / output_file_option ),
81+ "aws-cn" : ("cn-north-1 " , TRANSFORM_TEST_DIR / "output" / " aws-cn" / output_file_option ),
82+ "aws-us-gov" : ("us-gov-west-1" , TRANSFORM_TEST_DIR / "output" / " aws-us-gov" / output_file_option ),
8583 }
8684
8785 for partition , (region , output_path ) in transform_test_output_paths .items ():
@@ -100,18 +98,18 @@ def generate_transform_test_output_files(input_file_path: str, file_basename: st
10098 replace_aws_partition (partition , output_path )
10199
102100
103- def get_input_file_path () -> str :
101+ def get_input_file_path () -> Path :
104102 input_file_option = str (CLI_OPTIONS .template_file )
105- return os . path . join ( os . getcwd (), input_file_option )
103+ return Path . cwd () / input_file_option
106104
107105
108- def copy_input_file_to_transform_test_dir (input_file_path : str , transform_test_input_path : str ) -> None :
106+ def copy_input_file_to_transform_test_dir (input_file_path : Path , transform_test_input_path : Path ) -> None :
109107 shutil .copyfile (input_file_path , transform_test_input_path )
110108 print (f"Transform Test input file generated { transform_test_input_path } " )
111109
112110
113- def verify_input_template (input_file_path : str ): # type: ignore[no-untyped-def]
114- if "arn:aws:" in Path ( input_file_path ) .read_text (encoding = "utf-8" ):
111+ def verify_input_template (input_file_path : Path ) -> None :
112+ if "arn:aws:" in input_file_path .read_text (encoding = "utf-8" ):
115113 print (
116114 "WARNING: hardcoded partition name detected. Consider replace it with pseudo parameter {AWS::Partition}" ,
117115 file = sys .stderr ,
@@ -120,23 +118,23 @@ def verify_input_template(input_file_path: str): # type: ignore[no-untyped-def]
120118
121119def format_test_files () -> None :
122120 subprocess .run (
123- [sys .executable , os . path . join ( SCRIPT_DIR , "json-format.py" ) , "--write" , "tests" ],
121+ [sys .executable , SCRIPT_DIR / "json-format.py" , "--write" , "tests" ],
124122 check = True ,
125123 )
126124
127125 subprocess .run (
128- [sys .executable , os . path . join ( SCRIPT_DIR , "yaml-format.py" ) , "--write" , "tests" ],
126+ [sys .executable , SCRIPT_DIR / "yaml-format.py" , "--write" , "tests" ],
129127 check = True ,
130128 )
131129
132130
133131def main () -> None :
134132 input_file_path = get_input_file_path ()
135- file_basename = Path ( input_file_path ) .stem
133+ file_basename = input_file_path .stem
136134
137135 verify_input_template (input_file_path )
138136
139- transform_test_input_path = os . path . join ( TRANSFORM_TEST_DIR , "input" , file_basename + ".yaml" )
137+ transform_test_input_path = TRANSFORM_TEST_DIR / "input" / ( file_basename + ".yaml" )
140138 copy_input_file_to_transform_test_dir (input_file_path , transform_test_input_path )
141139
142140 generate_transform_test_output_files (transform_test_input_path , file_basename )
0 commit comments