Option C Tools
Loading...
Searching...
No Matches
test_import_fixing.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_formatter/test_import_fixing.py
4
5"""
6Tests for import fixing.
7
8Purpose
9-------
10Verify that the formatter correctly fixes _dbg import issues.
11
12Responsibilities
13----------------
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.
18
19Diagnostics
20-----------
21Domain: TESTS.FORMATTER
22Levels:
23 L2 — test lifecycle
24 L3 — test case setup
25 L4 — detailed import parsing and verification
26
27Contracts
28---------
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.
33"""
34
35import pytest
36from pathlib import Path
37
38from oct.formatter.oct_format import fix_dbg_import, FormatterContext
39
40
41# Helper to create a file with enough lines to not be trivial
42def _make_nontrivial(content: str) -> str:
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)
48
49
50def test_add_missing_import(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
51 """Test that canonical import is added when missing."""
52 test_file = temp_project_root / "test_missing_import.py"
53 content = _make_nontrivial(
54 "#!/usr/bin/env python3\n"
55 "# -*- coding: utf-8 -*-\n"
56 "# test.py\n"
57 "\n"
58 "def foo():\n"
59 " _dbg('TEST', 'test')\n"
60 )
61 test_file.write_text(content, encoding="utf-8")
62
63 changed, new_content, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
64
65 assert changed is True
66 assert "from oc_diagnostics import _dbg" in new_content
67
68
69def test_skip_trivial_files(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
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")
74
75 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
76
77 assert changed is False
78
79
80def test_import_dry_run(temp_project_root: Path, formatter_ctx: FormatterContext):
81 """Test that dry-run mode doesn't modify files."""
82 test_file = temp_project_root / "test_import_dryrun.py"
83 content = _make_nontrivial(
84 "#!/usr/bin/env python3\n"
85 "# -*- coding: utf-8 -*-\n"
86 "# test.py\n"
87 "\n"
88 "def foo():\n"
89 " _dbg('TEST', 'test')\n"
90 )
91 test_file.write_text(content, encoding="utf-8")
92
93 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx)
94
95 assert changed is True # Change detected
96 assert test_file.read_text(encoding="utf-8") == content # But not applied
97
98
99def test_canonical_import_no_change(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
100 """Test that canonical imports are not modified."""
101 test_file = temp_project_root / "test_canonical_import.py"
102 content = _make_nontrivial(
103 "#!/usr/bin/env python3\n"
104 "# -*- coding: utf-8 -*-\n"
105 "# test.py\n"
106 "\n"
107 "from oc_diagnostics import _dbg\n"
108 "\n"
109 "def foo():\n"
110 " _dbg('TEST', 'test')\n"
111 )
112 test_file.write_text(content, encoding="utf-8")
113
114 changed, _, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
115
116 assert changed is False
117
118
119def test_preserve_other_imports(temp_project_root: Path, formatter_ctx_fix: FormatterContext):
120 """Test that other imports are preserved."""
121 test_file = temp_project_root / "test_preserve_imports.py"
122 content = _make_nontrivial(
123 "#!/usr/bin/env python3\n"
124 "# -*- coding: utf-8 -*-\n"
125 "# test.py\n"
126 "\n"
127 "import sys\n"
128 "import os\n"
129 "\n"
130 "def foo():\n"
131 " _dbg('TEST', 'test')\n"
132 )
133 test_file.write_text(content, encoding="utf-8")
134
135 changed, new_content, messages = fix_dbg_import(test_file, content, formatter_ctx_fix)
136
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
test_preserve_other_imports(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_import_dry_run(Path temp_project_root, FormatterContext formatter_ctx)
test_canonical_import_no_change(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_skip_trivial_files(Path temp_project_root, FormatterContext formatter_ctx_fix)
test_add_missing_import(Path temp_project_root, FormatterContext formatter_ctx_fix)