Skip to content

Commit

Permalink
Restore coverage from the examples
Browse files Browse the repository at this point in the history
The examples seemed to be running against a different version of
Incremental (possibly from pip's cache?). Ensure they run against
exactly the version of Incremental we're trying to test against by
pre-installing it in an isolated environment.

Since this introduces an isolated environment, also install coverage-p
to enable coverage of the subprocess.
  • Loading branch information
twm committed Jul 27, 2024
1 parent 47af87a commit 6bfc5ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
9 changes: 3 additions & 6 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ source_pkgs = incremental
source = tests
branch = True
parallel = True
# This must be an absolute path because the example tests
# run Python processes with alternative working directories.
data_file = ${TOX_INI_DIR-.}/.coverage

[paths]
source =
src/
.tox/*/lib/python*/site-packages/
.tox/pypy*/site-packages/
src/incremental
*/src/incremental
*/site-packages/incremental

[report]
exclude_lines =
Expand Down
29 changes: 24 additions & 5 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import os
from importlib import metadata
from subprocess import run
from tempfile import TemporaryDirectory

from build import ProjectBuilder
from build.env import DefaultIsolatedEnv
from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase

Expand All @@ -20,11 +22,28 @@


def build_and_install(path): # type: (FilePath) -> None
builder = ProjectBuilder(path.path)
pkgfile = builder.build("wheel", output_directory=os.environ["PIP_FIND_LINKS"])

# Force reinstall in case tox reused the venv.
run(["pip", "install", "--force-reinstall", pkgfile], check=True)
with TemporaryDirectory(prefix="dist") as dist_dir:
with DefaultIsolatedEnv(installer="pip") as env:
env.install(
{
# Install the *exact* version of Incremental under test.
# Otherwise pip might select a different version from
# its cache.
#
# These are formally PEP 508 markers, so we pass a
# file URL.
"incremental @ file://" + os.environ["TOX_PACKAGE"],
# A .pth file so that subprocess generate coverage.
"coverage-p",
}
)
builder = ProjectBuilder.from_isolated_env(env, path.path)
env.install(builder.build_system_requires)
env.install(builder.get_requires_for_build("wheel", {}))
pkgfile = builder.build("wheel", output_directory=dist_dir)

# Force reinstall in case tox reused the venv.
run(["pip", "install", "--force-reinstall", pkgfile], check=True)


class ExampleTests(TestCase):
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ setenv =
; Suppress pointless chatter in the output.
PIP_DISABLE_PIP_VERSION_CHECK=yes

tests: TOX_INI_DIR={toxinidir}
tests: COVERAGE_PROCESS_START={toxinidir}/.coveragerc
tests: PIP_FIND_LINKS={env:PIP_FIND_LINKS:{distdir}}
; This must be an absolute path because the example tests
; run Python processes with alternative working directories.
tests: COVERAGE_FILE={toxinidir}/.coverage

commands =
python -V
Expand Down

0 comments on commit 6bfc5ba

Please sign in to comment.