Skip to content

Commit

Permalink
Use project specific name in devfile (#280)
Browse files Browse the repository at this point in the history
* Use unique project specific name in devfile

* Fix existing unit tests by mocking the behavior of new method

* Refactor logic for lines that were not covered by tests

* Add unit test for new method

* Code cleanup
  • Loading branch information
shatakshiiii authored Aug 22, 2024
1 parent f8f5a83 commit c8714db
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schemaVersion: 2.2.2
metadata:
name: ansible-demo
name: {{ dev_file_name }}
components:
- name: tooling-container
container:
Expand Down
17 changes: 17 additions & 0 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import shutil
import uuid

from pathlib import Path
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -110,13 +111,28 @@ def init_exists(self) -> None:
err = f"failed to remove existing directory {self._init_path}: {e}"
raise CreatorError(err) from e

def unique_name_in_devfile(self) -> str:
"""Use project specific name in devfile.
Returns:
Unique name entry.
"""
final_name: str
if self._project == "collection":
final_name = f"{self._namespace}.{self._collection_name}"
if self._project == "ansible-project":
final_name = f"{self._scm_org}.{self._scm_project}"
final_uuid = str(uuid.uuid4())[:8]
return f"{final_name}-{final_uuid}"

def _scaffold_collection(self) -> None:
"""Scaffold a collection project."""
self.output.debug(msg="started copying collection skeleton to destination")
template_data = TemplateData(
namespace=self._namespace,
collection_name=self._collection_name,
creator_version=self._creator_version,
dev_file_name=self.unique_name_in_devfile(),
)
copier = Copier(
resources=["collection_project", *self.common_resources],
Expand All @@ -141,6 +157,7 @@ def _scaffold_playbook(self: Init) -> None:
creator_version=self._creator_version,
scm_org=self._scm_org,
scm_project=self._scm_project,
dev_file_name=self.unique_name_in_devfile(),
)

copier = Copier(
Expand Down
2 changes: 2 additions & 0 deletions src/ansible_creator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TemplateData:
creator_version: The version of the creator.
dev_container_image: The devcontainer image.
dev_file_image: The devfile image.
dev_file_name: The unique name entry in devfile.
namespace: The namespace of the collection.
recommended_extensions: A list of recommended VsCode extensions.
scm_org: The organization of the source control management.
Expand All @@ -33,6 +34,7 @@ class TemplateData:
creator_version: str = ""
dev_container_image: Sequence[str] = GLOBAL_TEMPLATE_VARS["DEV_CONTAINER_IMAGE"]
dev_file_image: Sequence[str] = GLOBAL_TEMPLATE_VARS["DEV_FILE_IMAGE"]
dev_file_name: str = ""
namespace: str = ""
recommended_extensions: Sequence[str] = field(
default_factory=lambda: GLOBAL_TEMPLATE_VARS["RECOMMENDED_EXTENSIONS"],
Expand Down
2 changes: 2 additions & 0 deletions tests/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@


FIXTURES_DIR = (Path(__file__).parent / "fixtures").resolve()

UUID_LENGTH = 8
2 changes: 1 addition & 1 deletion tests/fixtures/collection/testorg/testcol/devfile.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schemaVersion: 2.2.2
metadata:
name: ansible-demo
name: testorg.testcol
components:
- name: tooling-container
container:
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/project/playbook_project/devfile.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schemaVersion: 2.2.2
metadata:
name: ansible-demo
name: weather.demo
components:
- name: tooling-container
container:
Expand Down
114 changes: 112 additions & 2 deletions tests/units/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ansible_creator.output import Output
from ansible_creator.subcommands.init import Init
from ansible_creator.utils import TermFeatures
from tests.defaults import FIXTURES_DIR
from tests.defaults import FIXTURES_DIR, UUID_LENGTH


class ConfigDict(TypedDict):
Expand Down Expand Up @@ -51,7 +51,7 @@ def fixture_cli_args(tmp_path: Path, output: Output) -> ConfigDict:
"""Create a dict to use for a Init class object as fixture.
Args:
tmp_path: App configuration object.
tmp_path: Temporary directory path.
output: Output class object.
Returns:
Expand Down Expand Up @@ -94,18 +94,34 @@ def test_run_success_for_collection(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
cli_args: ConfigDict,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test Init.run().
Args:
capsys: Pytest fixture to capture stdout and stderr.
tmp_path: Temporary directory path.
cli_args: Dictionary, partial Init class object.
monkeypatch: Pytest monkeypatch fixture.
"""
cli_args["project"] = "collection"
init = Init(
Config(**cli_args),
)

# Mock the "unique_name_in_devfile" method
def mock_unique_name_in_devfile(self: Init) -> str:
coll_namespace = self._namespace
coll_name = self._collection_name
return f"{coll_namespace}.{coll_name}"

# Apply the mock
monkeypatch.setattr(
Init,
"unique_name_in_devfile",
mock_unique_name_in_devfile,
)

init.run()
result = capsys.readouterr().out

Expand Down Expand Up @@ -140,6 +156,7 @@ def test_run_success_ansible_project(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
cli_args: ConfigDict,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test Init.run().
Expand All @@ -149,6 +166,7 @@ def test_run_success_ansible_project(
capsys: Pytest fixture to capture stdout and stderr.
tmp_path: Temporary directory path.
cli_args: Dictionary, partial Init class object.
monkeypatch: Pytest monkeypatch fixture.
"""
cli_args["collection"] = ""
cli_args["project"] = "ansible-project"
Expand All @@ -158,6 +176,20 @@ def test_run_success_ansible_project(
init = Init(
Config(**cli_args),
)

# Mock the "unique_name_in_devfile" method
def mock_unique_name_in_devfile(self: Init) -> str:
coll_namespace = self._scm_org
coll_name = self._scm_project
return f"{coll_namespace}.{coll_name}"

# Apply the mock
monkeypatch.setattr(
Init,
"unique_name_in_devfile",
mock_unique_name_in_devfile,
)

init.run()
result = capsys.readouterr().out

Expand Down Expand Up @@ -292,3 +324,81 @@ def test_is_file_error(tmp_path: Path) -> None:
with pytest.raises(CreatorError) as exc_info:
init.run()
assert "but is a file" in str(exc_info.value)


@pytest.fixture(name="cli_args_collection")
def fixture_collection_project(tmp_path: Path, output: Output) -> Config:
"""Fixture for Config object with collection project.
Args:
tmp_path: Temporary directory path.
output: Output class object.
Returns:
Config: Config class object.
"""
return Config(
subcommand="init",
namespace="testns",
collection_name="testname",
init_path=str(tmp_path / "test_path"),
force=False,
creator_version="1.0.0",
project="collection",
scm_org="",
scm_project="",
output=output,
)


@pytest.fixture(name="cli_args_playbook")
def fixture_playbook_project(tmp_path: Path, output: Output) -> Config:
"""Fixture for Config object with ansible-project.
Args:
tmp_path: Temporary directory path.
output: Output class object.
Returns:
Config: Config class object.
"""
return Config(
subcommand="init",
namespace="",
collection_name="",
init_path=str(tmp_path / "test_path"),
force=False,
creator_version="1.0.0",
project="ansible-project",
scm_org="foo",
scm_project="bar",
output=output,
)


def test_name_in_devfile_collection(cli_args_collection: Config) -> None:
"""Test unique_name_in_devfile method for collection project.
Args:
cli_args_collection: Configuration object for collection project.
"""
init = Init(cli_args_collection)
unique_name = init.unique_name_in_devfile()
assert unique_name.startswith("testns.testname-")
uuid_part = unique_name.split("-")[-1] # Extract the UUID part
assert len(uuid_part) == UUID_LENGTH, "UUID part length mismatch"


def test_name_in_devfile_playbook(
cli_args_playbook: Config,
) -> None:
"""Test unique_name_in_devfile method for playbook project.
Args:
cli_args_playbook: Configuration object for playbook project.
"""
init = Init(cli_args_playbook)
unique_name = init.unique_name_in_devfile()
assert unique_name.startswith("foo.bar-")
uuid_part = unique_name.split("-")[-1] # Extract the UUID part
assert len(uuid_part) == UUID_LENGTH, "UUID part length mismatch"

0 comments on commit c8714db

Please sign in to comment.