6Tests for import fixing.
10Verify that the formatter correctly fixes _dbg import issues.
14- Test addition of canonical _dbg import when missing.
15- Test removal of aliased/incorrect imports.
16- Test correct placement after header block.
17- Test preservation of other imports.
21Domain: TESTS.FORMATTER
25 L4 — detailed import parsing and verification
29- Canonical form is: from oc_diagnostics import _dbg
30- Import must be at module level (after header block).
31- Trivial files (< 20 lines) are skipped.
32- Other imports are preserved.
36from pathlib
import Path
43 """Pad content to be > 20 lines."""
44 lines = content.split(
'\n')
45 while len(lines) < 22:
46 lines.append(
"# padding")
47 return '\n'.join(lines)
51 """Test that canonical import is added when missing."""
52 test_file = temp_project_root /
"test_missing_import.py"
54 "#!/usr/bin/env python3\n"
55 "# -*- coding: utf-8 -*-\n"
59 " _dbg('TEST', 'test')\n"
61 test_file.write_text(content, encoding=
"utf-8")
63 changed, new_content, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
65 assert changed
is True
66 assert "from oc_diagnostics import _dbg" in new_content
70 """Test that trivial files (< 20 lines) are skipped."""
71 test_file = temp_project_root /
"test_trivial.py"
72 content =
"# No imports\ndef foo():\n pass\n"
73 test_file.write_text(content, encoding=
"utf-8")
75 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
77 assert changed
is False
81 """Test that dry-run mode doesn't modify files."""
82 test_file = temp_project_root /
"test_import_dryrun.py"
84 "#!/usr/bin/env python3\n"
85 "# -*- coding: utf-8 -*-\n"
89 " _dbg('TEST', 'test')\n"
91 test_file.write_text(content, encoding=
"utf-8")
93 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx)
95 assert changed
is True
96 assert test_file.read_text(encoding=
"utf-8") == content
100 """Test that canonical imports are not modified."""
101 test_file = temp_project_root /
"test_canonical_import.py"
103 "#!/usr/bin/env python3\n"
104 "# -*- coding: utf-8 -*-\n"
107 "from oc_diagnostics import _dbg\n"
110 " _dbg('TEST', 'test')\n"
112 test_file.write_text(content, encoding=
"utf-8")
114 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
116 assert changed
is False
120 """Test that other imports are preserved."""
121 test_file = temp_project_root /
"test_preserve_imports.py"
123 "#!/usr/bin/env python3\n"
124 "# -*- coding: utf-8 -*-\n"
131 " _dbg('TEST', 'test')\n"
133 test_file.write_text(content, encoding=
"utf-8")
135 changed, new_content, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
137 assert changed
is True
138 assert "import sys" in new_content
139 assert "import os" in new_content
140 assert "from oc_diagnostics import _dbg" in new_content