File size: 3,954 Bytes
d8cd787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Process-level checks: a crash or deadline cannot erase collected case results."""
import importlib.util
import os
from pathlib import Path
import xml.etree.ElementTree as ET

ROOT = Path(__file__).resolve().parents[3]
spec = importlib.util.spec_from_file_location('complete_runner', ROOT / 'scripts/run_feature_audit.py')
runner = importlib.util.module_from_spec(spec)
spec.loader.exec_module(runner)


def test_journal_keeps_pass_and_reports_skip_as_failure(tmp_path, monkeypatch):
    test = tmp_path / 'test_phase_contract.py'
    test.write_text('import pytest\ndef test_pass(): assert 2+2 == 4\ndef test_skip(): pytest.skip("runtime absent")\n')
    monkeypatch.setattr(runner, 'BACKEND', tmp_path)
    env = dict(os.environ, PYTHONPATH=str(ROOT / 'scripts'))
    process = runner.execute(str(test), tmp_path / 'attempt', env, 20, 10)
    cases = runner.terminal_cases(tmp_path / 'attempt')
    assert process['exit_code'] == 1
    assert len(cases) == 2
    assert sorted(row['status'] for row in cases.values()) == ['failed', 'passed']
    assert 'Audit forbids skipped tests' in next(row['reason'] for row in cases.values() if row['status'] == 'failed')
    assert not list(ET.parse(tmp_path / 'attempt/pytest.xml').iter('skipped'))


def test_completed_phase_survives_abrupt_process_exit(tmp_path, monkeypatch):
    test = tmp_path / 'test_crash_contract.py'
    test.write_text('import os\ndef test_pass(): assert True\ndef test_crash(): os._exit(7)\n')
    monkeypatch.setattr(runner, 'BACKEND', tmp_path)
    env = dict(os.environ, PYTHONPATH=str(ROOT / 'scripts'))
    process = runner.execute(str(test), tmp_path / 'attempt', env, 20, 10)
    cases = runner.terminal_cases(tmp_path / 'attempt')
    assert process['exit_code'] == 7
    assert len(cases) == 1
    assert next(iter(cases.values()))['status'] == 'passed'
    assert not any('test_crash' == key.split('::')[-1] for key in cases)


def test_deadline_retains_earlier_terminal_case(tmp_path, monkeypatch):
    test = tmp_path / 'test_deadline_contract.py'
    test.write_text('import time\ndef test_pass(): assert True\ndef test_slow(): time.sleep(60)\n')
    monkeypatch.setattr(runner, 'BACKEND', tmp_path)
    env = dict(os.environ, PYTHONPATH=str(ROOT / 'scripts'))
    process = runner.execute(str(test), tmp_path / 'attempt', env, 2, 60)
    cases = runner.terminal_cases(tmp_path / 'attempt')
    assert process['deadline_exceeded'] is True
    assert len(cases) == 1
    assert next(iter(cases.values()))['status'] == 'passed'


def test_selected_case_does_not_execute_uncollected_sibling(tmp_path, monkeypatch):
    import json
    import sys
    test = tmp_path / 'test_selection.py'
    test.write_text('import pytest\ndef test_selected(): assert True\ndef test_unselected(): pytest.skip("must not execute")\n')
    out = tmp_path / 'report'
    monkeypatch.setattr(runner, 'BACKEND', tmp_path)
    monkeypatch.setattr(sys, 'argv', ['audit', '--outdir', str(out),
                                    '--test-root', str(test) + '::test_selected'])
    assert runner.main() == 0
    summary = json.loads((out / 'summary.json').read_text())
    assert summary['planned_cases'] == summary['recorded_cases'] == 1
    assert summary['counts'] == {'passed': 1}


def test_collection_skip_is_a_failure_not_an_empty_green_run(tmp_path, monkeypatch):
    test = tmp_path / 'test_collection_skip.py'
    test.write_text('import pytest\npytest.skip("missing prerequisite", allow_module_level=True)\n')
    monkeypatch.setattr(runner, 'BACKEND', tmp_path)
    env = dict(os.environ, PYTHONPATH=str(ROOT / 'scripts'))
    result = runner.execute(str(test), tmp_path / 'attempt', env, 20, 10)
    assert result['exit_code'] != 0
    assert runner.terminal_cases(tmp_path / 'attempt') == {}
    junit = ET.parse(tmp_path / 'attempt/pytest.xml')
    assert not list(junit.iter('skipped'))
    assert 'Audit forbids collection skips' in (tmp_path / 'attempt/process.log').read_text()