import re
import subprocess
from pathlib import Path
def get_test_files():
result = subprocess.run(
["rg", "-l", r"test!\(|async_test!\(", "crates/ggen-core/src/"],
capture_output=True,
text=True
)
return [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()]
def has_import(content):
return bool(re.search(r'use\s+chicago_tdd_tools::', content))
def uses_async_test(content):
return bool(re.search(r'async_test!\s*\(', content))
def uses_test(content):
return bool(re.search(r'\btest!\s*\(', content))
def fix_file(filepath):
content = filepath.read_text()
if has_import(content):
has_test_macro = uses_test(content)
has_async_macro = uses_async_test(content)
import_match = re.search(r'use\s+chicago_tdd_tools::([^;]+);', content)
if import_match:
imports = import_match.group(1).strip()
if imports == 'test' and has_async_macro:
new_content = re.sub(
r'use\s+chicago_tdd_tools::test;',
'use chicago_tdd_tools::{test, async_test};',
content
)
if new_content != content:
filepath.write_text(new_content)
print(f"✓ Updated import in {filepath}")
return True
print(f" Skipped {filepath} (already has import)")
return False
has_test_macro = uses_test(content)
has_async_macro = uses_async_test(content)
if not (has_test_macro or has_async_macro):
print(f" Skipped {filepath} (no test macros found)")
return False
if has_test_macro and has_async_macro:
import_line = "use chicago_tdd_tools::{test, async_test};"
elif has_async_macro:
import_line = "use chicago_tdd_tools::async_test;"
else:
import_line = "use chicago_tdd_tools::test;"
cfg_test_pattern = r'(#\[cfg\(test\)\]\s*(?:mod\s+\w+\s*\{|pub\s+mod\s+\w+\s*\{))\s*((?:use\s+[^;]+;\s*)*)'
match = re.search(cfg_test_pattern, content)
if match:
before = match.group(1)
existing_uses = match.group(2)
replacement = f"{before}\n {import_line}\n{existing_uses}"
new_content = content[:match.start()] + replacement + content[match.end():]
filepath.write_text(new_content)
print(f"✓ Added import to {filepath}")
return True
print(f" Could not find test module in {filepath}")
return False
def main():
files = get_test_files()
print(f"Found {len(files)} files with test macros")
fixed_count = 0
for filepath in sorted(files):
if fix_file(filepath):
fixed_count += 1
print(f"\nFixed {fixed_count} files")
if __name__ == "__main__":
main()