Skip to content

Commit bf13214

Browse files
authored
🔧 Enhance oneline parser warning logging (#52) (#55)
1 parent bf3bd2c commit bf13214

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ uv.lock
2323
# coverage files
2424
.coverage
2525
coverage.xml
26+
invalid_objs.json

‎src/sphinx_codelinks/analyse/analyse.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ def extract_oneline_need(
198198
if not src_comment.source_file:
199199
row_offset += 1
200200
continue
201-
self.oneline_warnings.append(
202-
AnalyseWarning(
203-
str(src_comment.source_file.filepath),
204-
src_comment.node.start_point.row + row_offset + 1,
205-
resolved.msg,
206-
MarkedContentType.need,
207-
resolved.sub_type.value,
208-
)
201+
lineno = src_comment.node.start_point.row + row_offset + 1
202+
warning = AnalyseWarning(
203+
str(src_comment.source_file.filepath),
204+
lineno,
205+
resolved.msg,
206+
MarkedContentType.need,
207+
resolved.sub_type.value,
209208
)
209+
self.oneline_warnings.append(warning)
210210
row_offset += 1
211211
continue
212212
yield resolved, row_offset

‎src/sphinx_codelinks/cmd.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757

5858
@app.command(no_args_is_help=True)
59-
def analyse(
59+
def analyse( # noqa: PLR0912
6060
config: Annotated[
6161
Path,
6262
typer.Argument(
@@ -151,6 +151,15 @@ def analyse(
151151
codelinks_config.projects = specifed_project_configs
152152
analyse_projects = AnalyseProjects(codelinks_config)
153153
analyse_projects.run()
154+
155+
# Output warnings to console for CLI users
156+
for src_analyse in analyse_projects.projects_analyse.values():
157+
for warning in src_analyse.oneline_warnings:
158+
logger.warning(
159+
f"Oneline parser warning in {warning.file_path}:{warning.lineno} "
160+
f"- {warning.sub_type}: {warning.msg}",
161+
)
162+
154163
analyse_projects.dump_markers()
155164

156165

‎tests/test_analyse.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,27 @@ def test_analyse_oneline_needs(
141141
for src_file in src_analyse.src_files:
142142
cnt_comments += len(src_file.src_comments)
143143
assert cnt_comments == result["num_comments"]
144+
145+
146+
def test_oneline_parser_warnings_are_collected(tmp_path):
147+
"""Test that oneline parser warnings are collected for later output."""
148+
src_dir = TEST_DIR / "data" / "oneline_comment_default"
149+
src_paths = [src_dir / "default_oneliners.c"]
150+
151+
src_analyse_config = SourceAnalyseConfig(
152+
src_files=src_paths,
153+
src_dir=src_dir,
154+
get_need_id_refs=False,
155+
get_oneline_needs=True,
156+
get_rst=False,
157+
oneline_comment_style=ONELINE_COMMENT_STYLE_DEFAULT,
158+
)
159+
160+
src_analyse = SourceAnalyse(src_analyse_config)
161+
src_analyse.run()
162+
163+
# Verify that warnings were collected
164+
assert len(src_analyse.oneline_warnings) == 1
165+
warning = src_analyse.oneline_warnings[0]
166+
assert "too_many_fields" in warning.sub_type
167+
assert warning.lineno == 17

‎tests/test_cmd.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,43 @@ def test_analyse(config_path: Path, tmp_path: Path) -> None:
6767
assert marked_content
6868

6969

70+
def test_analyse_outputs_warnings(tmp_path: Path) -> None:
71+
"""Test that the analyse CLI command outputs warnings to console."""
72+
# Create a config file that will produce warnings
73+
src_dir = TEST_DIR / "data" / "oneline_comment_default"
74+
config_dict = {
75+
"codelinks": {
76+
"outdir": str(tmp_path),
77+
"projects": {
78+
"test_project": {
79+
"source_discover": {
80+
"src_dir": str(src_dir),
81+
"include": ["*.c"],
82+
"comment_type": "cpp",
83+
},
84+
"analyse": {
85+
"get_oneline_needs": True,
86+
# Use default oneline_comment_style which will cause warnings
87+
# for the test file with too many fields
88+
},
89+
}
90+
},
91+
}
92+
}
93+
94+
config_file = tmp_path / "test_config.toml"
95+
with config_file.open("w", encoding="utf-8") as f:
96+
toml.dump(config_dict, f)
97+
98+
options: list[str] = ["analyse", str(config_file)]
99+
result = runner.invoke(app, options)
100+
101+
assert result.exit_code == 0
102+
# Verify that warnings are output to console
103+
assert "Oneline parser warning" in result.output
104+
assert "too_many_fields" in result.output
105+
106+
70107
@pytest.mark.parametrize(
71108
("options", "stdout"),
72109
[

0 commit comments

Comments
 (0)