From c3d8197309e3ee248ffd350d5185bc2eb52f7e9c Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Mon, 14 Nov 2022 20:46:20 +0200 Subject: [PATCH] Add test cases for mac-style newlines See https://github.com/akaihola/darker/pull/377#discussion_r957014910 --- src/darker/tests/test_black_diff.py | 6 ++-- src/darker/tests/test_import_sorting.py | 2 +- src/darker/tests/test_main.py | 14 ++++++++-- src/darker/tests/test_utils.py | 37 ++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/darker/tests/test_black_diff.py b/src/darker/tests/test_black_diff.py index dac35f256..1d9c71a00 100644 --- a/src/darker/tests/test_black_diff.py +++ b/src/darker/tests/test_black_diff.py @@ -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( @@ -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}") @@ -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): diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index a624102b5..b71d6aba9 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -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")), diff --git a/src/darker/tests/test_main.py b/src/darker/tests/test_main.py index 862025736..f06678a6a 100644 --- a/src/darker/tests/test_main.py +++ b/src/darker/tests/test_main.py @@ -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 ): @@ -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, @@ -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 ): @@ -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", diff --git a/src/darker/tests/test_utils.py b/src/darker/tests/test_utils.py index b124919b8..dbb6711b6 100644 --- a/src/darker/tests/test_utils.py +++ b/src/darker/tests/test_utils.py @@ -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""" @@ -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"])), @@ -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): @@ -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""" @@ -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""" @@ -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=(), @@ -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): @@ -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), @@ -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):