From a9eeecd6d295d9f3141662c1738709794d242084 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Sun, 13 Dec 2020 12:09:40 -0600 Subject: [PATCH] Demonstrate issue #14 --- tests/test_collect.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tests/test_collect.py b/tests/test_collect.py index a76251e..240eea4 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -3,6 +3,7 @@ from unittest.mock import call import freezegun +import pytest COMMENT = """\ .. this line should be dropped @@ -343,3 +344,85 @@ def test_collect_version_in_config(cli_invoke, changelog_d, temp_dir): + "- The first change.\n" ) assert changelog_text == expected + + +def test_collect_linux_newlines(cli_invoke, changelog_d, temp_dir): + """Verify that existing Linux newlines are preserved during collection.""" + + changelog = temp_dir / "CHANGELOG.rst" + existing_text = "Line one\nLine two" + with changelog.open("wb") as file: + file.write(existing_text.encode("utf8")) + (changelog_d / "20170616_nedbat.rst").write_text(COMMENT + FRAG1 + COMMENT) + with freezegun.freeze_time("2020-02-25T15:18:19"): + cli_invoke(["collect"]) + with changelog.open("rb") as file: + new_text = file.read().decode("utf8") + windows_newline_count = new_text.count("\r\n") + linux_newline_count = new_text.count("\n") - windows_newline_count + mac_newline_count = new_text.count("\r") - windows_newline_count + assert linux_newline_count, "Linux newlines were not preserved" + assert mac_newline_count == 0, "Linux newlines were not preserved" + assert windows_newline_count == 0, "Linux newlines were not preserved" + + +def test_collect_windows_newlines(cli_invoke, changelog_d, temp_dir): + """Verify that existing Windows newlines are preserved during collection.""" + + changelog = temp_dir / "CHANGELOG.rst" + existing_text = "Line one\r\nLine two" + with changelog.open("wb") as file: + file.write(existing_text.encode("utf8")) + (changelog_d / "20170616_nedbat.rst").write_text(COMMENT + FRAG1 + COMMENT) + with freezegun.freeze_time("2020-02-25T15:18:19"): + cli_invoke(["collect"]) + with changelog.open("rb") as file: + new_text = file.read().decode("utf8") + windows_newline_count = new_text.count("\r\n") + linux_newline_count = new_text.count("\n") - windows_newline_count + mac_newline_count = new_text.count("\r") - windows_newline_count + assert windows_newline_count, "Windows newlines were not preserved" + assert linux_newline_count == 0, "Windows newlines were not preserved" + assert mac_newline_count == 0, "Windows newlines were not preserved" + + +@pytest.mark.parametrize( + "platform, newline", + ( + ("Windows", "\r\n"), + ("Linux", "\n"), + ("Mac", "\r"), + ), +) +def test_collect_respect_existing_newlines( + cli_invoke, + changelog_d, + temp_dir, + platform, + newline, +): + """Verify that existing newline styles are preserved during collection.""" + + index_map = { + "\r\n": 0, + "\n": 1, + "\r": 2, + } + + changelog = temp_dir / "CHANGELOG.rst" + existing_text = "Line one" + newline + "Line two" + with changelog.open("wb") as file: + file.write(existing_text.encode("utf8")) + (changelog_d / "20170616_nedbat.rst").write_text(COMMENT + FRAG1 + COMMENT) + with freezegun.freeze_time("2020-02-25T15:18:19"): + cli_invoke(["collect"]) + with changelog.open("rb") as file: + new_text = file.read().decode("utf8") + + counts = [new_text.count("\r\n")] # Windows + counts.append(new_text.count("\n") - counts[0]) # Linux + counts.append(new_text.count("\r") - counts[0]) # Mac + + msg = platform + " newlines were not preserved" + assert counts.pop(index_map[newline]), msg + assert counts == [0, 0], msg