|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | + |
| 7 | + |
| 8 | +def run_script(script: str, *args: str, timeout: int = 30) -> subprocess.CompletedProcess[str]: |
| 9 | + """Run an inline Python script as a subprocess and return the completed process.""" |
| 10 | + return subprocess.run( |
| 11 | + [sys.executable, '-c', script, *args], |
| 12 | + capture_output=True, |
| 13 | + text=True, |
| 14 | + encoding='utf-8', |
| 15 | + timeout=timeout, |
| 16 | + check=False, |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def scenario_script(extra: str = '') -> str: |
| 21 | + """Return a self-contained script that creates a Scenario and calls cli().""" |
| 22 | + return textwrap.dedent(f''' |
| 23 | + import sys |
| 24 | + sys.path.insert(0, {str(__import__('pathlib').Path(__file__).parent.parent.parent)!r}) |
| 25 | + from microbenchmark import Scenario |
| 26 | +
|
| 27 | + tick = [0.0] |
| 28 | + def fake_timer(): |
| 29 | + tick[0] += 0.001 |
| 30 | + return tick[0] |
| 31 | +
|
| 32 | + s = Scenario(lambda: None, name='bench', number=10, timer=fake_timer) |
| 33 | + {extra} |
| 34 | + s.cli() |
| 35 | + ''') |
| 36 | + |
| 37 | + |
| 38 | +class TestScenarioCliOutput: |
| 39 | + def test_cli_outputs_name(self) -> None: |
| 40 | + proc = run_script(scenario_script()) |
| 41 | + assert 'benchmark: bench' in proc.stdout |
| 42 | + |
| 43 | + def test_cli_outputs_mean(self) -> None: |
| 44 | + proc = run_script(scenario_script()) |
| 45 | + assert 'mean:' in proc.stdout |
| 46 | + |
| 47 | + def test_cli_outputs_best(self) -> None: |
| 48 | + proc = run_script(scenario_script()) |
| 49 | + assert 'best:' in proc.stdout |
| 50 | + |
| 51 | + def test_cli_outputs_worst(self) -> None: |
| 52 | + proc = run_script(scenario_script()) |
| 53 | + assert 'worst:' in proc.stdout |
| 54 | + |
| 55 | + def test_cli_output_has_s_suffix(self) -> None: |
| 56 | + proc = run_script(scenario_script()) |
| 57 | + assert 's\n' in proc.stdout or proc.stdout.rstrip().endswith('s') |
| 58 | + |
| 59 | + def test_cli_exit_code_0_by_default(self) -> None: |
| 60 | + proc = run_script(scenario_script()) |
| 61 | + assert proc.returncode == 0 |
| 62 | + |
| 63 | + def test_cli_writes_to_stdout(self) -> None: |
| 64 | + proc = run_script(scenario_script()) |
| 65 | + assert proc.stdout.strip() != '' |
| 66 | + assert proc.stderr == '' |
| 67 | + |
| 68 | + |
| 69 | +class TestScenarioCliNumberArg: |
| 70 | + def test_number_arg_changes_durations_count(self) -> None: |
| 71 | + # We can't directly inspect durations from subprocess output, |
| 72 | + # but we can verify the CLI accepts --number without error |
| 73 | + proc = run_script(scenario_script(), '--number', '5') |
| 74 | + assert proc.returncode == 0 |
| 75 | + assert 'benchmark: bench' in proc.stdout |
| 76 | + |
| 77 | + def test_number_arg_default_uses_scenario_number(self) -> None: |
| 78 | + proc = run_script(scenario_script()) |
| 79 | + assert proc.returncode == 0 |
| 80 | + |
| 81 | + |
| 82 | +class TestScenarioCliMaxMean: |
| 83 | + def test_max_mean_below_threshold_exit_0(self) -> None: |
| 84 | + # fake_timer increments by 0.001 each call, so mean per run = 0.001 |
| 85 | + proc = run_script(scenario_script(), '--max-mean', '1.0') |
| 86 | + assert proc.returncode == 0 |
| 87 | + |
| 88 | + def test_max_mean_above_threshold_exit_1(self) -> None: |
| 89 | + # mean will be ~0.001s; threshold 0.000001 is far below |
| 90 | + proc = run_script(scenario_script(), '--max-mean', '0.000001') |
| 91 | + assert proc.returncode == 1 |
| 92 | + |
| 93 | + def test_max_mean_still_prints_output(self) -> None: |
| 94 | + proc = run_script(scenario_script(), '--max-mean', '0.000001') |
| 95 | + assert 'benchmark: bench' in proc.stdout |
| 96 | + |
| 97 | + def test_max_mean_equal_threshold_exit_0(self) -> None: |
| 98 | + # mean = exactly threshold → should pass (mean <= threshold) |
| 99 | + # With fake_timer: each call delta = 0.001, number=10 |
| 100 | + # mean = 0.001. Set --max-mean = 10 to ensure it passes. |
| 101 | + proc = run_script(scenario_script(), '--max-mean', '10') |
| 102 | + assert proc.returncode == 0 |
| 103 | + |
| 104 | + |
| 105 | +class TestScenarioCliHelp: |
| 106 | + def test_help_exits_0(self) -> None: |
| 107 | + proc = run_script(scenario_script(), '--help') |
| 108 | + assert proc.returncode == 0 |
| 109 | + |
| 110 | + def test_help_output_not_empty(self) -> None: |
| 111 | + proc = run_script(scenario_script(), '--help') |
| 112 | + combined = proc.stdout + proc.stderr |
| 113 | + assert len(combined) > 0 |
| 114 | + |
| 115 | + def test_help_mentions_number(self) -> None: |
| 116 | + proc = run_script(scenario_script(), '--help') |
| 117 | + combined = proc.stdout + proc.stderr |
| 118 | + assert 'number' in combined.lower() |
| 119 | + |
| 120 | + def test_help_mentions_max_mean(self) -> None: |
| 121 | + proc = run_script(scenario_script(), '--help') |
| 122 | + combined = proc.stdout + proc.stderr |
| 123 | + assert 'max-mean' in combined.lower() or 'max_mean' in combined.lower() |
0 commit comments