Skip to content

Commit

Permalink
update init-path behaviour (#118)
Browse files Browse the repository at this point in the history
* update init-path behaviour

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* debug

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* update init-path behaviour

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix - 2

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix - 2

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix - 3

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix - 4

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* maybe a fix - 5

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* check stderr

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* hopefully works

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* hopefully works #2

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* hopefully works #3

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* hopefully works #4

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* add a comment on what was done

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Bump mkdocs-material to 9.5.17

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

---------

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>
  • Loading branch information
NilashishC authored Apr 3, 2024
1 parent d75f028 commit 8178ec7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .config/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mkdocs-ansible==24.2.1
mkdocs-autorefs==1.0.1
mkdocs-gen-files==0.5.0
mkdocs-htmlproofer-plugin==1.2.0
mkdocs-material==9.5.13
mkdocs-material==9.5.17
mkdocs-material-extensions==1.3.1
mkdocs-minify-plugin==0.8.0
mkdocs-monorepo-plugin==1.1.0
Expand Down
29 changes: 17 additions & 12 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,34 @@ def run(self: Init) -> None:
:raises CreatorError: if computed collection path is an existing directory or file.
"""
col_path = os.path.join(self._init_path, self._namespace, self._collection_name)
if self._init_path.endswith("collections/ansible_collections"):
self._init_path = os.path.join(
self._init_path,
self._namespace,
self._collection_name,
)

self.output.debug(msg=f"final collection path set to {col_path}")
self.output.debug(msg=f"final collection path set to {self._init_path}")

# check if init_path already exists
if os.path.exists(col_path):
if os.path.isfile(col_path):
msg = f"the path {col_path} already exists, but is a file - aborting"
if os.path.exists(self._init_path):
if os.path.isfile(self._init_path):
msg = f"the path {self._init_path} already exists, but is a file - aborting"
raise CreatorError(
msg,
)

if not self._force:
msg = (
f"The directory {col_path} already exists.\n"
f"The directory {self._init_path} already exists.\n"
f"You can use --force to re-initialize this directory."
f"\nHowever it will delete ALL existing contents in it."
)
raise CreatorError(msg)

# user requested --force, re-initializing existing directory
self.output.warning(f"re-initializing existing directory {col_path}")
for root, dirs, files in os.walk(col_path, topdown=True):
self.output.warning(f"re-initializing existing directory {self._init_path}")
for root, dirs, files in os.walk(self._init_path, topdown=True):
for old_dir in dirs:
path = os.path.join(root, old_dir)
self.output.debug(f"removing tree {old_dir}")
Expand All @@ -76,15 +81,15 @@ def run(self: Init) -> None:
os.unlink(path)

# if init_path does not exist, create it
if not os.path.exists(col_path):
self.output.debug(msg=f"creating new directory at {col_path}")
os.makedirs(col_path)
if not os.path.exists(self._init_path):
self.output.debug(msg=f"creating new directory at {self._init_path}")
os.makedirs(self._init_path)

# copy new_collection container to destination, templating files when found
self.output.debug(msg="started copying collection skeleton to destination")
copy_container(
source="new_collection",
dest=col_path,
dest=self._init_path,
templar=self._templar,
template_data={
"namespace": self._namespace,
Expand Down
20 changes: 15 additions & 5 deletions tests/integration/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ def test_run_init_no_input(cli):


def test_run_init_basic(cli, tmp_path):
result = cli(f"ansible-creator init testorg.testcol --init-path {tmp_path}")
final_dest = f"{tmp_path}/collections/ansible_collections"
cli(f"mkdir -p {final_dest}")

result = cli(
f"ansible-creator init testorg.testcol --init-path {final_dest}",
)
assert result.returncode == 0

# check stdout
Expand All @@ -86,13 +91,18 @@ def test_run_init_basic(cli, tmp_path):
)

# fail to override existing collection with force=false (default)
result = cli(f"ansible-creator init testorg.testcol --init-path {tmp_path}")
result = cli(
f"ansible-creator init testorg.testcol --init-path {final_dest}",
)

assert result.returncode != 0

# this is required to handle random line breaks in CI, especially with macos runners
mod_stderr = "".join([line.strip() for line in result.stderr.splitlines()])
assert (
re.search(
rf"Error: The directory\s+{tmp_path}/testorg/testcol\s+already exists.",
result.stderr,
flags=re.MULTILINE,
rf"Error:\s*The\s*directory\s*{final_dest}/testorg/testcol\s*already\s*exists",
mod_stderr,
)
is not None
)
Expand Down
2 changes: 1 addition & 1 deletion tests/units/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def cli_args(tmp_path) -> dict:
"subcommand": "init",
"verbose": 0,
"collection": "testorg.testcol",
"init_path": tmp_path,
"init_path": tmp_path / "testorg" / "testcol",
}


Expand Down

0 comments on commit 8178ec7

Please sign in to comment.