Skip to content

Commit

Permalink
fix(strict-deps): fail if venv is inconsistent
Browse files Browse the repository at this point in the history
This runs `pip check` to ensure that all the packages in the charm
virtualenv have all of their required dependencies, failing the build
if they do not.

Fixes #1781
  • Loading branch information
lengau committed Aug 2, 2024
1 parent ff36d80 commit ab8179d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 10 additions & 8 deletions charmcraft/charm_builder.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}"])
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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."""
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_charm_builder.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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

0 comments on commit ab8179d

Please sign in to comment.