Skip to content

Commit

Permalink
Get rid of missing newline error in clang.
Browse files Browse the repository at this point in the history
On Darwin, executing g++ runs clang, which emits an error when a C++
code block does not end in a newline. This is different from how g++
behaves on Linux.

This error doesn't add any value in the context of a code block in an
RST file, so we just always add a newline to the C++ source code of a
block to always get rid of it.
  • Loading branch information
tjni committed Jul 26, 2023
1 parent 1d343ba commit edd6f42
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ NOTE: please use them in this order.

[diff v1.0.3...main](https://github.com/rstcheck/rstcheck-core/compare/v1.0.3...main)

### Miscellaneous

- Ignore "no newline at end of file" errors when C++ code is checked by clang (such as on macOS)

## [1.0.3 (2022-11-12)](https://github.com/rstcheck/rstcheck-core/releases/v1.0.3)

[diff v1.0.2...v1.0.3](https://github.com/rstcheck/rstcheck-core/compare/v1.0.2...v1.0.3)
Expand Down
1 change: 1 addition & 0 deletions docs/source/spelling_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ kwarg
kwargs
linter
linters
macOS
makefile
monkypatch
monkypatched
Expand Down
4 changes: 3 additions & 1 deletion src/rstcheck_core/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,9 @@ def check_cpp(self, source_code: str) -> types.YieldedLintError:
"""
logger.debug("Check C++ source.")
yield from self._gcc_checker(
source_code,
# Add a newline to ignore "no newline at end of file" errors
# that are reported using clang (e.g. on macOS).
source_code + "\n",
".cpp",
[os.getenv("CXX", "g++")]
+ shlex.split(os.getenv("CXXFLAGS", ""))
Expand Down
32 changes: 0 additions & 32 deletions tests/integration_tests/test_file_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class TestWithoutConfigFile:
"""Test without config file in dir tree."""

@staticmethod
@pytest.mark.skipif(sys.platform == "darwin", reason="MacOS specific variant exists")
def test_error_without_config_file() -> None:
"""Test bad example without set config file and implicit config file shows errors."""
test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst"
Expand All @@ -133,21 +132,6 @@ def test_error_without_config_file() -> None:

assert len(result) == 6

@staticmethod
@pytest.mark.skipif(sys.platform != "darwin", reason="MacOS specific error count")
def test_error_without_config_file_macos() -> None:
"""Test bad example without set config file and implicit config file shows errors.
On MacOS the cpp code block generates an additional error compared to linux:
``(ERROR/3) (cpp) warning: no newline at end of file [-Wnewline-eof]``
"""
test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst"
init_config = config.RstcheckConfig()

result = checker.check_file(test_file, init_config)

assert len(result) == 7

@staticmethod
def test_no_error_with_set_ini_config_file() -> None:
"""Test bad example with set INI config file does not error."""
Expand Down Expand Up @@ -192,7 +176,6 @@ class TestWithConfigFile:
"""Test with config file in dir tree."""

@staticmethod
@pytest.mark.skipif(sys.platform == "darwin", reason="MacOS specific variant exists")
def test_file_1_is_bad_without_config() -> None:
"""Test bad file ``bad.rst`` without config file is not ok."""
test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst"
Expand All @@ -202,21 +185,6 @@ def test_file_1_is_bad_without_config() -> None:

assert len(result) == 6

@staticmethod
@pytest.mark.skipif(sys.platform != "darwin", reason="MacOS specific error count")
def test_file_1_is_bad_without_config_macos() -> None:
"""Test bad file ``bad.rst`` without config file is not ok.
On MacOS the cpp code block generates an additional error compared to linux:
``(ERROR/3) (cpp) warning: no newline at end of file [-Wnewline-eof]``
"""
test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst"
init_config = config.RstcheckConfig(config_path=pathlib.Path("NONE"))

result = checker.check_file(test_file, init_config)

assert len(result) == 7

@staticmethod
def test_file_2_is_bad_without_config() -> None:
"""Test bad file ``bad_rst.rst`` without config file not ok."""
Expand Down
37 changes: 0 additions & 37 deletions tests/integration_tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ class TestWithoutConfigFile:
"""Test without config file in dir tree."""

@staticmethod
@pytest.mark.skipif(sys.platform == "darwin", reason="MacOS specific variant exists")
def test_error_without_config_file(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad example without set config file and implicit config file shows errors."""
test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst"
Expand All @@ -225,23 +224,6 @@ def test_error_without_config_file(capsys: pytest.CaptureFixture[str]) -> None:
assert result != 0
assert len(ERROR_CODE_REGEX.findall(capsys.readouterr().err)) == 6

@staticmethod
@pytest.mark.skipif(sys.platform != "darwin", reason="MacOS specific error count")
def test_error_without_config_file_macos(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad example without set config file and implicit config file shows errors.
On MacOS the cpp code block generates an additional error compared to linux:
``(ERROR/3) (cpp) warning: no newline at end of file [-Wnewline-eof]``
"""
test_file = EXAMPLES_DIR / "without_configuration" / "bad.rst"
init_config = config.RstcheckConfig()
_runner = runner.RstcheckMainRunner(check_paths=[test_file], rstcheck_config=init_config)

result = _runner.run()

assert result != 0
assert len(ERROR_CODE_REGEX.findall(capsys.readouterr().err)) == 7

@staticmethod
def test_no_error_with_set_ini_config_file(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad example with set INI config file does not error."""
Expand Down Expand Up @@ -287,7 +269,6 @@ class TestWithConfigFile:
"""Test with config file in dir tree."""

@staticmethod
@pytest.mark.skipif(sys.platform == "darwin", reason="MacOS specific variant exists")
def test_file_1_is_bad_without_config(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad file ``bad.rst`` without config file is not ok."""
test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst"
Expand All @@ -300,24 +281,6 @@ def test_file_1_is_bad_without_config(capsys: pytest.CaptureFixture[str]) -> Non
assert result != 0
assert len(ERROR_CODE_REGEX.findall(capsys.readouterr().err)) == 6

@staticmethod
@pytest.mark.skipif(sys.platform != "darwin", reason="MacOS specific error count")
def test_file_1_is_bad_without_config_macos(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad file ``bad.rst`` without config file is not ok.
On MacOS the cpp code block generates an additional error compared to linux:
``(ERROR/3) (cpp) warning: no newline at end of file [-Wnewline-eof]``
"""
test_file = EXAMPLES_DIR / "with_configuration" / "bad.rst"
config_file = pathlib.Path("NONE")
init_config = config.RstcheckConfig(config_path=config_file)
_runner = runner.RstcheckMainRunner(check_paths=[test_file], rstcheck_config=init_config)

result = _runner.run()

assert result != 0
assert len(ERROR_CODE_REGEX.findall(capsys.readouterr().err)) == 7

@staticmethod
def test_file_2_is_bad_without_config(capsys: pytest.CaptureFixture[str]) -> None:
"""Test bad file ``bad_rst.rst`` without config file not ok."""
Expand Down

0 comments on commit edd6f42

Please sign in to comment.