8OI-509 / FS-505 regression tests — :func:`oct.core.parsing.header_boundary`
9is the single implementation shared by the linter and the formatter for
10locating the start of file content after an Option-C 4-line header
11(shebang → encoding → file-identity → blank).
13Also covers :func:`oct.core.parsing.safe_parse` — shared AST helper that
14captures ``SyntaxWarning`` diagnostics instead of leaking them to stderr.
18- Full 4-line header → boundary is 4.
19- Missing shebang → skips straight to encoding.
20- No header at all → boundary is 0.
21- Trailing blank is only consumed when at least one header element matched.
22- Identity comment is matched via either the strict expected string or the
23 path-basename fallback.
24- ``safe_parse`` returns (None, []) on syntax error.
25- ``safe_parse`` captures invalid-escape SyntaxWarnings without printing.
32 L3 — assertion details
37- Tests operate on in-memory strings; no files are created or read.
40from pathlib
import Path
42from oct.core.parsing
import header_boundary, safe_parse
46 return text.splitlines()
50 path = Path(
"oct/core/parsing.py")
52 "#!/usr/bin/env python3\n"
53 "# -*- coding: utf-8 -*-\n"
54 "# oct/core/parsing.py\n"
58 assert header_boundary(
59 _lines(text), path, expected_identity=
"# oct/core/parsing.py"
64 path = Path(
"oct/core/parsing.py")
66 "# -*- coding: utf-8 -*-\n"
67 "# oct/core/parsing.py\n"
71 assert header_boundary(
72 _lines(text), path, expected_identity=
"# oct/core/parsing.py"
78 text =
"import os\nprint('hi')\n"
79 assert header_boundary(
_lines(text), path) == 0
84 text =
"\nimport os\n"
85 assert header_boundary(
_lines(text), path) == 0
89 """When no expected_identity is passed, any `# ...basename` qualifies."""
90 path = Path(
"pkg/mod.py")
92 "#!/usr/bin/env python3\n"
93 "# some/other/prefix mod.py\n"
97 assert header_boundary(
_lines(text), path) == 3
101 tree, warns = safe_parse(
"def broken(:\n")
108 tree, warns = safe_parse(src)
109 assert tree
is not None
110 assert any(
"invalid escape" in w
for w
in warns)
test_full_header_boundary_is_four()
test_safe_parse_captures_invalid_escape_warning()
test_identity_fallback_matches_basename()
test_trailing_blank_not_consumed_without_header()
test_safe_parse_returns_none_on_syntax_error()
test_no_header_returns_zero()
list[str] _lines(str text)
test_missing_shebang_skips_to_encoding()