Skip to content

Commit

Permalink
Fix issue with "inmanta module build" command on a v1 module if inman…
Browse files Browse the repository at this point in the history
…ta_plugins dir already exists (Issue #4954, PR #4994)

Pull request opened by the merge tool on behalf of #4994
  • Loading branch information
FloLey authored and inmantaci committed Oct 20, 2022
1 parent 2ccf220 commit 7450609
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
7 changes: 7 additions & 0 deletions changelogs/unreleased/4954-module-build-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
description: Fix issue with "inmanta module build" command on a v1 module if inmanta_plugins dir already exists
issue-nr: 4954
change-type: patch
destination-branches: [master, iso5]
sections:
bugfix: "{{description}}"
6 changes: 6 additions & 0 deletions src/inmanta/moduletool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,12 @@ def _do_update(self, output_directory: str, setup_cfg: ConfigParser, warn_on_mer
# move plugins or create
old_plugins = os.path.join(output_directory, "plugins")
new_plugins = os.path.join(output_directory, "inmanta_plugins", self._module.name)
if os.path.exists(new_plugins) and os.listdir(new_plugins):
raise ModuleBuildFailedError(
msg=f"Could not build module: inmanta_plugins/{self._module.name} directory already exists and is not empty"
)
if os.path.exists(new_plugins):
os.rmdir(new_plugins)
if os.path.exists(old_plugins):
shutil.move(old_plugins, new_plugins)
else:
Expand Down
65 changes: 64 additions & 1 deletion tests/moduletool/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from inmanta import moduletool
from inmanta.const import CF_CACHE_DIR
from inmanta.module import ModuleMetadataFileNotFound
from inmanta.moduletool import V2ModuleBuilder
from inmanta.moduletool import ModuleBuildFailedError, V2ModuleBuilder
from packaging import version
from utils import v1_module_from_template

Expand Down Expand Up @@ -257,3 +257,66 @@ def test_create_dev_build_of_pre_tagged_module(tmpdir, modules_dir: str, explici
assert re.search(r"1.2.3\.dev[0-9]{14}", path_to_wheel)
else:
assert "1.2.3.dev0" in path_to_wheel


@pytest.mark.slowtest
@pytest.mark.parametrize_any(
"module_name, is_empty",
[
("minimalv1module", True),
("minimalv1module", False),
("elaboratev1module", True),
("elaboratev1module", False),
],
)
def test_build_v1_module_existing_plugin_dir(tmpdir, modules_dir: str, module_name, is_empty) -> None:
"""
Verify that an exception is thrown if the inmanta_plugins/{module_name} directory already exists and is not empty
and that the build succeeds if the directory already exists and is empty
"""
module_dir = os.path.join(modules_dir, module_name)
module_copy_dir = os.path.join(tmpdir, module_name)
shutil.copytree(module_dir, module_copy_dir)

assert os.path.isdir(module_copy_dir)

inmanta_plugins_path = os.path.join(module_copy_dir, "inmanta_plugins")
os.mkdir(inmanta_plugins_path)
my_module_path = os.path.join(inmanta_plugins_path, module_name)
os.mkdir(my_module_path)

assert os.path.isdir(my_module_path)

if not is_empty:
with open(os.path.join(my_module_path, "smt.txt"), "w") as fh:
fh.write("test")

with pytest.raises(
ModuleBuildFailedError,
match=f"Could not build module: inmanta_plugins/{module_name} directory already exists and is not empty",
):
moduletool.ModuleTool().build(module_copy_dir)
else:
moduletool.ModuleTool().build(module_copy_dir)

dist_dir = os.path.join(module_copy_dir, "dist")
dist_dir_content = os.listdir(dist_dir)
assert len(dist_dir_content) == 1
wheel_file = os.path.join(dist_dir, dist_dir_content[0])
assert wheel_file.endswith(".whl")
extract_dir = os.path.join(tmpdir, "extract")
with zipfile.ZipFile(wheel_file) as z:
z.extractall(extract_dir)

assert not os.path.exists(os.path.join(extract_dir, "plugins"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "setup.cfg"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "__init__.py"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "model", "_init.cf"))

if "elaborate" in module_name:
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "files", "test.txt"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "templates", "template.txt.j2"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "model", "other.cf"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "py.typed"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "other_module.py"))
assert os.path.exists(os.path.join(extract_dir, "inmanta_plugins", module_name, "subpkg", "__init__.py"))

0 comments on commit 7450609

Please sign in to comment.