Skip to content

Commit

Permalink
Test expected behavior on whitespace-only input
Browse files Browse the repository at this point in the history
This commit introduces two tests and modifies other two tests. The
introduced tests verify the expected behaviour on files containing a
single newline character via 1) `format_str` and 2)
`format_file_in_place`. In the other hand, the first modified test
(`test_format_file_contents`) verify that `NothingChanged` is raised
on files containing only a single newline, and that whitespace-only
files are properly formatted to a single newline character. The second
modified test (`test_reformat_one_with_stdin_empty`) validates the
expected behavior when the input is passed via stdin.

Before the fix introduced in the previous commit, these tests (and a
couple of others) failed on cases covering whitespace-only files. Now,
these tests are passed for all cases.

Signed-off-by: Antonio Ossa Guerra <[email protected]>
  • Loading branch information
aaossa committed Oct 21, 2022
1 parent 74861f0 commit e134d90
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
6 changes: 6 additions & 0 deletions tests/data/simple_cases/whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@





# output
81 changes: 69 additions & 12 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ def test_empty_ff(self) -> None:
os.unlink(tmp_file)
self.assertFormatEqual(expected, actual)

@patch("black.dump_to_file", dump_to_stderr)
def test_one_empty_line(self) -> None:
for nl in ["\n", "\r\n"]:
source = expected = nl
actual = fs(source)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, DEFAULT_MODE)

def test_one_empty_line_ff(self) -> None:
for nl in ["\n", "\r\n"]:
expected = nl
tmp_file = Path(black.dump_to_file(nl))
try:
self.assertFalse(ff(tmp_file, write_back=black.WriteBack.YES))
with open(tmp_file, "rb") as f:
actual = f.read().decode("utf8")
finally:
os.unlink(tmp_file)
self.assertFormatEqual(expected, actual)

def test_experimental_string_processing_warns(self) -> None:
self.assertWarns(
black.mode.Deprecated, black.Mode, experimental_string_processing=True
Expand Down Expand Up @@ -936,6 +957,15 @@ def test_format_file_contents(self) -> None:
just_nl = "\n"
with self.assertRaises(black.NothingChanged):
black.format_file_contents(just_nl, mode=mode, fast=False)
just_crlf = "\r\n"
with self.assertRaises(black.NothingChanged):
black.format_file_contents(just_crlf, mode=mode, fast=False)
just_whitespace_nl = "\n\t\n \n\t \n \t\n\n"
actual = black.format_file_contents(just_whitespace_nl, mode=mode, fast=False)
self.assertEqual("\n", actual)
just_whitespace_crlf = "\r\n\t\r\n \r\n\t \r\n \t\r\n\r\n"
actual = black.format_file_contents(just_whitespace_crlf, mode=mode, fast=False)
self.assertEqual("\r\n", actual)
same = "j = [1, 2, 3]\n"
with self.assertRaises(black.NothingChanged):
black.format_file_contents(same, mode=mode, fast=False)
Expand Down Expand Up @@ -1239,18 +1269,45 @@ def test_reformat_one_with_stdin_and_existing_path(self) -> None:
report.done.assert_called_with(expected, black.Changed.YES)

def test_reformat_one_with_stdin_empty(self) -> None:
output = io.StringIO()
with patch("io.TextIOWrapper", lambda *args, **kwargs: output):
try:
black.format_stdin_to_stdout(
fast=True,
content="",
write_back=black.WriteBack.YES,
mode=DEFAULT_MODE,
)
except io.UnsupportedOperation:
pass # StringIO does not support detach
assert output.getvalue() == ""
cases = [
("", ""),
("\n", "\n"),
("\r\n", "\r\n"),
(" \t", ""),
(" \t\n\t ", "\n"),
(" \t\r\n\t ", "\r\n"),
]

def _new_wrapper(
output: io.StringIO, io_TextIOWrapper: io.TextIOWrapper
) -> Callable[[Any, Any], io.TextIOWrapper]:
def get_output(*args: Any, **kwargs: Any) -> io.TextIOWrapper:
if args == (sys.stdout.buffer,):
# It's `format_stdin_to_stdout()` calling `io.TextIOWrapper()`,
# return our mock object.
return output
# It's something else (i.e. `decode_bytes()`) calling
# `io.TextIOWrapper()`, pass through to the original implementation.
# See discussion in https://github.com/psf/black/pull/2489
return io_TextIOWrapper(*args, **kwargs)

return get_output

for content, expected in cases:
output = io.StringIO()
io_TextIOWrapper = io.TextIOWrapper

with patch("io.TextIOWrapper", _new_wrapper(output, io_TextIOWrapper)):
try:
black.format_stdin_to_stdout(
fast=True,
content=content,
write_back=black.WriteBack.YES,
mode=DEFAULT_MODE,
)
except io.UnsupportedOperation:
pass # StringIO does not support detach
assert output.getvalue() == expected

def test_invalid_cli_regex(self) -> None:
for option in ["--include", "--exclude", "--extend-exclude", "--force-exclude"]:
Expand Down

0 comments on commit e134d90

Please sign in to comment.