Skip to content

Commit cf22df4

Browse files
committed
Add test cases for mac-style newlines
See #377 (comment)
1 parent 63daf50 commit cf22df4

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

src/darker/tests/test_black_diff.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_filter_python_files( # pylint: disable=too-many-arguments
174174

175175

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

195195

196-
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
196+
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
197197
def test_run_black_always_uses_unix_newlines(newline):
198198
"""Content is always passed to Black with Unix newlines"""
199199
src = TextDocument.from_str(f"print ( 'touché' ){newline}")
@@ -229,11 +229,13 @@ def test_run_black_ignores_excludes():
229229
("", ""),
230230
("\n", "\n"),
231231
("\r\n", "\r\n"),
232+
("\r", "\r"),
232233
(" ", ""),
233234
("\t", ""),
234235
(" \t", ""),
235236
(" \t\n", "\n"),
236237
(" \t\r\n", "\r\n"),
238+
(" \t\r", "\r"),
237239
],
238240
)
239241
def test_run_black_all_whitespace_input(src_content, expect):

src/darker/tests/test_import_sorting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_import_sorting_importable_with_and_without_isort(present):
3131

3232

3333
@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-1"])
34-
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
34+
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
3535
@pytest.mark.kwparametrize(
3636
dict(content=ORIGINAL_SOURCE, expect=ORIGINAL_SOURCE),
3737
dict(content=("import sys", "import os"), expect=("import sys", "import os")),

src/darker/tests/test_main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ def test_isort_option_with_isort_calls_sortimports(tmpdir, run_isort, isort_args
152152
black_exclude=set(),
153153
isort_exclude=set(),
154154
)
155-
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
155+
@pytest.mark.parametrize(
156+
"newline", ["\n", "\r\n", "\r"], ids=["unix", "windows", "macos"]
157+
)
156158
def test_format_edited_parts(
157159
git_repo, black_config, black_exclude, isort_exclude, newline, expect
158160
):
@@ -492,7 +494,9 @@ def test_blacken_single_file(
492494
expect_retval=0,
493495
root_as_cwd=True,
494496
)
495-
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
497+
@pytest.mark.parametrize(
498+
"newline", ["\n", "\r\n", "\r"], ids=["unix", "windows", "macos"]
499+
)
496500
def test_main(
497501
git_repo,
498502
monkeypatch,
@@ -602,7 +606,7 @@ def test_main_in_plain_directory(tmp_path, capsys):
602606
@pytest.mark.parametrize(
603607
"encoding, text", [(b"utf-8", b"touch\xc3\xa9"), (b"iso-8859-1", b"touch\xe9")]
604608
)
605-
@pytest.mark.parametrize("newline", [b"\n", b"\r\n"])
609+
@pytest.mark.parametrize("newline", [b"\n", b"\r\n", "\r"])
606610
def test_main_encoding(
607611
git_repo, find_project_root_cache_clear, encoding, text, newline
608612
):
@@ -785,6 +789,10 @@ def test_print_diff(tmp_path, capsys):
785789
new_content=TextDocument(lines=["touché"], newline="\r\n"),
786790
expect=b"touch\xc3\xa9\r\n",
787791
),
792+
dict(
793+
new_content=TextDocument(lines=["touché"], newline="\r"),
794+
expect=b"touch\xc3\xa9\r",
795+
),
788796
dict(
789797
new_content=TextDocument(lines=["touché"], encoding="iso-8859-1"),
790798
expect=b"touch\xe9\n",

src/darker/tests/test_utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ def textdocument(request, textdocument_factory):
5656
dict(string="", expect="\n"),
5757
dict(string="\n", expect="\n"),
5858
dict(string="\r\n", expect="\r\n"),
59+
dict(string="\r", expect="\r"),
5960
dict(string="one line\n", expect="\n"),
6061
dict(string="one line\r\n", expect="\r\n"),
62+
dict(string="one line\r", expect="\r"),
6163
dict(string="first line\nsecond line\n", expect="\n"),
6264
dict(string="first line\r\nsecond line\r\n", expect="\r\n"),
65+
dict(string="first line\rsecond line\r", expect="\r"),
6366
dict(string="first unix\nthen windows\r\n", expect="\n"),
6467
dict(string="first windows\r\nthen unix\n", expect="\r\n"),
68+
dict(string="first unix\nthen macos\r", expect="\n"),
69+
dict(string="first macos\rthen unix\n", expect="\r"),
70+
dict(string="first macos\rthen windows\r\n", expect="\r"),
71+
dict(string="first windows\r\nthen macos\r", expect="\r\n"),
6572
)
6673
def test_detect_newline(string, expect):
6774
"""``detect_newline()`` gives correct results"""
@@ -157,13 +164,14 @@ def test_textdocument_set_encoding(textdocument, expect):
157164
dict(
158165
doc=TextDocument(lines=["zéro", "un"], newline="\r\n"), expect="zéro\r\nun\r\n"
159166
),
167+
dict(doc=TextDocument(lines=["zéro", "un"], newline="\r"), expect="zéro\run\r"),
160168
)
161169
def test_textdocument_string(doc, expect):
162170
"""TextDocument.string respects the newline setting"""
163171
assert doc.string == expect
164172

