|
| 1 | +""" |
| 2 | +Copyright 2025 Inmanta |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | + |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import tempfile |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +import packaging.utils |
| 28 | +from inmanta import env, module, moduletool |
| 29 | +from libpip2pi.commands import dir2pi |
| 30 | +from packaging import version |
| 31 | +from utils import module_from_template |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="session") |
| 35 | +def pip_index(modules_v2_dir: str) -> str: |
| 36 | + """ |
| 37 | + Returns the path to a pip index that contains several version for the same python package. |
| 38 | + """ |
| 39 | + with tempfile.TemporaryDirectory() as root_dir: |
| 40 | + source_dir = os.path.join(root_dir, "source") |
| 41 | + build_dir = os.path.join(root_dir, "build") |
| 42 | + index_dir = os.path.join(build_dir, "simple") |
| 43 | + |
| 44 | + modules_to_build = [ |
| 45 | + ("elaboratev2module", version.Version("1.2.3"), False), |
| 46 | + ("elaboratev2module", version.Version("2.3.4"), False), |
| 47 | + ("elaboratev2module", version.Version("2.3.5"), True), |
| 48 | + ("minimalv2module", version.Version("1.1.1"), False), |
| 49 | + ] |
| 50 | + |
| 51 | + for module_name, mod_version, is_prerelease in modules_to_build: |
| 52 | + template_dir = os.path.join(modules_v2_dir, module_name) |
| 53 | + module_dir = os.path.join(source_dir, module_name) |
| 54 | + module_from_template( |
| 55 | + source_dir=template_dir, |
| 56 | + dest_dir=module_dir, |
| 57 | + new_version=mod_version, |
| 58 | + ) |
| 59 | + moduletool.ModuleTool().build( |
| 60 | + path=module_dir, |
| 61 | + output_dir=build_dir, |
| 62 | + dev_build=is_prerelease, |
| 63 | + wheel=True, |
| 64 | + sdist=True, |
| 65 | + ) |
| 66 | + shutil.rmtree(module_dir) |
| 67 | + |
| 68 | + # The setuptools and wheel packages are required by `pip download` |
| 69 | + subprocess.check_call([sys.executable, "-m", "pip", "download", "setuptools", "wheel"], cwd=build_dir) |
| 70 | + dir2pi(argv=["dir2pi", build_dir]) |
| 71 | + yield index_dir |
| 72 | + |
| 73 | + |
| 74 | +def execute_inmanta_download(module_req: str, install: bool, download_dir: str | None) -> str: |
| 75 | + """ |
| 76 | + Executes the `inmanta module download` command and returns the ModuleV2 object for the |
| 77 | + downloaded module. |
| 78 | + """ |
| 79 | + if download_dir is None: |
| 80 | + download_dir = os.getcwd() |
| 81 | + module_name: str = module.InmantaModuleRequirement.parse(module_req).name |
| 82 | + assert not os.listdir(download_dir) |
| 83 | + m = moduletool.ModuleTool() |
| 84 | + m.download(module_req=module_req, install=install, directory=download_dir) |
| 85 | + files = os.listdir(download_dir) |
| 86 | + assert len(files) == 1 |
| 87 | + assert files[0] == module_name |
| 88 | + return module.ModuleV2(project=None, path=os.path.join(download_dir, module_name)) |
| 89 | + |
| 90 | + |
| 91 | +def assert_files_in_module( |
| 92 | + module_dir: str, module_name: str, has_files_dir: bool, has_templates_dir: bool, has_tests_dir: bool |
| 93 | +) -> None: |
| 94 | + """ |
| 95 | + Verify that the directory structure of the extracted python package is correct. |
| 96 | +
|
| 97 | + :param has_files_dir: True iff the given module has a files directory. |
| 98 | + :param has_templates_dir: True iff the given module has a templates directory. |
| 99 | + :param has_tests_dir: True iff the given module has a tests directory. |
| 100 | + """ |
| 101 | + assert os.path.exists(os.path.join(module_dir, "inmanta_plugins")) |
| 102 | + for file_or_dir, must_exist_in_root_dir in [ |
| 103 | + ("tests", has_tests_dir), |
| 104 | + ("files", has_files_dir), |
| 105 | + ("templates", has_templates_dir), |
| 106 | + ("setup.cfg", True), |
| 107 | + ]: |
| 108 | + assert not os.path.exists(os.path.join(module_dir, "inmanta_plugins", module_name, file_or_dir)) |
| 109 | + assert os.path.exists(os.path.join(module_dir, file_or_dir)) == must_exist_in_root_dir |
| 110 | + |
| 111 | + |
| 112 | +def test_download_module(tmpdir, monkeypatch, tmpvenv_active, pip_index: str): |
| 113 | + """ |
| 114 | + Test the `inmanta module download` command with a version constraint. |
| 115 | + """ |
| 116 | + monkeypatch.setenv("PIP_INDEX_URL", pip_index) |
| 117 | + monkeypatch.setenv("PIP_PRE", "false") |
| 118 | + download_dir = os.path.join(tmpdir, "download") |
| 119 | + |
| 120 | + # Test download package without constraint |
| 121 | + os.mkdir(download_dir) |
| 122 | + mod: module.ModuleV2 = execute_inmanta_download(module_req="elaboratev2module", install=False, download_dir=download_dir) |
| 123 | + assert_files_in_module( |
| 124 | + module_dir=mod.path, module_name="elaboratev2module", has_files_dir=True, has_templates_dir=True, has_tests_dir=True |
| 125 | + ) |
| 126 | + assert mod.version == version.Version("2.3.4") |
| 127 | + shutil.rmtree(download_dir) |
| 128 | + |
| 129 | + # Test download package with constraint |
| 130 | + os.mkdir(download_dir) |
| 131 | + mod: module.ModuleV2 = execute_inmanta_download( |
| 132 | + module_req="elaboratev2module~=1.2.0", install=False, download_dir=download_dir |
| 133 | + ) |
| 134 | + assert_files_in_module( |
| 135 | + module_dir=mod.path, module_name="elaboratev2module", has_files_dir=True, has_templates_dir=True, has_tests_dir=True |
| 136 | + ) |
| 137 | + assert mod.version == version.Version("1.2.3") |
| 138 | + shutil.rmtree(download_dir) |
| 139 | + |
| 140 | + # Test download package with --pre |
| 141 | + os.mkdir(download_dir) |
| 142 | + monkeypatch.setenv("PIP_PRE", "true") |
| 143 | + mod: module.ModuleV2 = execute_inmanta_download(module_req="elaboratev2module", install=False, download_dir=download_dir) |
| 144 | + assert_files_in_module( |
| 145 | + module_dir=mod.path, module_name="elaboratev2module", has_files_dir=True, has_templates_dir=True, has_tests_dir=True |
| 146 | + ) |
| 147 | + assert mod.version.base_version == "2.3.5" |
| 148 | + assert mod.version.is_prerelease |
| 149 | + shutil.rmtree(download_dir) |
| 150 | + |
| 151 | + # Test downloading a package that doesn't have any of the optional directories |
| 152 | + # (e.g. files, templates, tests). |
| 153 | + monkeypatch.setenv("PIP_PRE", "false") |
| 154 | + os.mkdir(download_dir) |
| 155 | + mod: module.ModuleV2 = execute_inmanta_download(module_req="minimalv2module", install=False, download_dir=download_dir) |
| 156 | + assert_files_in_module( |
| 157 | + module_dir=mod.path, module_name="minimalv2module", has_files_dir=False, has_templates_dir=False, has_tests_dir=False |
| 158 | + ) |
| 159 | + assert mod.version == version.Version("1.1.1") |
| 160 | + |
| 161 | + |
| 162 | +def test_download_cwd(tmpdir, monkeypatch, tmpvenv_active, pip_index: str): |
| 163 | + """ |
| 164 | + Test the `inmanta module download` command when downloading to the current working directory. |
| 165 | + """ |
| 166 | + monkeypatch.setenv("PIP_INDEX_URL", pip_index) |
| 167 | + monkeypatch.setenv("PIP_PRE", "false") |
| 168 | + download_dir = os.path.join(tmpdir, "download") |
| 169 | + os.mkdir(download_dir) |
| 170 | + monkeypatch.chdir(download_dir) |
| 171 | + mod: module.ModuleV2 = execute_inmanta_download(module_req="elaboratev2module", install=False, download_dir=None) |
| 172 | + assert_files_in_module( |
| 173 | + module_dir=mod.path, module_name="elaboratev2module", has_files_dir=True, has_templates_dir=True, has_tests_dir=True |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +def test_download_install(tmpdir, monkeypatch, tmpvenv_active, pip_index: str): |
| 178 | + """ |
| 179 | + Test the install option of the `inmanta module download` command. |
| 180 | + """ |
| 181 | + monkeypatch.setenv("PIP_INDEX_URL", pip_index) |
| 182 | + monkeypatch.setenv("PIP_PRE", "false") |
| 183 | + download_dir = os.path.join(tmpdir, "download") |
| 184 | + os.mkdir(download_dir) |
| 185 | + pkg_name = "inmanta-module-minimalv2module" |
| 186 | + pkgs_installed_in_editable_mode: dict[packaging.utils.NormalizedName, version.Version] |
| 187 | + pkgs_installed_in_editable_mode = env.process_env.get_installed_packages(only_editable=True) |
| 188 | + assert pkg_name not in pkgs_installed_in_editable_mode |
| 189 | + execute_inmanta_download(module_req="minimalv2module", install=True, download_dir=download_dir) |
| 190 | + pkgs_installed_in_editable_mode = env.process_env.get_installed_packages(only_editable=True) |
| 191 | + assert pkg_name in pkgs_installed_in_editable_mode |
| 192 | + assert pkgs_installed_in_editable_mode[pkg_name] == version.Version("1.1.1") |
0 commit comments