|
1 | | -import os |
| 1 | +import re |
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | 4 | import pytest |
| 5 | +from pytest_cases import parametrize_with_cases |
5 | 6 |
|
6 | | -from tests.helpers import RequirementFiles, run_command |
| 7 | +from tests.helpers import run_command |
| 8 | +from tests.test_venv_install_cases import CasesVenvInstallDevRequirementstxt, CasesVenvInstallRequirementstxt |
| 9 | + |
| 10 | +_package_regex = re.compile(r"^([a-zA-Z0-9_-]+)\b") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.order(after="test_venv_activate.py::test_venv_activate") |
| 14 | +@pytest.mark.parametrize("command_arg", ["", "requirements.txt"]) |
| 15 | +@parametrize_with_cases(argnames=["files"], cases=CasesVenvInstallRequirementstxt) |
| 16 | +def test_venv_install_requirements_txt( |
| 17 | + command_arg: str, |
| 18 | + files: dict[str, str], |
| 19 | + tmp_path: Path, |
| 20 | + capfd: pytest.CaptureFixture, |
| 21 | +): |
| 22 | + (tmp_path / "requirements.txt").write_text(files["requirements.txt"]) |
| 23 | + |
| 24 | + # Install the requirements |
| 25 | + run_command(f"venv install {command_arg} --skip-lock", cwd=tmp_path, activated=True) |
| 26 | + |
| 27 | + # Check pip install log output |
| 28 | + output: str = capfd.readouterr().out |
| 29 | + assert "Installing requirements from requirements.txt" in output |
| 30 | + |
| 31 | + installed_line = [line for line in output.splitlines() if line.startswith("Successfully installed")][0] |
| 32 | + for requirement in files["requirements.txt"].splitlines(): |
| 33 | + re_match = _package_regex.match(requirement) |
| 34 | + if re_match is None: |
| 35 | + raise ValueError(f"Could not extract package name from requirement '{requirement}'") |
| 36 | + |
| 37 | + package_name = re_match.group() |
| 38 | + assert package_name in installed_line |
7 | 39 |
|
8 | 40 |
|
9 | 41 | @pytest.mark.order(after="test_venv_activate.py::test_venv_activate") |
10 | | -@pytest.mark.parametrize( |
11 | | - "file", |
12 | | - [ |
13 | | - "", |
14 | | - "requirements.txt", |
15 | | - "dev-requirements.txt", |
16 | | - "requirements.lock", |
17 | | - "dev-requirements.lock", |
18 | | - ], |
19 | | -) |
20 | | -def test_venv_install(file: str, venv_dir: RequirementFiles, create_test_credentials: None): |
21 | | - """Checks that we can install requirements in an activated environment""" |
22 | | - file_path: str | Path = venv_dir.get(file, file) |
23 | | - |
24 | | - run_command(f"venv install {file_path} --skip-lock", activated=True, cwd=venv_dir["base"]) |
| 42 | +@parametrize_with_cases(argnames=["files"], cases=CasesVenvInstallDevRequirementstxt) |
| 43 | +def test_venv_install_dev_requirements_txt( |
| 44 | + files: dict[str, str], |
| 45 | + tmp_path: Path, |
| 46 | + capfd: pytest.CaptureFixture, |
| 47 | +): |
| 48 | + for file_name, contents in files.items(): |
| 49 | + (tmp_path / file_name).write_text(contents) |
| 50 | + |
| 51 | + # Install the requirements |
| 52 | + run_command("venv install dev-requirements.txt --skip-lock", cwd=tmp_path, activated=True) |
| 53 | + |
| 54 | + # Check pip install log output |
| 55 | + output: str = capfd.readouterr().out |
| 56 | + assert "Installing requirements from dev-requirements.txt" in output |
| 57 | + |
| 58 | + installed_line = [line for line in output.splitlines() if line.startswith("Successfully installed")][0] |
| 59 | + for requirement in [ |
| 60 | + *files["requirements.txt"].splitlines(), |
| 61 | + *files["dev-requirements.txt"].splitlines(), |
| 62 | + ]: |
| 63 | + if requirement.startswith("-r"): |
| 64 | + # Skip '-r requirements.txt' line |
| 65 | + continue |
| 66 | + |
| 67 | + _check_package_was_installed(requirement=requirement, installed_line=installed_line) |
| 68 | + |
| 69 | + |
| 70 | +def _check_package_was_installed(requirement: str, installed_line: str) -> None: |
| 71 | + re_match = _package_regex.match(requirement) |
| 72 | + if re_match is None: |
| 73 | + raise ValueError(f"Could not extract package name from requirement '{requirement}'") |
| 74 | + |
| 75 | + package_name = re_match.group() |
| 76 | + assert package_name in installed_line |
0 commit comments