From 1c3946418761a709da7cdaf33a8c368b70d0f314 Mon Sep 17 00:00:00 2001 From: tanwigeetika1618 Date: Mon, 11 Nov 2024 10:44:05 +0530 Subject: [PATCH 1/2] Add support to scaffold devcontainer --- src/ansible_creator/arg_parser.py | 3 ++- src/ansible_creator/subcommands/add.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index c026da9..b4dc702 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -35,7 +35,7 @@ MIN_COLLECTION_NAME_LEN = 2 COMING_SOON = ( - "add resource devcontainer", + # "add resource devcontainer", "add resource role", "add plugin action", "add plugin filter", @@ -257,6 +257,7 @@ def _add_resource_devcontainer(self: Parser, subparser: SubParser[ArgumentParser "current working directory.", ) + self._add_overwrite(parser) self._add_args_common(parser) def _add_resource_devfile(self: Parser, subparser: SubParser[ArgumentParser]) -> None: diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 424c741..162f83e 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -79,6 +79,8 @@ def _scaffold(self) -> None: # Call the appropriate scaffolding function based on the resource type if self._resource_type == "devfile": template_data = self._get_devfile_template_data() + elif self._resource_type == "devcontainer": + template_data = self._get_devcontainer_template_data() else: @@ -148,3 +150,15 @@ def _get_devfile_template_data(self) -> TemplateData: creator_version=self._creator_version, dev_file_name=self.unique_name_in_devfile(), ) + + def _get_devcontainer_template_data(self) -> TemplateData: + """Get the template data for devcontainer resources. + + Returns: + TemplateData: Data required for templating the devcontainer resource. + """ + return TemplateData( + resource_type=self._resource_type, + creator_version=self._creator_version, + dev_file_name=self.unique_name_in_devfile(), + ) From 6ce6e913c8a14fd57397b5bc6377c0d6a93a648c Mon Sep 17 00:00:00 2001 From: tanwigeetika1618 Date: Tue, 12 Nov 2024 09:54:14 +0530 Subject: [PATCH 2/2] Add test coverage for devcontainer resource in test_add.py --- tests/units/test_add.py | 65 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/units/test_add.py b/tests/units/test_add.py index d989008..3536480 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -153,7 +153,7 @@ def mock_unique_name_in_devfile() -> str: ): add.run() - # expect a warning followed by playbook project creation msg + # expect a warning followed by devfile resource creation msg # when response to overwrite is yes. monkeypatch.setattr("builtins.input", lambda _: "y") add.run() @@ -266,3 +266,66 @@ def test_run_error_unsupported_resource_type( with pytest.raises(CreatorError) as exc_info: add.run() assert "Unsupported resource type: unsupported_type" in str(exc_info.value) + + +def test_run_success_add_devcontainer( + capsys: pytest.CaptureFixture[str], + tmp_path: Path, + cli_args: ConfigDict, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Test Add.run() for adding a devcontainer. + + Successfully adds devcontainer to path. + + Args: + capsys: Pytest fixture to capture stdout and stderr. + tmp_path: Temporary directory path. + cli_args: Dictionary, partial Add class object. + monkeypatch: Pytest monkeypatch fixture. + """ + # Set the resource_type to devcontainer + cli_args["resource_type"] = "devcontainer" + add = Add( + Config(**cli_args), + ) + add.run() + result = capsys.readouterr().out + assert re.search("Note: Resource added to", result) is not None + + # Verify the generated devcontainer files match the expected structure + expected_devcontainer = tmp_path / ".devcontainer" + effective_devcontainer = FIXTURES_DIR / "collection" / "testorg" / "testcol" / ".devcontainer" + cmp_result = dircmp(expected_devcontainer, effective_devcontainer) + diff = has_differences(dcmp=cmp_result, errors=[]) + assert diff == [], diff + + # Test for overwrite prompt and failure with no overwrite option + conflict_file = tmp_path / ".devcontainer" / "devcontainer.json" + conflict_file.write_text('{ "name": "conflict" }') + + # expect a CreatorError when the response to overwrite is no. + monkeypatch.setattr("builtins.input", lambda _: "n") + fail_msg = ( + "The destination directory contains files that will be overwritten." + " Please re-run ansible-creator with --overwrite to continue." + ) + with pytest.raises( + CreatorError, + match=fail_msg, + ): + add.run() + + # expect a warning followed by devcontainer resource creation msg + # when response to overwrite is yes. + monkeypatch.setattr("builtins.input", lambda _: "y") + add.run() + result = capsys.readouterr().out + assert ( + re.search( + "already exists", + result, + ) + is not None + ), result + assert re.search("Note: Resource added to", result) is not None