From 88392e4d58c213d89d7211ba440d3e78f61e2bd0 Mon Sep 17 00:00:00 2001 From: Preocts Date: Sun, 22 Jun 2025 14:41:40 -0400 Subject: [PATCH] Remove extra newline created on write The write process was creating an extra newline at the end of the file. Now it won't do that. If the input file does not end with a newline, the written result will. As all files should. --- src/commentedconfigparser/commentedconfigparser.py | 3 +++ tests/commentedconfigparser_test.py | 6 +++--- tests/empty_comments_expected.ini | 1 - tests/header_expected.ini | 1 - tests/multi_expected.ini | 1 - tests/pydocs_expected.ini | 1 - tests/regression_original_expected.ini | 1 - 7 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/commentedconfigparser/commentedconfigparser.py b/src/commentedconfigparser/commentedconfigparser.py index 3b04823..5ad4a61 100644 --- a/src/commentedconfigparser/commentedconfigparser.py +++ b/src/commentedconfigparser/commentedconfigparser.py @@ -127,4 +127,7 @@ def _restore_comments(self, content: str) -> str: rendered.append(line + "\n") + # Remove extra trailing newline + rendered[-1] = rendered[-1].rstrip() + return "".join(rendered) diff --git a/tests/commentedconfigparser_test.py b/tests/commentedconfigparser_test.py index c1513c2..814f8a5 100644 --- a/tests/commentedconfigparser_test.py +++ b/tests/commentedconfigparser_test.py @@ -128,7 +128,7 @@ def test_regression_read_dict_loads_normally() -> None: def test_regression_write_normally() -> None: cc = CommentedConfigParser() - expected = "[TEST]\ntest=pass\n\n" + expected = "[TEST]\ntest=pass\n" cc.read_string(expected) mock_file = StringIO() @@ -139,7 +139,7 @@ def test_regression_write_normally() -> None: def test_write_with_no_comments() -> None: cc = CommentedConfigParser() - expected = "[TEST]\ntest=pass\n\n" + expected = "[TEST]\ntest=pass\n" cc.read_dict({"TEST": {"test": "pass"}}) mock_file = StringIO() @@ -181,7 +181,7 @@ def test_issue_46_duplicating_sections(tmp_path: Path) -> None: # https://github.com/Preocts/commented-configparser/issues/46 tmp_file = tmp_path / "issue46_test_file.ini" starting_config = "[example]\nfoo = 0\n" - expected = "[example]\nfoo = 9\n\n" + expected = "[example]\nfoo = 9\n" tmp_file.write_text(starting_config, "utf-8") cc = CommentedConfigParser() cc.read(tmp_file) diff --git a/tests/empty_comments_expected.ini b/tests/empty_comments_expected.ini index 1d3abce..ea13022 100644 --- a/tests/empty_comments_expected.ini +++ b/tests/empty_comments_expected.ini @@ -12,4 +12,3 @@ bar = foo [BIZ] baz = bar - diff --git a/tests/header_expected.ini b/tests/header_expected.ini index 7c8c045..29e8e96 100644 --- a/tests/header_expected.ini +++ b/tests/header_expected.ini @@ -8,4 +8,3 @@ foo = bar # Comment here bar = foo - diff --git a/tests/multi_expected.ini b/tests/multi_expected.ini index 8c1e012..3390309 100644 --- a/tests/multi_expected.ini +++ b/tests/multi_expected.ini @@ -8,4 +8,3 @@ foo = bar unique to multi02 = foo ; A nice little comment here from multi01 unique to multi01 = foo - diff --git a/tests/pydocs_expected.ini b/tests/pydocs_expected.ini index ea54312..dcb500b 100644 --- a/tests/pydocs_expected.ini +++ b/tests/pydocs_expected.ini @@ -40,4 +40,3 @@ multiline_values = are deeper than the first line of a value # Did I mention we can indent comments, too? - diff --git a/tests/regression_original_expected.ini b/tests/regression_original_expected.ini index 90482e4..f4da53f 100644 --- a/tests/regression_original_expected.ini +++ b/tests/regression_original_expected.ini @@ -18,4 +18,3 @@ multi-line = value03 closing = 0 # Trailing comment -