diff --git a/.gitignore b/.gitignore index bf5055a..8acb770 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ uv.lock # coverage files .coverage coverage.xml +invalid_objs.json diff --git a/src/sphinx_codelinks/analyse/analyse.py b/src/sphinx_codelinks/analyse/analyse.py index a8e9751..50b7b84 100644 --- a/src/sphinx_codelinks/analyse/analyse.py +++ b/src/sphinx_codelinks/analyse/analyse.py @@ -198,15 +198,15 @@ def extract_oneline_need( if not src_comment.source_file: row_offset += 1 continue - self.oneline_warnings.append( - AnalyseWarning( - str(src_comment.source_file.filepath), - src_comment.node.start_point.row + row_offset + 1, - resolved.msg, - MarkedContentType.need, - resolved.sub_type.value, - ) + lineno = src_comment.node.start_point.row + row_offset + 1 + warning = AnalyseWarning( + str(src_comment.source_file.filepath), + lineno, + resolved.msg, + MarkedContentType.need, + resolved.sub_type.value, ) + self.oneline_warnings.append(warning) row_offset += 1 continue yield resolved, row_offset diff --git a/src/sphinx_codelinks/cmd.py b/src/sphinx_codelinks/cmd.py index a702414..e341615 100644 --- a/src/sphinx_codelinks/cmd.py +++ b/src/sphinx_codelinks/cmd.py @@ -56,7 +56,7 @@ @app.command(no_args_is_help=True) -def analyse( +def analyse( # noqa: PLR0912 config: Annotated[ Path, typer.Argument( @@ -151,6 +151,15 @@ def analyse( codelinks_config.projects = specifed_project_configs analyse_projects = AnalyseProjects(codelinks_config) analyse_projects.run() + + # Output warnings to console for CLI users + for src_analyse in analyse_projects.projects_analyse.values(): + for warning in src_analyse.oneline_warnings: + logger.warning( + f"Oneline parser warning in {warning.file_path}:{warning.lineno} " + f"- {warning.sub_type}: {warning.msg}", + ) + analyse_projects.dump_markers() diff --git a/tests/test_analyse.py b/tests/test_analyse.py index 9f74ca7..ab8dc16 100644 --- a/tests/test_analyse.py +++ b/tests/test_analyse.py @@ -141,3 +141,27 @@ def test_analyse_oneline_needs( for src_file in src_analyse.src_files: cnt_comments += len(src_file.src_comments) assert cnt_comments == result["num_comments"] + + +def test_oneline_parser_warnings_are_collected(tmp_path): + """Test that oneline parser warnings are collected for later output.""" + src_dir = TEST_DIR / "data" / "oneline_comment_default" + src_paths = [src_dir / "default_oneliners.c"] + + src_analyse_config = SourceAnalyseConfig( + src_files=src_paths, + src_dir=src_dir, + get_need_id_refs=False, + get_oneline_needs=True, + get_rst=False, + oneline_comment_style=ONELINE_COMMENT_STYLE_DEFAULT, + ) + + src_analyse = SourceAnalyse(src_analyse_config) + src_analyse.run() + + # Verify that warnings were collected + assert len(src_analyse.oneline_warnings) == 1 + warning = src_analyse.oneline_warnings[0] + assert "too_many_fields" in warning.sub_type + assert warning.lineno == 17 diff --git a/tests/test_cmd.py b/tests/test_cmd.py index 4222dca..247e50d 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -67,6 +67,43 @@ def test_analyse(config_path: Path, tmp_path: Path) -> None: assert marked_content +def test_analyse_outputs_warnings(tmp_path: Path) -> None: + """Test that the analyse CLI command outputs warnings to console.""" + # Create a config file that will produce warnings + src_dir = TEST_DIR / "data" / "oneline_comment_default" + config_dict = { + "codelinks": { + "outdir": str(tmp_path), + "projects": { + "test_project": { + "source_discover": { + "src_dir": str(src_dir), + "include": ["*.c"], + "comment_type": "cpp", + }, + "analyse": { + "get_oneline_needs": True, + # Use default oneline_comment_style which will cause warnings + # for the test file with too many fields + }, + } + }, + } + } + + config_file = tmp_path / "test_config.toml" + with config_file.open("w", encoding="utf-8") as f: + toml.dump(config_dict, f) + + options: list[str] = ["analyse", str(config_file)] + result = runner.invoke(app, options) + + assert result.exit_code == 0 + # Verify that warnings are output to console + assert "Oneline parser warning" in result.output + assert "too_many_fields" in result.output + + @pytest.mark.parametrize( ("options", "stdout"), [