102 lines
4.0 KiB
Python
102 lines
4.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Smoke tests for test_generation_orchestrator.py
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# Add scripts dir to path for imports (orchestrator.py lives in scripts/)
|
||
|
|
SCRIPT_DIR = Path(__file__).resolve().parent
|
||
|
|
sys.path.insert(0, str(SCRIPT_DIR))
|
||
|
|
|
||
|
|
from test_generation_orchestrator import (
|
||
|
|
load_queue, save_queue, GenResult, has_new_code,
|
||
|
|
_stub, GENERATOR_NAMES, GENERATORS
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_load_queue_empty_when_missing():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
p = Path(tmp) / "nofile.txt"
|
||
|
|
assert load_queue(p) == []
|
||
|
|
|
||
|
|
def test_save_and_load_queue_roundtrip():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
p = Path(tmp) / "queue.txt"
|
||
|
|
items = ["repo1", "# comment", "", "repo2"]
|
||
|
|
save_queue(p, items)
|
||
|
|
loaded = load_queue(p)
|
||
|
|
assert loaded == ["repo1", "repo2"]
|
||
|
|
|
||
|
|
def test_stub_generator_creates_test_file():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
repo = Path(tmp) / "repo"
|
||
|
|
repo.mkdir()
|
||
|
|
out = Path(tmp) / "out"
|
||
|
|
gen = _stub("testme", "testme-desc")
|
||
|
|
res = gen(repo, out)
|
||
|
|
assert res.tests_written == 1
|
||
|
|
assert res.pass_rate == 1.0
|
||
|
|
assert (out / "test_testme_autogenerated.py").exists()
|
||
|
|
content = (out / "test_testme_autogenerated.py").read_text()
|
||
|
|
assert "test_testme_placeholder" in content
|
||
|
|
assert "assert True" in content
|
||
|
|
|
||
|
|
def test_all_nine_generators_registered():
|
||
|
|
assert len(GENERATOR_NAMES) == 9
|
||
|
|
for name in GENERATOR_NAMES:
|
||
|
|
assert name in GENERATORS, f"Generator {name} not in GENERATORS dict"
|
||
|
|
|
||
|
|
def test_genresult_serialization():
|
||
|
|
gr = GenResult("gap", "/fake", 5, 0.8, coverage_delta=2.5, error=None)
|
||
|
|
d = gr.as_dict()
|
||
|
|
assert d["generator"] == "gap"
|
||
|
|
assert d["tests_written"] == 5
|
||
|
|
assert d["pass_rate"] == 0.8
|
||
|
|
assert d["coverage_delta"] == 2.5
|
||
|
|
assert "timestamp" in d
|
||
|
|
|
||
|
|
def test_has_new_code_when_no_last():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
repo = Path(tmp) / "repo"
|
||
|
|
repo.mkdir()
|
||
|
|
# initialize git
|
||
|
|
subprocess.run(["git", "init"], cwd=repo, check=True, capture_output=True)
|
||
|
|
(repo / "file.txt").write_text("hello")
|
||
|
|
subprocess.run(["git", "add", "."], cwd=repo, check=True, capture_output=True)
|
||
|
|
subprocess.run(["git", "commit", "-m", "init"], cwd=repo, check=True, capture_output=True)
|
||
|
|
assert has_new_code(repo, None) is True
|
||
|
|
|
||
|
|
def test_has_new_code_when_behind():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
repo = Path(tmp) / "repo"
|
||
|
|
repo.mkdir()
|
||
|
|
subprocess.run(["git", "init"], cwd=repo, check=True, capture_output=True)
|
||
|
|
(repo / "f1").write_text("a")
|
||
|
|
subprocess.run(["git", "add", "."], cwd=repo, check=True, capture_output=True)
|
||
|
|
subprocess.run(["git", "commit", "-m", "first"], cwd=repo, check=True, capture_output=True)
|
||
|
|
first_sha = subprocess.run(["git", "rev-parse", "HEAD"], capture_output=True, text=True, cwd=repo).stdout.strip()
|
||
|
|
# make a new commit
|
||
|
|
(repo / "f2").write_text("b")
|
||
|
|
subprocess.run(["git", "add", "."], cwd=repo, check=True, capture_output=True)
|
||
|
|
subprocess.run(["git", "commit", "-m", "second"], cwd=repo, check=True, capture_output=True)
|
||
|
|
assert has_new_code(repo, first_sha) is True
|
||
|
|
|
||
|
|
def test_has_new_code_when_up_to_date():
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
repo = Path(tmp) / "repo"
|
||
|
|
repo.mkdir()
|
||
|
|
subprocess.run(["git", "init"], cwd=repo, check=True, capture_output=True)
|
||
|
|
(repo / "f").write_text("a")
|
||
|
|
subprocess.run(["git", "add", "."], cwd=repo, check=True, capture_output=True)
|
||
|
|
subprocess.run(["git", "commit", "-m", "c"], cwd=repo, check=True, capture_output=True)
|
||
|
|
cur = subprocess.run(["git", "rev-parse", "HEAD"], capture_output=True, text=True, cwd=repo).stdout.strip()
|
||
|
|
assert has_new_code(repo, cur) is False
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
import pytest
|
||
|
|
sys.exit(pytest.main([__file__, "-v"]))
|