Repository: psf/black
File: tests/test_black.py (Python)

Code before:
    @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
        )

    def test_piping(self) -> None:
        source, expected = read_data_from_file(PROJECT_ROOT / "src/black/__init__.py")
        result = BlackRunner().invoke(
            black.main,
            [
                "-",
                "--fast",
                f"--line-length={black.DEFAULT_LINE_LENGTH}",
                f"--config={EMPTY_CONFIG}",
            ],
            input=BytesIO(source.encode("utf8")),
        )
        self.assertEqual(result.exit_code, 0)
        self.assertFormatEqual(expected, result.output)
        if source != result.output:
            black.assert_equivalent(source, result.output)
            black.assert_stable(source, result.output, DEFAULT_MODE)

    def test_piping_diff(self) -> None:
        diff_header = re.compile(
            r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d "
            r"\+\d\d\d\d"
        )
        source, _ = read_data("simple_cases", "expression.py")
        expected, _ = read_data("simple_cases", "expression.diff")

Review comment (bug):
The tests failing on Windows seem to confirm that writing a newline actually writes a `\r\n` instead of `\n`.

```
Expected tree:
file_input
  ENDMARKER '\n' ''
/file_input
Actual tree:
file_input
  ENDMARKER '\r\n' ''
/file_input
```

I'm getting this on Windows:

```python
>>> with open("temp.py", "w") as f:
...     f.write("\n")
...
1
>>>
>>> with open("temp.py", "rb") as f:
...     print(f.read())
...
b'\r\n'                
```

Code after:
    @patch("black.dump_to_file", dump_to_stderr)
    def test_one_empty_line(self) -> None:
        mode = black.Mode(preview=True)
        for nl in ["\n", "\r\n"]:
            source = expected = nl
            assert_format(source, expected, mode=mode)

    def test_one_empty_line_ff(self) -> None:
        mode = black.Mode(preview=True)
        for nl in ["\n", "\r\n"]:
            expected = nl
            tmp_file = Path(black.dump_to_file(nl))
            if system() == "Windows":
                # Writing files in text mode automatically uses the system newline,
                # but in this case we don't want this for testing reasons. See:
                # https://github.com/psf/black/pull/3348
                with open(tmp_file, "wb") as f:
                    f.write(nl.encode("utf-8"))
            try:
                self.assertFalse(
                    ff(tmp_file, mode=mode, 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
        )

    def test_piping(self) -> None:
        source, expected = read_data_from_file(PROJECT_ROOT / "src/black/__init__.py")
        result = BlackRunner().invoke(
            black.main,
            [
                "-",
                "--fast",
                f"--line-length={black.DEFAULT_LINE_LENGTH}",
                f"--config={EMPTY_CONFIG}",
            ],
            input=BytesIO(source.encode("utf8")),
        )
        self.assertEqual(result.exit_code, 0)
        self.assertFormatEqual(expected, result.output)
        if source != result.output:
            black.assert_equivalent(source, result.output)
            black.assert_stable(source, result.output, DEFAULT_MODE)