Skip to content

Commit

Permalink
test: add tests that check extras work as expected
Browse files Browse the repository at this point in the history
For each extras, it installs the project with that extra, and checks whether the expected extra dependencies have been installed.
  • Loading branch information
marcofavorito authored and marcofavoritobi committed Aug 24, 2023
1 parent 9e0e72b commit c716659
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tempfile
from pathlib import Path
from typing import Generator
from venv import EnvBuilder

import pytest

Expand All @@ -42,3 +43,15 @@ def temporary_directory() -> Generator[Path, None, None]:
os.chdir(tmpdir)
yield Path(tmpdir)
os.chdir(old_dir)


@pytest.fixture(scope="function")
def venv_path( # pylint: disable=redefined-outer-name
temporary_directory: Path,
) -> Generator[Path, None, None]:
"""Fixture that creates a virtual environment."""
env_builder = EnvBuilder(clear=True, with_pip=True)
venv_dirpath = temporary_directory / "venv"
env_builder.create(env_dir=venv_dirpath)
yield venv_dirpath
env_builder.clear_directory(venv_dirpath)
57 changes: 57 additions & 0 deletions tests/test_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Black-box ABM Calibration Kit (Black-it)
# Copyright (C) 2021-2023 Banca d'Italia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""This module contains tests for the setuptools extras mechanism."""
import os
import subprocess # nosec
from pathlib import Path
from typing import AbstractSet, Mapping

import pytest

from tests.conftest import ROOT_DIR

# Define your extras and expected dependencies here.
extras_dependencies = {
"gp-sampler": {"GPy"},
"xgboost-sampler": {"xgboost"},
}


@pytest.mark.not_on_ci
@pytest.mark.parametrize("extra, dependencies", extras_dependencies.items())
def test_extras_installation(
extra: str, dependencies: Mapping[str, AbstractSet[str]], venv_path: Path
) -> None:
"""Test that the extras dependencies are installed as expected."""
bin_dirname = "Scripts" if os.name == "nt" else "bin"
bin_dir = venv_path / bin_dirname
pip_bin_path = bin_dir / "pip"

# Install the project with the extra
subprocess.check_call( # nosec
[pip_bin_path, "install", f".[{extra}]"], cwd=ROOT_DIR
)

# Get a list of installed packages in the virtual environment
output = (
subprocess.check_output([pip_bin_path, "freeze"]).decode().splitlines() # nosec
)
installed_packages = [pkg.split("==")[0] for pkg in output]

# Check that each dependency for the extra is installed
for dependency in dependencies:
assert dependency in installed_packages

0 comments on commit c716659

Please sign in to comment.