Skip to content

Commit 965325c

Browse files
NilashishCssbarnea
andauthored
Do not fail if init-path exists but is empty (#134)
* error out only if init-path is not empty Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * remove redundant changes Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> * removed redundant file Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> --------- Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com> Co-authored-by: Sorin Sbarnea <sorin.sbarnea@gmail.com>
1 parent 759ff0d commit 965325c

3 files changed

Lines changed: 32 additions & 26 deletions

File tree

src/ansible_creator/subcommands/init.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,35 @@ def run(self: Init) -> None:
5656

5757
# check if init_path already exists
5858
if os.path.exists(self._init_path):
59+
# init-path exists and is a file
5960
if os.path.isfile(self._init_path):
6061
msg = f"the path {self._init_path} already exists, but is a file - aborting"
6162
raise CreatorError(
6263
msg,
6364
)
64-
65-
if not self._force:
66-
msg = (
67-
f"The directory {self._init_path} already exists.\n"
68-
f"You can use --force to re-initialize this directory."
69-
f"\nHowever it will delete ALL existing contents in it."
65+
if os.listdir(self._init_path):
66+
# init-path exists and is not empty, but user did not request --force
67+
if not self._force:
68+
msg = (
69+
f"The directory {self._init_path} is not empty.\n"
70+
f"You can use --force to re-initialize this directory."
71+
f"\nHowever it will delete ALL existing contents in it."
72+
)
73+
raise CreatorError(msg)
74+
75+
# user requested --force, re-initializing existing directory
76+
self.output.warning(
77+
f"re-initializing existing directory {self._init_path}",
7078
)
71-
raise CreatorError(msg)
72-
73-
# user requested --force, re-initializing existing directory
74-
self.output.warning(f"re-initializing existing directory {self._init_path}")
75-
for root, dirs, files in os.walk(self._init_path, topdown=True):
76-
for old_dir in dirs:
77-
path = os.path.join(root, old_dir)
78-
self.output.debug(f"removing tree {old_dir}")
79-
shutil.rmtree(path)
80-
for old_file in files:
81-
path = os.path.join(root, old_file)
82-
self.output.debug(f"removing file {old_file}")
83-
os.unlink(path)
79+
for root, dirs, files in os.walk(self._init_path, topdown=True):
80+
for old_dir in dirs:
81+
path = os.path.join(root, old_dir)
82+
self.output.debug(f"removing tree {old_dir}")
83+
shutil.rmtree(path)
84+
for old_file in files:
85+
path = os.path.join(root, old_file)
86+
self.output.debug(f"removing file {old_file}")
87+
os.unlink(path)
8488

8589
# if init_path does not exist, create it
8690
if not os.path.exists(self._init_path):

tests/integration/test_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_run_init_basic(cli, tmp_path):
101101
mod_stderr = "".join([line.strip() for line in result.stderr.splitlines()])
102102
assert (
103103
re.search(
104-
rf"Error:\s*The\s*directory\s*{final_dest}/testorg/testcol\s*already\s*exists",
104+
rf"Error:\s*The\s*directory\s*{final_dest}/testorg/testcol\s*is\s*not\s*empty.",
105105
mod_stderr,
106106
)
107107
is not None

tests/units/test_init.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_run_success_for_collection(
5757

5858
# fail to override existing collection with force=false (default)
5959
fail_msg = (
60-
f"The directory {tmp_path}/testorg/testcol already exists."
60+
f"The directory {tmp_path}/testorg/testcol is not empty."
6161
"\nYou can use --force to re-initialize this directory."
6262
"\nHowever it will delete ALL existing contents in it."
6363
)
@@ -107,7 +107,7 @@ def test_run_success_ansible_project(
107107

108108
# fail to override existing ansible-project directory with force=false (default)
109109
fail_msg = (
110-
f"The directory {tmp_path}/new_project already exists."
110+
f"The directory {tmp_path}/new_project is not empty."
111111
"\nYou can use --force to re-initialize this directory."
112112
"\nHowever it will delete ALL existing contents in it."
113113
)
@@ -212,12 +212,14 @@ def test_warning(
212212
)
213213
init.run()
214214
result = capsys.readouterr().out
215+
216+
# this is required to handle random line breaks in CI, especially with macos runners
217+
mod_result = "".join([line.strip() for line in result.splitlines()])
215218
assert (
216219
re.search(
217-
" Warning: The parameters 'scm-org' and 'scm-project' "
218-
"have no effect when project\n is not set to "
219-
"ansible-project",
220-
result,
220+
rf"Warning:\s*The parameters\s*'scm-org'\s*and\s*'scm-project'"
221+
rf"\s*have\s*no\s*effect\s*when\s*project\s*is\s*not\s*set\s*to\s*ansible-project",
222+
mod_result,
221223
)
222224
is not None
223225
)

0 commit comments

Comments
 (0)