Skip to content

Commit 764a914

Browse files
committed
🔧 Refactor oneline parser warning logging and enhance CLI output
1 parent c5f90a4 commit 764a914

4 files changed

Lines changed: 57 additions & 36 deletions

File tree

‎src/sphinx_codelinks/analyse/analyse.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,6 @@ def extract_oneline_need(
207207
resolved.sub_type.value,
208208
)
209209
self.oneline_warnings.append(warning)
210-
logger.warning(
211-
"Oneline parser warning in %s:%d - %s: %s",
212-
src_comment.source_file.filepath,
213-
lineno,
214-
resolved.sub_type.value,
215-
resolved.msg,
216-
)
217210
row_offset += 1
218211
continue
219212
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: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# @Test suite for source code analysis and marker extraction, TEST_ANA_1, test, [IMPL_LNK_1, IMPL_ONE_1, IMPL_MRST_1]
22
import json
3-
import logging
43
from pathlib import Path
54

65
import pytest
@@ -144,9 +143,8 @@ def test_analyse_oneline_needs(
144143
assert cnt_comments == result["num_comments"]
145144

146145

147-
def test_oneline_parser_warning_is_logged(tmp_path, caplog):
148-
"""Test that oneline parser warnings are logged to the console."""
149-
146+
def test_oneline_parser_warnings_are_collected(tmp_path):
147+
"""Test that oneline parser warnings are collected for later output."""
150148
src_dir = TEST_DIR / "data" / "oneline_comment_default"
151149
src_paths = [src_dir / "default_oneliners.c"]
152150

@@ -159,27 +157,11 @@ def test_oneline_parser_warning_is_logged(tmp_path, caplog):
159157
oneline_comment_style=ONELINE_COMMENT_STYLE_DEFAULT,
160158
)
161159

162-
# Ensure the logger propagates to root so caplog can capture it
163-
analyse_logger = logging.getLogger("sphinx_codelinks.analyse.analyse")
164-
original_propagate = analyse_logger.propagate
165-
analyse_logger.propagate = True
166-
167-
try:
168-
with caplog.at_level(
169-
logging.WARNING, logger="sphinx_codelinks.analyse.analyse"
170-
):
171-
src_analyse = SourceAnalyse(src_analyse_config)
172-
src_analyse.run()
173-
174-
# Verify that warnings were collected
175-
assert len(src_analyse.oneline_warnings) == 1
176-
177-
# Verify that the warning was logged
178-
warning_records = [
179-
r for r in caplog.records if "Oneline parser warning" in r.message
180-
]
181-
assert len(warning_records) >= 1
182-
assert "too_many_fields" in warning_records[0].message
183-
finally:
184-
# Restore original propagate setting
185-
analyse_logger.propagate = original_propagate
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)