Skip to content

Commit

Permalink
Refactor canary auto-generation cli semantics (#1085)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-1010 authored Aug 21, 2024
1 parent 9a27f91 commit 0e1551d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
9 changes: 8 additions & 1 deletion src/rpdk/core/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def generate(args):
args.profile,
)
project.generate_docs()
project.generate_canary_files()
project.generate_canary_files(
args.local_code_generation,
)
LOG.warning("Generated files for %s", project.type_name)


Expand All @@ -38,3 +40,8 @@ def setup_subparser(subparsers, parents):
"--target-schemas", help="Path to target schemas.", nargs="*", default=[]
)
parser.add_argument("--profile", help="AWS profile to use.")
parser.add_argument(
"--local-code-generation",
action="store_true",
help="Enable local code generation.",
)
14 changes: 3 additions & 11 deletions src/rpdk/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def __init__(self, overwrite_enabled=False, root=None):
self.executable_entrypoint = None
self.fragment_dir = None
self.canary_settings = {}
self.has_canary_settings = None
self.target_info = {}

self.env = Environment(
Expand Down Expand Up @@ -256,7 +255,7 @@ def rpdk_config(self):

@property
def file_generation_enabled(self):
if self.has_canary_settings is False:
if self.canary_settings == {}:
return False
return True

Expand Down Expand Up @@ -339,10 +338,6 @@ def validate_and_load_resource_settings(self, raw_settings):
self._plugin = load_plugin(raw_settings["language"])
self.settings = raw_settings.get("settings", {})
self.canary_settings = raw_settings.get("canarySettings", {})
if raw_settings.get("canarySettings", False) is False:
self.has_canary_settings = False
else:
self.has_canary_settings = True

def _write_example_schema(self):
self.schema = resource_json(
Expand Down Expand Up @@ -1322,18 +1317,15 @@ def _load_target_info(

return type_info

def generate_canary_files(self) -> None:
def generate_canary_files(self, local_code_generation=False) -> None:
if (
not self.file_generation_enabled
or not Path(self.target_contract_test_folder_path).exists()
or not local_code_generation
):
LOG.info("Skipping Canary Auto-Generation")
return
LOG.info("Starting Canary Auto-Generation...")
if self.file_generation_enabled and self.canary_settings == {}:
LOG.warning(
"canarySettings are provided but empty. Generation is enabled with default settings."
)
self._setup_stack_template_environment()
self._generate_stack_template_files()
LOG.info("Finished Canary Auto-Generation")
Expand Down
76 changes: 61 additions & 15 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,7 +2805,7 @@ def test_generate_canary_files(project):

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
Expand Down Expand Up @@ -2858,7 +2858,7 @@ def test_create_template_file(mock_yaml_dump, project):

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)
expected_template_data = {
Expand Down Expand Up @@ -2941,6 +2941,29 @@ def test_generate_canary_files_no_canary_settings(project):
}
tmp_path = project.root
setup_rpdk_config(project, rpdk_config)
project.generate_canary_files(local_code_generation=True)

canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
assert not canary_root_path.exists()
assert not canary_folder_path.exists()


def test_generate_canary_files_no_local_code_generation(project):
rpdk_config = {
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
"language": LANGUAGE,
"runtime": RUNTIME,
"entrypoint": None,
"testEntrypoint": None,
"futureProperty": "value",
"typeName": "AWS::Example::Resource",
"canarySettings": {
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
},
}
tmp_path = project.root
setup_rpdk_config(project, rpdk_config)
project.generate_canary_files()

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


def test_generate_canary_files_false_local_code_generation(project):
rpdk_config = {
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
"language": LANGUAGE,
"runtime": RUNTIME,
"entrypoint": None,
"testEntrypoint": None,
"futureProperty": "value",
"typeName": "AWS::Example::Resource",
"canarySettings": {
CONTRACT_TEST_FILE_NAMES: ["inputs_1.json", "inputs_2.json"],
},
}
tmp_path = project.root
setup_rpdk_config(project, rpdk_config)
project.generate_canary_files(local_code_generation=False)

canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
assert not canary_root_path.exists()
assert not canary_folder_path.exists()


def test_generate_canary_files_empty_input_files(project):
rpdk_config = {
ARTIFACT_TYPE_RESOURCE: "RESOURCE",
Expand All @@ -2964,7 +3010,7 @@ def test_generate_canary_files_empty_input_files(project):
}
tmp_path = project.root
setup_rpdk_config(project, rpdk_config)
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)

canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
Expand All @@ -2987,11 +3033,11 @@ def test_generate_canary_files_empty_canary_settings(project):
}
tmp_path = project.root
setup_rpdk_config(project, rpdk_config)
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
canary_folder_path = tmp_path / TARGET_CANARY_FOLDER
assert canary_root_path.exists()
assert canary_folder_path.exists()
assert not canary_root_path.exists()
assert not canary_folder_path.exists()


def _get_mock_yaml_dump_call_arg(
Expand Down Expand Up @@ -3045,7 +3091,7 @@ def test_generate_canary_files_with_patch_inputs(mock_yaml_dump, project):

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)
canary_root_path = tmp_path / TARGET_CANARY_ROOT_FOLDER
Expand Down Expand Up @@ -3125,7 +3171,7 @@ def test_create_template_file_with_patch_inputs(mock_yaml_dump, project):

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3226,7 +3272,7 @@ def test_create_template_file_by_list_index(mock_yaml_dump, project):

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3303,7 +3349,7 @@ def test_create_template_file_with_skipped_patch_operation(mock_yaml_dump, proje

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3381,7 +3427,7 @@ def test_create_template_file_with_patch_inputs_missing_from_create(

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3477,7 +3523,7 @@ def test_create_template_file_throws_error_with_invalid_path(mock_yaml_dump, pro
with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
with pytest.raises(jsonpatch.JsonPointerException):
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3531,7 +3577,7 @@ def test_create_template_file_with_nested_replace_patch_inputs(mock_yaml_dump, p

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down Expand Up @@ -3636,7 +3682,7 @@ def test_create_template_file_with_nested_remove_patch_inputs(mock_yaml_dump, pr

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)
expected_template_data = {
Expand Down Expand Up @@ -3734,7 +3780,7 @@ def test_create_template_file_with_nested_add_patch_inputs(mock_yaml_dump, proje

with patch_settings(project, data) as mock_open, patch_load as mock_load:
project.load_settings()
project.generate_canary_files()
project.generate_canary_files(local_code_generation=True)
mock_open.assert_called_once_with("r", encoding="utf-8")
mock_load.assert_called_once_with(LANGUAGE)

Expand Down

0 comments on commit 0e1551d

Please sign in to comment.