Skip to content

Commit 0e1551d

Browse files
authored
Refactor canary auto-generation cli semantics (#1085)
1 parent 9a27f91 commit 0e1551d

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

src/rpdk/core/generate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def generate(args):
2020
args.profile,
2121
)
2222
project.generate_docs()
23-
project.generate_canary_files()
23+
project.generate_canary_files(
24+
args.local_code_generation,
25+
)
2426
LOG.warning("Generated files for %s", project.type_name)
2527

2628

@@ -38,3 +40,8 @@ def setup_subparser(subparsers, parents):
3840
"--target-schemas", help="Path to target schemas.", nargs="*", default=[]
3941
)
4042
parser.add_argument("--profile", help="AWS profile to use.")
43+
parser.add_argument(
44+
"--local-code-generation",
45+
action="store_true",
46+
help="Enable local code generation.",
47+
)

src/rpdk/core/project.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ def __init__(self, overwrite_enabled=False, root=None):
179179
self.executable_entrypoint = None
180180
self.fragment_dir = None
181181
self.canary_settings = {}
182-
self.has_canary_settings = None
183182
self.target_info = {}
184183

185184
self.env = Environment(
@@ -256,7 +255,7 @@ def rpdk_config(self):
256255

257256
@property
258257
def file_generation_enabled(self):
259-
if self.has_canary_settings is False:
258+
if self.canary_settings == {}:
260259
return False
261260
return True
262261

@@ -339,10 +338,6 @@ def validate_and_load_resource_settings(self, raw_settings):
339338
self._plugin = load_plugin(raw_settings["language"])
340339
self.settings = raw_settings.get("settings", {})
341340
self.canary_settings = raw_settings.get("canarySettings", {})
342-
if raw_settings.get("canarySettings", False) is False:
343-
self.has_canary_settings = False
344-
else:
345-
self.has_canary_settings = True
346341

347342
def _write_example_schema(self):
348343
self.schema = resource_json(
@@ -1322,18 +1317,15 @@ def _load_target_info(
13221317

13231318
return type_info
13241319

1325-
def generate_canary_files(self) -> None:
1320+
def generate_canary_files(self, local_code_generation=False) -> None:
13261321
if (
13271322
not self.file_generation_enabled
13281323
or not Path(self.target_contract_test_folder_path).exists()
1324+
or not local_code_generation
13291325
):
13301326
LOG.info("Skipping Canary Auto-Generation")
13311327
return
13321328
LOG.info("Starting Canary Auto-Generation...")
1333-
if self.file_generation_enabled and self.canary_settings == {}:
1334-
LOG.warning(
1335-
"canarySettings are provided but empty. Generation is enabled with default settings."
1336-
)
13371329
self._setup_stack_template_environment()
13381330
self._generate_stack_template_files()
13391331
LOG.info("Finished Canary Auto-Generation")

tests/test_project.py

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ def test_generate_canary_files(project):
28052805

28062806
with patch_settings(project, data) as mock_open, patch_load as mock_load:
28072807
project.load_settings()
2808-
project.generate_canary_files()
2808+
project.generate_canary_files(local_code_generation=True)
28092809
mock_open.assert_called_once_with("r", encoding="utf-8")
28102810
mock_load.assert_called_once_with(LANGUAGE)
28112811
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
@@ -2858,7 +2858,7 @@ def test_create_template_file(mock_yaml_dump, project):
28582858

28592859
with patch_settings(project, data) as mock_open, patch_load as mock_load:
28602860
project.load_settings()
2861-
project.generate_canary_files()
2861+
project.generate_canary_files(local_code_generation=True)
28622862
mock_open.assert_called_once_with("r", encoding="utf-8")
28632863
mock_load.assert_called_once_with(LANGUAGE)
28642864
expected_template_data = {
@@ -2941,6 +2941,29 @@ def test_generate_canary_files_no_canary_settings(project):
29412941
}
29422942
tmp_path = project.root
29432943
setup_rpdk_config(project, rpdk_config)
2944+
project.generate_canary_files(local_code_generation=True)
2945+
2946+
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
2947+
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
2948+
assert not canary_root_path.exists()
2949+
assert not canary_folder_path.exists()
2950+
2951+
2952+
def test_generate_canary_files_no_local_code_generation(project):
2953+
rpdk_config = {
2954+
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
2955+
"language": LANGUAGE,
2956+
"runtime": RUNTIME,
2957+
"entrypoint": None,
2958+
"testEntrypoint": None,
2959+
"futureProperty": "value",
2960+
"typeName": "AWS::Example::Resource",
2961+
"canarySettings": {
2962+
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
2963+
},
2964+
}
2965+
tmp_path = project.root
2966+
setup_rpdk_config(project, rpdk_config)
29442967
project.generate_canary_files()
29452968

29462969
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
@@ -2949,6 +2972,29 @@ def test_generate_canary_files_no_canary_settings(project):
29492972
assert not canary_folder_path.exists()
29502973

29512974

2975+
def test_generate_canary_files_false_local_code_generation(project):
2976+
rpdk_config = {
2977+
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
2978+
"language": LANGUAGE,
2979+
"runtime": RUNTIME,
2980+
"entrypoint": None,
2981+
"testEntrypoint": None,
2982+
"futureProperty": "value",
2983+
"typeName": "AWS::Example::Resource",
2984+
"canarySettings": {
2985+
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
2986+
},
2987+
}
2988+
tmp_path = project.root
2989+
setup_rpdk_config(project, rpdk_config)
2990+
project.generate_canary_files(local_code_generation=False)
2991+
2992+
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
2993+
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
2994+
assert not canary_root_path.exists()
2995+
assert not canary_folder_path.exists()
2996+
2997+
29522998
def test_generate_canary_files_empty_input_files(project):
29532999
rpdk_config = {
29543000
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
@@ -2964,7 +3010,7 @@ def test_generate_canary_files_empty_input_files(project):
29643010
}
29653011
tmp_path = project.root
29663012
setup_rpdk_config(project, rpdk_config)
2967-
project.generate_canary_files()
3013+
project.generate_canary_files(local_code_generation=True)
29683014

29693015
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
29703016
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
@@ -2987,11 +3033,11 @@ def test_generate_canary_files_empty_canary_settings(project):
29873033
}
29883034
tmp_path = project.root
29893035
setup_rpdk_config(project, rpdk_config)
2990-
project.generate_canary_files()
3036+
project.generate_canary_files(local_code_generation=True)
29913037
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
29923038
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
2993-
assert canary_root_path.exists()
2994-
assert canary_folder_path.exists()
3039+
assert not canary_root_path.exists()
3040+
assert not canary_folder_path.exists()
29953041

29963042

29973043
def _get_mock_yaml_dump_call_arg(
@@ -3045,7 +3091,7 @@ def test_generate_canary_files_with_patch_inputs(mock_yaml_dump, project):
30453091

30463092
with patch_settings(project, data) as mock_open, patch_load as mock_load:
30473093
project.load_settings()
3048-
project.generate_canary_files()
3094+
project.generate_canary_files(local_code_generation=True)
30493095
mock_open.assert_called_once_with("r", encoding="utf-8")
30503096
mock_load.assert_called_once_with(LANGUAGE)
30513097
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
@@ -3125,7 +3171,7 @@ def test_create_template_file_with_patch_inputs(mock_yaml_dump, project):
31253171

31263172
with patch_settings(project, data) as mock_open, patch_load as mock_load:
31273173
project.load_settings()
3128-
project.generate_canary_files()
3174+
project.generate_canary_files(local_code_generation=True)
31293175
mock_open.assert_called_once_with("r", encoding="utf-8")
31303176
mock_load.assert_called_once_with(LANGUAGE)
31313177

@@ -3226,7 +3272,7 @@ def test_create_template_file_by_list_index(mock_yaml_dump, project):
32263272

32273273
with patch_settings(project, data) as mock_open, patch_load as mock_load:
32283274
project.load_settings()
3229-
project.generate_canary_files()
3275+
project.generate_canary_files(local_code_generation=True)
32303276
mock_open.assert_called_once_with("r", encoding="utf-8")
32313277
mock_load.assert_called_once_with(LANGUAGE)
32323278

@@ -3303,7 +3349,7 @@ def test_create_template_file_with_skipped_patch_operation(mock_yaml_dump, proje
33033349

33043350
with patch_settings(project, data) as mock_open, patch_load as mock_load:
33053351
project.load_settings()
3306-
project.generate_canary_files()
3352+
project.generate_canary_files(local_code_generation=True)
33073353
mock_open.assert_called_once_with("r", encoding="utf-8")
33083354
mock_load.assert_called_once_with(LANGUAGE)
33093355

@@ -3381,7 +3427,7 @@ def test_create_template_file_with_patch_inputs_missing_from_create(
33813427

33823428
with patch_settings(project, data) as mock_open, patch_load as mock_load:
33833429
project.load_settings()
3384-
project.generate_canary_files()
3430+
project.generate_canary_files(local_code_generation=True)
33853431
mock_open.assert_called_once_with("r", encoding="utf-8")
33863432
mock_load.assert_called_once_with(LANGUAGE)
33873433

@@ -3477,7 +3523,7 @@ def test_create_template_file_throws_error_with_invalid_path(mock_yaml_dump, pro
34773523
with patch_settings(project, data) as mock_open, patch_load as mock_load:
34783524
project.load_settings()
34793525
with pytest.raises(jsonpatch.JsonPointerException):
3480-
project.generate_canary_files()
3526+
project.generate_canary_files(local_code_generation=True)
34813527
mock_open.assert_called_once_with("r", encoding="utf-8")
34823528
mock_load.assert_called_once_with(LANGUAGE)
34833529

@@ -3531,7 +3577,7 @@ def test_create_template_file_with_nested_replace_patch_inputs(mock_yaml_dump, p
35313577

35323578
with patch_settings(project, data) as mock_open, patch_load as mock_load:
35333579
project.load_settings()
3534-
project.generate_canary_files()
3580+
project.generate_canary_files(local_code_generation=True)
35353581
mock_open.assert_called_once_with("r", encoding="utf-8")
35363582
mock_load.assert_called_once_with(LANGUAGE)
35373583

@@ -3636,7 +3682,7 @@ def test_create_template_file_with_nested_remove_patch_inputs(mock_yaml_dump, pr
36363682

36373683
with patch_settings(project, data) as mock_open, patch_load as mock_load:
36383684
project.load_settings()
3639-
project.generate_canary_files()
3685+
project.generate_canary_files(local_code_generation=True)
36403686
mock_open.assert_called_once_with("r", encoding="utf-8")
36413687
mock_load.assert_called_once_with(LANGUAGE)
36423688
expected_template_data = {
@@ -3734,7 +3780,7 @@ def test_create_template_file_with_nested_add_patch_inputs(mock_yaml_dump, proje
37343780

37353781
with patch_settings(project, data) as mock_open, patch_load as mock_load:
37363782
project.load_settings()
3737-
project.generate_canary_files()
3783+
project.generate_canary_files(local_code_generation=True)
37383784
mock_open.assert_called_once_with("r", encoding="utf-8")
37393785
mock_load.assert_called_once_with(LANGUAGE)
37403786

0 commit comments

Comments
 (0)