From 745060921e57985ce42790d909f1c538d4384328 Mon Sep 17 00:00:00 2001 From: Lejoly Florent Date: Thu, 20 Oct 2022 15:54:35 +0200 Subject: [PATCH] Fix issue with "inmanta module build" command on a v1 module if inmanta_plugins dir already exists (Issue #4954, PR #4994) Pull request opened by the merge tool on behalf of #4994 --- .../unreleased/4954-module-build-fix.yml | 7 ++ src/inmanta/moduletool.py | 6 ++ tests/moduletool/test_build.py | 65 ++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/4954-module-build-fix.yml diff --git a/changelogs/unreleased/4954-module-build-fix.yml b/changelogs/unreleased/4954-module-build-fix.yml new file mode 100644 index 0000000000..3f99be2d68 --- /dev/null +++ b/changelogs/unreleased/4954-module-build-fix.yml @@ -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}}" diff --git a/src/inmanta/moduletool.py b/src/inmanta/moduletool.py index 6940690b05..9cd03715f2 100644 --- a/src/inmanta/moduletool.py +++ b/src/inmanta/moduletool.py @@ -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: diff --git a/tests/moduletool/test_build.py b/tests/moduletool/test_build.py index 3f219027df..be3f125b6b 100644 --- a/tests/moduletool/test_build.py +++ b/tests/moduletool/test_build.py @@ -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 @@ -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"))