|
| 1 | +"""Acceptance tests for issue #205: the concurrency contract of the command line. |
| 2 | +
|
| 3 | +``ctrace`` deprecated ``--async`` for ``-j``/``--jobs`` (CoreTrace/coretrace#158): the |
| 4 | +analyzer follows it rather than adding an ``--async`` mode. ``-j N`` is ``--jobs N``; |
| 5 | +``0`` analyses in one process per available core, as ``runtime.jobs`` of ``ctrace``; |
| 6 | +the default stays one process. The command always waits for the complete report, which |
| 7 | +is the same whatever the number of processes. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import os |
| 13 | +import re |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from coretrace_python import engine |
| 19 | +from coretrace_python.cli import EXIT_ERROR, main |
| 20 | + |
| 21 | +PROJECT = { |
| 22 | + "app/__init__.py": "", |
| 23 | + "app/a.py": "import os\n\n\ndef run(cmd):\n os.system(cmd)\n", |
| 24 | + "app/b.py": "def handle(code):\n eval(code)\n", |
| 25 | + "app/c.py": "import hashlib\n\n\ndef digest(data):\n return hashlib.md5(data)\n", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def project(tmp_path: Path) -> Path: |
| 31 | + root = tmp_path / "proj" |
| 32 | + for name, text in PROJECT.items(): |
| 33 | + (root / name).parent.mkdir(parents=True, exist_ok=True) |
| 34 | + (root / name).write_text(text, encoding="utf-8") |
| 35 | + return root |
| 36 | + |
| 37 | + |
| 38 | +def report(capsys: pytest.CaptureFixture[str], *options: str) -> tuple[int, str]: |
| 39 | + status = main(["--check", *options, "--format", "json"]) |
| 40 | + out = capsys.readouterr().out |
| 41 | + return status, out |
| 42 | + |
| 43 | + |
| 44 | +def test_j_is_jobs_and_the_report_does_not_depend_on_it( |
| 45 | + project: Path, capsys: pytest.CaptureFixture[str] |
| 46 | +) -> None: |
| 47 | + sequential = report(capsys, str(project)) |
| 48 | + |
| 49 | + assert report(capsys, str(project), "-j", "2") == sequential |
| 50 | + assert report(capsys, str(project), "--jobs", "2") == sequential |
| 51 | + |
| 52 | + |
| 53 | +def test_zero_jobs_is_one_process_per_available_core( |
| 54 | + project: Path, capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch |
| 55 | +) -> None: |
| 56 | + sequential = report(capsys, str(project)) |
| 57 | + used: list[int] = [] |
| 58 | + analyze = engine.analyze_project |
| 59 | + |
| 60 | + def recording(*args: object, jobs: int = 1, **kwargs: object) -> engine.ProjectAnalysis: |
| 61 | + used.append(jobs) |
| 62 | + return analyze(*args, jobs=jobs, **kwargs) # type: ignore[arg-type] |
| 63 | + |
| 64 | + monkeypatch.setattr(engine, "analyze_project", recording) |
| 65 | + cores = getattr(os, "process_cpu_count", os.cpu_count)() or 1 |
| 66 | + |
| 67 | + assert report(capsys, str(project), "-j", "0") == sequential |
| 68 | + assert used == [cores] |
| 69 | + |
| 70 | + |
| 71 | +def test_the_default_stays_one_process( |
| 72 | + project: Path, capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch |
| 73 | +) -> None: |
| 74 | + used: list[int] = [] |
| 75 | + analyze = engine.analyze_project |
| 76 | + |
| 77 | + def recording(*args: object, jobs: int = 1, **kwargs: object) -> engine.ProjectAnalysis: |
| 78 | + used.append(jobs) |
| 79 | + return analyze(*args, jobs=jobs, **kwargs) # type: ignore[arg-type] |
| 80 | + |
| 81 | + monkeypatch.setattr(engine, "analyze_project", recording) |
| 82 | + report(capsys, str(project)) |
| 83 | + |
| 84 | + assert used == [1] |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize("value", ["-1", "-8"]) |
| 88 | +def test_a_negative_number_of_jobs_is_an_error( |
| 89 | + project: Path, capsys: pytest.CaptureFixture[str], value: str |
| 90 | +) -> None: |
| 91 | + assert main(["--check", str(project), "-j", value]) == EXIT_ERROR |
| 92 | + assert "--jobs" in capsys.readouterr().err |
| 93 | + |
| 94 | + |
| 95 | +def test_there_is_no_async_option(project: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 96 | + with pytest.raises(SystemExit) as exit: |
| 97 | + main(["--check", str(project), "--async"]) |
| 98 | + |
| 99 | + assert exit.value.code == EXIT_ERROR |
| 100 | + assert "--async" in capsys.readouterr().err |
| 101 | + |
| 102 | + |
| 103 | +def test_help_names_j_and_zero(capsys: pytest.CaptureFixture[str]) -> None: |
| 104 | + with pytest.raises(SystemExit): |
| 105 | + main(["--help"]) |
| 106 | + text = " ".join(capsys.readouterr().out.split()) |
| 107 | + |
| 108 | + # Python 3.13 prints "-j, --jobs N", earlier versions "-j N, --jobs N". |
| 109 | + assert re.search(r"-j( N)?, --jobs N", text) and "0 for one per available core" in text |
0 commit comments