165173

166-
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
174+
@pytest.mark.parametrize("newline", ["\n", "\r\n", "\r"])
167175
@pytest.mark.kwparametrize(
168176
dict(textdocument=TextDocument(), expect=""),
169177
dict(textdocument=TextDocument(lines=["zéro", "un"])),
@@ -172,6 +180,7 @@ def test_textdocument_string(doc, expect):
172180
dict(textdocument=TextDocument(string="zéro\nun\n", newline="\n")),
173181
dict(textdocument=TextDocument(lines=["zéro", "un"], newline="\r\n")),
174182
dict(textdocument=TextDocument(string="zéro\r\nun\r\n", newline="\r\n")),
183+
dict(textdocument=TextDocument(string="zéro\run\r", newline="\r")),
175184
expect="zéro{newline}un{newline}",
176185
)
177186
def test_textdocument_string_with_newline(textdocument, newline, expect):
@@ -187,6 +196,8 @@ def test_textdocument_string_with_newline(textdocument, newline, expect):
187196
dict(encoding="iso-8859-1", newline="\n", expect=b"z\xe9ro\nun\n"),
188197
dict(encoding="utf-8", newline="\r\n", expect=b"z\xc3\xa9ro\r\nun\r\n"),
189198
dict(encoding="iso-8859-1", newline="\r\n", expect=b"z\xe9ro\r\nun\r\n"),
199+
dict(encoding="utf-8", newline="\r", expect=b"z\xc3\xa9ro\run\r"),
200+
dict(encoding="iso-8859-1", newline="\r", expect=b"z\xe9ro\run\r"),
190201
)
191202
def test_textdocument_encoded_string(encoding, newline, expect):
192203
"""TextDocument.encoded_string uses correct encoding and newline"""
@@ -204,6 +215,7 @@ def test_textdocument_encoded_string(encoding, newline, expect):
204215
dict(
205216
doc=TextDocument(string="zéro\r\nun\r\n", newline="\r\n"), expect=("zéro", "un")
206217
),
218+
dict(doc=TextDocument(string="zéro\run\r", newline="\r"), expect=("zéro", "un")),
207219
)
208220
def test_textdocument_lines(doc, expect):
209221
"""TextDocument.lines is correct after parsing a string with different newlines"""
@@ -246,6 +258,13 @@ def test_textdocument_lines(doc, expect):
246258
expect_newline="\r\n",
247259
expect_mtime="",
248260
),
261+
dict(
262+
textdocument=TextDocument.from_str("a\rb\r"),
263+
expect_lines=("a", "b"),
264+
expect_encoding="utf-8",
265+
expect_newline="\r",
266+
expect_mtime="",
267+
),
249268
dict(
250269
textdocument=TextDocument.from_str("", mtime="my mtime"),
251270
expect_lines=(),
@@ -278,6 +297,7 @@ def test_textdocument_detect_encoding(textdocument, expect):
278297
@pytest.mark.kwparametrize(
279298
dict(textdocument=b'print("unix")\n', expect="\n"),
280299
dict(textdocument=b'print("windows")\r\n', expect="\r\n"),
300+
dict(textdocument=b'print("macos")\r', expect="\r"),
281301
indirect=["textdocument"],
282302
)
283303
def test_textdocument_detect_newline(textdocument, expect):
@@ -301,6 +321,11 @@ def test_textdocument_detect_newline(textdocument, expect):
301321
doc2=TextDocument(lines=["line1", "line2"]),
302322
expect=True,
303323
),
324+
dict(
325+
doc1=TextDocument(lines=["line1", "line2"], encoding="utf-16", newline="\r"),
326+
doc2=TextDocument(lines=["line1", "line2"]),
327+
expect=True,
328+
),
304329
dict(doc1=TextDocument(lines=["foo"]), doc2=TextDocument(""), expect=False),
305330
dict(doc1=TextDocument(lines=[]), doc2=TextDocument("foo\n"), expect=False),
306331
dict(doc1=TextDocument(lines=["foo"]), doc2=TextDocument("bar\n"), expect=False),
@@ -330,6 +355,16 @@ def test_textdocument_detect_newline(textdocument, expect):
330355
doc2=TextDocument("line1\nline2\n"),
331356
expect=True,
332357
),
358+
dict(
359+
doc1=TextDocument("line1\rline2\r"),
360+
doc2=TextDocument("line1\nline2\n"),
361+
expect=True,
362+
),
363+
dict(
364+
doc1=TextDocument("line1\r\nline2\r\n"),
365+
doc2=TextDocument("line1\rline2\r"),
366+
expect=True,
367+
),
333368
dict(doc1=TextDocument("foo"), doc2="line1\nline2\n", expect=NotImplemented),
334369
)
335370
def test_textdocument_eq(doc1, doc2, expect):

0 commit comments

Comments
 (0)