2
2
"""Automatically create transform tests input and output files given an input template."""
3
3
import argparse
4
4
import json
5
- import os
6
5
import shutil
7
6
import subprocess
8
7
import sys
17
16
from samtranslator .translator .transform import transform
18
17
from samtranslator .yaml_helper import yaml_parse
19
18
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"
22
21
23
22
iam_client = boto3 .client ("iam" )
24
23
42
41
CLI_OPTIONS = parser .parse_args ()
43
42
44
43
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" ))
47
46
return template
48
47
49
48
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 :
52
51
json .dump (obj , f , indent = 2 , sort_keys = True )
53
52
54
53
@@ -64,24 +63,23 @@ def add_regional_endpoint_configuration_if_needed(template: Dict[str, Any]) -> D
64
63
return template
65
64
66
65
67
- def replace_aws_partition (partition : str , file_path : str ) -> None :
66
+ def replace_aws_partition (partition : str , file_path : Path ) -> None :
68
67
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" )
72
70
print (f"Transform Test output files generated { file_path } " )
73
71
74
72
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 :
76
74
output_file_option = file_basename + ".json"
77
75
78
- with open (os . path . join ( input_file_path ) ) as f :
76
+ with input_file_path . open (encoding = "utf-8" ) as f :
79
77
manifest = yaml_parse (f ) # type: ignore[no-untyped-call]
80
78
81
79
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 ),
85
83
}
86
84
87
85
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
100
98
replace_aws_partition (partition , output_path )
101
99
102
100
103
- def get_input_file_path () -> str :
101
+ def get_input_file_path () -> Path :
104
102
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
106
104
107
105
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 :
109
107
shutil .copyfile (input_file_path , transform_test_input_path )
110
108
print (f"Transform Test input file generated { transform_test_input_path } " )
111
109
112
110
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" ):
115
113
print (
116
114
"WARNING: hardcoded partition name detected. Consider replace it with pseudo parameter {AWS::Partition}" ,
117
115
file = sys .stderr ,
@@ -120,23 +118,23 @@ def verify_input_template(input_file_path: str): # type: ignore[no-untyped-def]
120
118
121
119
def format_test_files () -> None :
122
120
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" ],
124
122
check = True ,
125
123
)
126
124
127
125
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" ],
129
127
check = True ,
130
128
)
131
129
132
130
133
131
def main () -> None :
134
132
input_file_path = get_input_file_path ()
135
- file_basename = Path ( input_file_path ) .stem
133
+ file_basename = input_file_path .stem
136
134
137
135
verify_input_template (input_file_path )
138
136
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" )
140
138
copy_input_file_to_transform_test_dir (input_file_path , transform_test_input_path )
141
139
142
140
generate_transform_test_output_files (transform_test_input_path , file_basename )
0 commit comments