diff --git a/charmcraft/charm_builder.py b/charmcraft/charm_builder.py index d328d3b84..40a0bb762 100644 --- a/charmcraft/charm_builder.py +++ b/charmcraft/charm_builder.py @@ -1,4 +1,4 @@ -# Copyright 2020-2023 Canonical Ltd. +# Copyright 2020-2024 Canonical Ltd. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -61,18 +61,18 @@ def __init__( installdir: pathlib.Path, entrypoint: pathlib.Path, allow_pip_binary: bool = None, - binary_python_packages: list[str] = None, - python_packages: list[str] = None, - requirements: list[pathlib.Path] = None, + binary_python_packages: list[str] | None = None, + python_packages: list[str] | None = None, + requirements: list[pathlib.Path] | None = None, strict_dependencies: bool = False, ) -> None: self.builddir = builddir self.installdir = installdir self.entrypoint = entrypoint self.allow_pip_binary = allow_pip_binary - self.binary_python_packages = binary_python_packages - self.python_packages = python_packages - self.requirement_paths = requirements + self.binary_python_packages = binary_python_packages or [] + self.python_packages = python_packages or [] + self.requirement_paths = requirements or [] self.strict_dependencies = strict_dependencies self.ignore_rules = self._load_juju_ignore() self.ignore_rules.extend_patterns([f"/{const.STAGING_VENV_DIRNAME}"]) @@ -227,7 +227,7 @@ def _calculate_dependencies_hash(self): return hashlib.sha1(deps_mashup.encode("utf8")).hexdigest() @instrum.Timer("Installing dependencies") - def _install_dependencies(self, staging_venv_dir): + def _install_dependencies(self, staging_venv_dir: pathlib.Path): """Install all dependencies in a specific directory.""" # create virtualenv using the host environment python with instrum.Timer("Creating venv"): @@ -309,6 +309,8 @@ def _install_strict_dependencies(self, pip_cmd: str) -> None: binary_deps=self.binary_python_packages or [], ) ) + # Validate that the environment is consistent. + _process_run([pip_cmd, "check"]) def handle_dependencies(self): """Handle from-directory and virtualenv dependencies.""" diff --git a/tests/integration/test_charm_builder.py b/tests/integration/test_charm_builder.py new file mode 100644 index 000000000..42ee3a01d --- /dev/null +++ b/tests/integration/test_charm_builder.py @@ -0,0 +1,84 @@ +# Copyright 2024 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# For further info, check https://github.com/canonical/charmcraft +"""Integration tests for CharmBuilder.""" + + +import pathlib + +import pytest +from charmcraft import charm_builder + + +@pytest.mark.parametrize( + "requirements", + [ + ["ops==2.15.0"], # Requires pyyaml and websocket-client + ] +) +def test_install_strict_dependencies_pip_check_error( + new_path: pathlib.Path, requirements: list[str] +): + build_dir = new_path / "build" + install_dir = new_path / "install" + entrypoint = build_dir / "entrypoint.py" + + build_dir.mkdir() + install_dir.mkdir() + + requirements_file = build_dir / "requirements.txt" + requirements_file.write_text("\n".join(requirements)) + + builder = charm_builder.CharmBuilder( + builddir=build_dir, + installdir=install_dir, + entrypoint=entrypoint, + requirements=[requirements_file], + strict_dependencies=True + ) + + with pytest.raises(RuntimeError, match="failed with retcode 1") as exc_info: + builder.handle_dependencies() + + +@pytest.mark.parametrize( + "requirements", + [ + ["craft-platforms==0.1.0"], # No dependencies + ] +) +def test_install_strict_dependencies_pip_check_success( + new_path: pathlib.Path, requirements: list[str] +): + build_dir = new_path / "build" + install_dir = new_path / "install" + entrypoint = build_dir / "entrypoint.py" + + build_dir.mkdir() + install_dir.mkdir() + + requirements_file = build_dir / "requirements.txt" + requirements_file.write_text("\n".join(requirements)) + + builder = charm_builder.CharmBuilder( + builddir=build_dir, + installdir=install_dir, + entrypoint=entrypoint, + requirements=[requirements_file], + strict_dependencies=True + ) + + with pytest.raises(RuntimeError, match="failed with retcode 1") as exc_info: + builder.handle_dependencies() diff --git a/tests/unit/test_charm_builder.py b/tests/unit/test_charm_builder.py index eb7146eb2..9ab996640 100644 --- a/tests/unit/test_charm_builder.py +++ b/tests/unit/test_charm_builder.py @@ -1,4 +1,4 @@ -# Copyright 2023 Canonical Ltd. +# Copyright 2023-2024 Canonical Ltd. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -105,6 +105,10 @@ def test_install_strict_dependencies_success( "--no-binary=:all:", "--requirement=requirements.txt", ] - fake_process.register(expected_command, returncode=0) + install_cmd = fake_process.register(expected_command, returncode=0) + check_cmd = fake_process.register(["/pip", "check"], returncode=0) builder._install_strict_dependencies("/pip") + + assert install_cmd.call_count() == 1 + assert check_cmd.call_count() == 1