from pathlib import Path
from domin8.tools.test_selection import select_tests_for_changes
from domin8.tools.diff_utils import FileChange
def test_selects_tests_for_python_import(tmp_path):
repo_root = Path('.').resolve()
# Create a module and a test file that imports it
module = Path('src/mymod/foo.py')
test_file = Path('tests/test_foo.py')
module.parent.mkdir(parents=True, exist_ok=True)
test_file.parent.mkdir(parents=True, exist_ok=True)
module.write_text('def a():\n return 1\n')
test_file.write_text('from src.mymod import foo\n\ndef test_a():\n assert foo.a()==1\n')
fc = FileChange(repo_relative_path='src/mymod/foo.py', action='edit', old_path='src/mymod/foo.py', new_path='src/mymod/foo.py', hunks=[], file_type='source')
tests = select_tests_for_changes([fc], repo_root)
assert any('test_foo.py' in t for t in tests)
def test_selects_tests_for_symbol_change(tmp_path):
repo_root = Path('.').resolve()
module = Path('src/mymod/sym.py')
test_file = Path('tests/test_sym.py')
module.parent.mkdir(parents=True, exist_ok=True)
test_file.parent.mkdir(parents=True, exist_ok=True)
module.write_text('def public_fn():\n return 1\n')
test_file.write_text('from src.mymod import sym\n\ndef test_public():\n assert sym.public_fn()==1\n')
# create a diff adding a new function symbol
diff = ('--- a/src/mymod/sym.py\n' "+++ b/src/mymod/sym.py\n" '@@ -1,0 +1,3 @@\n' '+def public_fn():\n' '+ return 1\n')
from domin8.tools.diff_utils import normalize_diff
normalized = normalize_diff(diff)
tests = select_tests_for_changes(normalized, repo_root)
assert any('test_sym.py' in t for t in tests)