Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for mac-style newlines #408

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/darker/tests/test_black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_filter_python_files( # pylint: disable=too-many-arguments


@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-1"])
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
def test_run_black(encoding, newline):
"""Running Black through its Python internal API gives correct results"""
src = TextDocument.from_lines(
Expand All @@ -193,7 +193,7 @@ def test_run_black(encoding, newline):
assert result.newline == newline


@pytest.mark.parametrize("newline", ["\n", "\r\n"])
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
def test_run_black_always_uses_unix_newlines(newline):
"""Content is always passed to Black with Unix newlines"""
src = TextDocument.from_str(f"print ( 'touché' ){newline}")
Expand Down Expand Up @@ -229,11 +229,13 @@ def test_run_black_ignores_excludes():
("", ""),
("\n", "\n"),
("\r\n", "\r\n"),
("\r", "\r"),
(" ", ""),
("\t", ""),
(" \t", ""),
(" \t\n", "\n"),
(" \t\r\n", "\r\n"),
(" \t\r", "\r"),
],
)
def test_run_black_all_whitespace_input(src_content, expect):
Expand Down
2 changes: 1 addition & 1 deletion src/darker/tests/test_import_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_import_sorting_importable_with_and_without_isort(present):


@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-1"])
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
@pytest.mark.kwparametrize(
dict(content=ORIGINAL_SOURCE, expect=ORIGINAL_SOURCE),
dict(content=("import sys", "import os"), expect=("import sys", "import os")),
Expand Down
14 changes: 11 additions & 3 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def test_isort_option_with_isort_calls_sortimports(tmpdir, run_isort, isort_args
black_exclude=set(),
isort_exclude=set(),
)
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
@pytest.mark.parametrize(
"newline", ["\n", "\r\n", "\r"], ids=["unix", "windows", "macos"]
)
def test_format_edited_parts(
git_repo, black_config, black_exclude, isort_exclude, newline, expect
):
Expand Down Expand Up @@ -492,7 +494,9 @@ def test_blacken_single_file(
expect_retval=0,
root_as_cwd=True,
)
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
@pytest.mark.parametrize(
"newline", ["\n", "\r\n", "\r"], ids=["unix", "windows", "macos"]
)
def test_main(
git_repo,
monkeypatch,
Expand Down Expand Up @@ -602,7 +606,7 @@ def test_main_in_plain_directory(tmp_path, capsys):
@pytest.mark.parametrize(
"encoding, text", [(b"utf-8", b"touch\xc3\xa9"), (b"iso-8859-1", b"touch\xe9")]
)
@pytest.mark.parametrize("newline", [b"\n", b"\r\n"])
@pytest.mark.parametrize("newline", [b"\n", b"\r\n", "\r"])
def test_main_encoding(
git_repo, find_project_root_cache_clear, encoding, text, newline
):
Expand Down Expand Up @@ -785,6 +789,10 @@ def test_print_diff(tmp_path, capsys):
new_content=TextDocument(lines=["touché"], newline="\r\n"),
expect=b"touch\xc3\xa9\r\n",
),
dict(
new_content=TextDocument(lines=["touché"], newline="\r"),
expect=b"touch\xc3\xa9\r",
),
dict(
new_content=TextDocument(lines=["touché"], encoding="iso-8859-1"),
expect=b"touch\xe9\n",
Expand Down
37 changes: 36 additions & 1 deletion src/darker/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ def textdocument(request, textdocument_factory):
dict(string="", expect="\n"),
dict(string="\n", expect="\n"),
dict(string="\r\n", expect="\r\n"),
dict(string="\r", expect="\r"),
dict(string="one line\n", expect="\n"),
dict(string="one line\r\n", expect="\r\n"),
dict(string="one line\r", expect="\r"),
dict(string="first line\nsecond line\n", expect="\n"),
dict(string="first line\r\nsecond line\r\n", expect="\r\n"),
dict(string="first line\rsecond line\r", expect="\r"),
dict(string="first unix\nthen windows\r\n", expect="\n"),
dict(string="first windows\r\nthen unix\n", expect="\r\n"),
dict(string="first unix\nthen macos\r", expect="\n"),
dict(string="first macos\rthen unix\n", expect="\r"),
dict(string="first macos\rthen windows\r\n", expect="\r"),
dict(string="first windows\r\nthen macos\r", expect="\r\n"),
)
def test_detect_newline(string, expect):
"""``detect_newline()`` gives correct results"""
Expand Down Expand Up @@ -157,13 +164,14 @@ def test_textdocument_set_encoding(textdocument, expect):
dict(
doc=TextDocument(lines=["zéro", "un"], newline="\r\n"), expect="zéro\r\nun\r\n"
),
dict(doc=TextDocument(lines=["zéro", "un"], newline="\r"), expect="zéro\run\r"),
)
def test_textdocument_string(doc, expect):
"""TextDocument.string respects the newline setting"""
assert doc.string == expect


