Skip to content

Commit 6778853

Browse files
committed
Adjust toxfile.py and --maxprocesses for coverage
Primarily, add a new plugin configuration option which customizes the test environment with a sitecustomize.py to launch coverage automatically. Additionally, customize `set_env` via this invocation path. In aggregate, this is a tox-plugin version of the type of behavior used by `pytest-cov`. Because the `coverage.process_startup()` call adds some overhead, highly parallel runs of the tests could start to fail under pytest-timeout. `tox p` parallelization on top of `pytest-xdist` parallelization became too aggressive, resulting in slow starts when timing was bad. Scaling maxprocesses back down to 4 (no appreciable difference in speed between 4 and 8) results in fast, successful runs.
1 parent 98a5a20 commit 6778853

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ attr = "globus_cli.version.__version__"
8383
profile = "black"
8484

8585
[tool.pytest.ini_options]
86-
addopts = "-n auto --maxprocesses 8 --timeout 3 --color=yes"
86+
addopts = "-n auto --maxprocesses 4 --timeout 3 --color=yes"
8787
filterwarnings = ["error"]
8888

8989
[tool.coverage.run]

tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ recreate =
2525
dependency_groups =
2626
!mindeps: test
2727
mindeps: test-mindeps
28+
# invoke custom plugin code to setup coverage under pytest-xdist
29+
globus_cli_coverage_sitecustomize = true
2830

2931
# the 'localsdk' factor allows CLI tests to be run against a local repo copy of globus-sdk
3032
# it requires that the GLOBUS_SDK_PATH env var is set

toxfile.py

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525

2626
log = logging.getLogger(__name__)
2727

28+
_INJECT_SITECUSTOMIZE = '''
29+
"""A sitecustomize.py injected by globus_cli_coverage_sitecustomize"""
30+
import coverage
31+
32+
coverage.process_startup()
33+
'''
34+
2835

2936
@impl
3037
def tox_add_env_config(env_conf: EnvConfigSet, state: State) -> None:
@@ -34,6 +41,12 @@ def tox_add_env_config(env_conf: EnvConfigSet, state: State) -> None:
3441
default=[],
3542
desc="A dir tree to remove before running the environment commands",
3643
)
44+
env_conf.add_config(
45+
keys=["globus_cli_coverage_sitecustomize"],
46+
of_type=bool,
47+
default=[],
48+
desc="Inject a sitecustomize.py to enable `coverage` under pytest-xdist",
49+
)
3750

3851

3952
@impl
@@ -44,3 +57,14 @@ def tox_before_run_commands(tox_env: ToxEnv) -> None:
4457
if path.exists():
4558
log.warning(f"globus_cli_rmtree: {path}")
4659
shutil.rmtree(path)
60+
61+
if tox_env.conf.load("globus_cli_coverage_sitecustomize"):
62+
site_packages_path = pathlib.Path(tox_env.conf.load("env_site_packages_dir"))
63+
inject_sitecustomize_path = site_packages_path / "sitecustomize.py"
64+
inject_sitecustomize_path.write_text(_INJECT_SITECUSTOMIZE)
65+
66+
# It is important that the tox configuration also sets
67+
# COVERAGE_PROCESS_START to the coverage configuration file.
68+
# If this is not done, `coverage.process_startup` will be a no-op.
69+
setenv = tox_env.conf.load("set_env")
70+
setenv.update({"COVERAGE_PROCESS_START": "pyproject.toml"})

0 commit comments

Comments
 (0)