def test_build_dependencies_virtualenv_binary_packages(tmp_path): """A virtualenv is created with the specified packages.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), binary_python_packages=["pkg1", "pkg2"], python_packages=[], requirements=[], ) with patch("charmcraft.charm_builder._process_run") as mock: with patch("shutil.copytree") as mock_copytree: builder.handle_dependencies() pip_cmd = str(charm_builder._find_venv_bin(tmp_path / STAGING_VENV_DIRNAME, "pip3")) assert mock.mock_calls == [ call(["python3", "-m", "venv", str(tmp_path / STAGING_VENV_DIRNAME)]), call([pip_cmd, "--version"]), call([pip_cmd, "install", "--upgrade", "pkg1", "pkg2"]), ] site_packages_dir = charm_builder._find_venv_site_packages(pathlib.Path(STAGING_VENV_DIRNAME)) assert mock_copytree.mock_calls == [call(site_packages_dir, build_dir / VENV_DIRNAME)]
def test_build_dependencies_virtualenv_all(tmp_path, emitter): """A virtualenv is created with the specified packages.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() reqs_file_1 = tmp_path / "reqs.txt" reqs_file_1.touch() reqs_file_2 = tmp_path / "reqs.txt" reqs_file_1.touch() builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), binary_python_packages=["pkg1", "pkg2"], python_packages=["pkg3", "pkg4"], requirements=[reqs_file_1, reqs_file_2], ) with patch("charmcraft.charm_builder._process_run") as mock: with patch("shutil.copytree") as mock_copytree: builder.handle_dependencies() pip_cmd = str( charm_builder._find_venv_bin(tmp_path / STAGING_VENV_DIRNAME, "pip3")) assert mock.mock_calls == [ call(["python3", "-m", "venv", str(tmp_path / STAGING_VENV_DIRNAME)]), call([pip_cmd, "--version"]), call([pip_cmd, "install", "--upgrade", "pkg1", "pkg2"]), call([ pip_cmd, "install", "--upgrade", "--no-binary", ":all:", "pkg3", "pkg4" ]), call([ pip_cmd, "install", "--upgrade", "--no-binary", ":all:", f"--requirement={reqs_file_1}", f"--requirement={reqs_file_2}", ]), ] site_packages_dir = charm_builder._find_venv_site_packages( pathlib.Path(STAGING_VENV_DIRNAME)) assert mock_copytree.mock_calls == [ call(site_packages_dir, build_dir / VENV_DIRNAME) ] emitter.assert_trace("Handling dependencies") emitter.assert_progress("Installing dependencies")
def test_find_venv_bin(monkeypatch, platform, result): monkeypatch.setattr(sys, "platform", platform) basedir = pathlib.Path("/basedir") venv_bin = charm_builder._find_venv_bin(basedir, "cmd") assert venv_bin.as_posix() == result