diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3404f730..ea18fb8a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -51,7 +51,7 @@ jobs: run: python -m pip install hatch 'click!=8.3.0' - name: Run tests - run: hatch run test:test + run: hatch run test:test --with-network - name: Upload coverage uses: actions/upload-artifact@v7 diff --git a/pyproject.toml b/pyproject.toml index d25daff6..51a60c37 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -76,6 +76,7 @@ test = [ "hatchling", "hatch-vcs", "click!=8.3.0", + "setuptools-scm", ] docs = [ "sphinx", @@ -283,3 +284,8 @@ description = "Sphinx docs." features = ["docs"] [tool.hatch.envs.docs.scripts] build = "sphinx-build -M html docs docs/_build -j auto --keep-going {args:--fail-on-warning --fresh-env -n}" + +[tool.pytest.ini_options] +markers = [ + "network: test that need network access", +] diff --git a/tests/conftest.py b/tests/conftest.py index d7b295c2..55668167 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,26 @@ E2E_PATH = pathlib.Path(__file__).parent.parent.absolute() / "e2e" +def pytest_addoption(parser: pytest.Parser) -> None: + parser.addoption( + "--with-network", + action="store_true", + default=False, + help="run tests that require network access", + ) + + +def pytest_collection_modifyitems( + config: pytest.Config, items: list[pytest.Item] +) -> None: + if config.getoption("--with-network"): + return + skip_network = pytest.mark.skip(reason="need --with-network option to run") + for item in items: + if "network" in item.keywords: + item.add_marker(skip_network) + + @pytest.fixture def testdata_path() -> typing.Generator[pathlib.Path, None, None]: yield TESTDATA_PATH diff --git a/tests/test_gitutils.py b/tests/test_gitutils.py index 11788404..2c40f3f3 100644 --- a/tests/test_gitutils.py +++ b/tests/test_gitutils.py @@ -1,10 +1,30 @@ import pathlib +import shutil +import subprocess +import sys from unittest.mock import Mock, patch import pytest +from packaging.version import Version from fromager.gitutils import git_clone_fast +needs_git_command = pytest.mark.skipif( + shutil.which("git") is None, reason="requires 'git' command" +) + + +def setuptools_scm_version(root_dir: pathlib.Path) -> Version: + out = subprocess.check_output( + [sys.executable, "-m", "setuptools_scm"], + text=True, + stderr=subprocess.STDOUT, + cwd=str(root_dir), + ) + # last line contains the version + lastline = out.strip().splitlines()[-1] + return Version(lastline) + @patch("fromager.external_commands.run") def test_git_clone_fast(m_run: Mock, tmp_path: pathlib.Path) -> None: @@ -56,8 +76,12 @@ def test_git_clone_fast_submodules(m_run: Mock, tmp_path: pathlib.Path) -> None: ) -@pytest.mark.skip(reason="needs network access") +@pytest.mark.network +@needs_git_command def test_git_clone_real(tmp_path: pathlib.Path) -> None: repo_url = "https://github.com/python-wheel-build/fromager.git" git_clone_fast(output_dir=tmp_path, repo_url=repo_url, ref="refs/tags/0.73.0") assert tmp_path.joinpath("src", "fromager").is_dir() + + # detect version from .git + assert setuptools_scm_version(tmp_path) == Version("0.73.0")