@pytest.mark.parametrize("newline", ["\n", "\r\n"])
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
@pytest.mark.kwparametrize(
dict(textdocument=TextDocument(), expect=""),
dict(textdocument=TextDocument(lines=["zéro", "un"])),
Expand All @@ -172,6 +180,7 @@ def test_textdocument_string(doc, expect):
dict(textdocument=TextDocument(string="zéro\nun\n", newline="\n")),
dict(textdocument=TextDocument(lines=["zéro", "un"], newline="\r\n")),
dict(textdocument=TextDocument(string="zéro\r\nun\r\n", newline="\r\n")),
dict(textdocument=TextDocument(string="zéro\run\r", newline="\r")),
expect="zéro{newline}un{newline}",
)
def test_textdocument_string_with_newline(textdocument, newline, expect):
Expand All @@ -187,6 +196,8 @@ def test_textdocument_string_with_newline(textdocument, newline, expect):
dict(encoding="iso-8859-1", newline="\n", expect=b"z\xe9ro\nun\n"),
dict(encoding="utf-8", newline="\r\n", expect=b"z\xc3\xa9ro\r\nun\r\n"),
dict(encoding="iso-8859-1", newline="\r\n", expect=b"z\xe9ro\r\nun\r\n"),
dict(encoding="utf-8", newline="\r", expect=b"z\xc3\xa9ro\run\r"),
dict(encoding="iso-8859-1", newline="\r", expect=b"z\xe9ro\run\r"),
)
def test_textdocument_encoded_string(encoding, newline, expect):
"""TextDocument.encoded_string uses correct encoding and newline"""
Expand All @@ -204,6 +215,7 @@ def test_textdocument_encoded_string(encoding, newline, expect):
dict(
doc=TextDocument(string="zéro\r\nun\r\n", newline="\r\n"), expect=("zéro", "un")
),
dict(doc=TextDocument(string="zéro\run\r", newline="\r"), expect=("zéro", "un")),
)
def test_textdocument_lines(doc, expect):
"""TextDocument.lines is correct after parsing a string with different newlines"""
Expand Down Expand Up @@ -246,6 +258,13 @@ def test_textdocument_lines(doc, expect):
expect_newline="\r\n",
expect_mtime="",
),
dict(
textdocument=TextDocument.from_str("a\rb\r"),
expect_lines=("a", "b"),
expect_encoding="utf-8",
expect_newline="\r",
expect_mtime="",
),
dict(
textdocument=TextDocument.from_str("", mtime="my mtime"),
expect_lines=(),
Expand Down Expand Up @@ -278,6 +297,7 @@ def test_textdocument_detect_encoding(textdocument, expect):
@pytest.mark.kwparametrize(
dict(textdocument=b'print("unix")\n', expect="\n"),
dict(textdocument=b'print("windows")\r\n', expect="\r\n"),
dict(textdocument=b'print("macos")\r', expect="\r"),
indirect=["textdocument"],
)
def test_textdocument_detect_newline(textdocument, expect):
Expand All @@ -301,6 +321,11 @@ def test_textdocument_detect_newline(textdocument, expect):
doc2=TextDocument(lines=["line1", "line2"]),
expect=True,
),
dict(
doc1=TextDocument(lines=["line1", "line2"], encoding="utf-16", newline="\r"),
doc2=TextDocument(lines=["line1", "line2"]),
expect=True,
),
dict(doc1=TextDocument(lines=["foo"]), doc2=TextDocument(""), expect=False),
dict(doc1=TextDocument(lines=[]), doc2=TextDocument("foo\n"), expect=False),
dict(doc1=TextDocument(lines=["foo"]), doc2=TextDocument("bar\n"), expect=False),
Expand Down Expand Up @@ -330,6 +355,16 @@ def test_textdocument_detect_newline(textdocument, expect):
doc2=TextDocument("line1\nline2\n"),
expect=True,
),
dict(
doc1=TextDocument("line1\rline2\r"),
doc2=TextDocument("line1\nline2\n"),
expect=True,
),
dict(
doc1=TextDocument("line1\r\nline2\r\n"),
doc2=TextDocument("line1\rline2\r"),
expect=True,
),
dict(doc1=TextDocument("foo"), doc2="line1\nline2\n", expect=NotImplemented),
)
def test_textdocument_eq(doc1, doc2, expect):
Expand Down