Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to scaffold devcontainer #320

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ansible_creator/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -257,6 +257,7 @@ def _add_resource_devcontainer(self, subparser: SubParser[ArgumentParser]) -> No
"current working directory.",
)

self._add_overwrite(parser)
self._add_args_common(parser)

def _add_resource_devfile(self, subparser: SubParser[ArgumentParser]) -> None:
Expand Down
14 changes: 14 additions & 0 deletions src/ansible_creator/subcommands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
msg = f"Unsupported resource type: {self._resource_type}"
Expand Down Expand Up @@ -147,3 +149,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(),
)
65 changes: 64 additions & 1 deletion tests/units/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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