Skip to content

Commit

Permalink
Demonstrate issue nedbat#14
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Dec 13, 2020
1 parent 030663d commit 2e2de1a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import call

import freezegun
import pytest

COMMENT = """\
.. this line should be dropped
Expand Down Expand Up @@ -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_mac_newlines(
cli_invoke,
changelog_d,
temp_dir,
platform,
newline,
):
"""Verify that existing newlines 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

0 comments on commit 2e2de1a

Please sign in to comment.