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

Code before:
            assert "Cannot use --line-ranges with ipynb files" in result.output
            assert result.exit_code == 1

    def test_line_ranges_in_pyproject_toml(self) -> None:
        config = THIS_DIR / "data" / "invalid_line_ranges.toml"
        result = BlackRunner().invoke(
            black.main, ["--code", "print()", "--config", str(config)]
        )
        assert result.exit_code == 2
        assert result.stderr_bytes is not None
        assert (
            b"Cannot use line-ranges in the pyproject.toml file." in result.stderr_bytes
        )

    def test_lines_with_leading_tabs_expanded(self) -> None:
        # See CVE-2024-21503. Mostly test that this completes in a reasonable
        # time.
        payload = "\t" * 10_000
        assert lines_with_leading_tabs_expanded(payload) == [payload]

        tab = " " * 8
        assert lines_with_leading_tabs_expanded("\tx") == [f"{tab}x"]
        assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"]
        assert lines_with_leading_tabs_expanded("\tx\n  y") == [f"{tab}x", "  y"]

    def test_carrige_return_edge_cases(self) -> None:
        # These tests are here instead of in the normal cases because
        # of git's newline normalization and because it's hard to
        # get `\r` vs `\r\n` vs `\n` to display properly
        assert (
            black.format_str(
                "try:\\\r# type: ignore\n pass\nfinally:\n pass\n",
                mode=black.FileMode(),
            )
            == "try:  # type: ignore\n    pass\nfinally:\n    pass\n"
        )
        assert black.format_str("{\r}", mode=black.FileMode()) == "{}\n"
        assert black.format_str("pass #\r#\n", mode=black.FileMode()) == "pass  #\n#\n"


class TestCaching:
    def test_get_cache_dir(
        self,
        tmp_path: Path,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        # Create multiple cache directories
        workspace1 = tmp_path / "ws1"
        workspace1.mkdir()
        workspace2 = tmp_path / "ws2"
        workspace2.mkdir()

Review comment (suggestion):
```suggestion
    def test_carriage_return_edge_cases(self) -> None:
```

Code after:
        # time.
        payload = "\t" * 10_000
        assert lines_with_leading_tabs_expanded(payload) == [payload]

        tab = " " * 8
        assert lines_with_leading_tabs_expanded("\tx") == [f"{tab}x"]
        assert lines_with_leading_tabs_expanded("\t\tx") == [f"{tab}{tab}x"]
        assert lines_with_leading_tabs_expanded("\tx\n  y") == [f"{tab}x", "  y"]

    def test_carriage_return_edge_cases(self) -> None:
        # These tests are here instead of in the normal cases because
        # of git's newline normalization and because it's hard to
        # get `\r` vs `\r\n` vs `\n` to display properly
        assert (
            black.format_str(
                "try:\\\r# type: ignore\n pass\nfinally:\n pass\n",
                mode=black.FileMode(),
            )
            == "try:  # type: ignore\n    pass\nfinally:\n    pass\n"
        )
        assert black.format_str("{\r}", mode=black.FileMode()) == "{}\n"
        assert black.format_str("pass #\r#\n", mode=black.FileMode()) == "pass  #\n#\n"

        assert black.format_str("x=\\\r\n1", mode=black.FileMode()) == "x = 1\n"
        assert black.format_str("x=\\\n1", mode=black.FileMode()) == "x = 1\n"
        assert black.format_str("x=\\\r1", mode=black.FileMode()) == "x = 1\n"
        assert (
            black.format_str("class A\\\r\n:...", mode=black.FileMode())
            == "class A: ...\n"
        )
        assert (
            black.format_str("class A\\\n:...", mode=black.FileMode())
            == "class A: ...\n"
        )
        assert (
            black.format_str("class A\\\r:...", mode=black.FileMode())
            == "class A: ...\n"
        )


class TestCaching:
    def test_get_cache_dir(
        self,
        tmp_path: Path,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        # Create multiple cache directories
        workspace1 = tmp_path / "ws1"
        workspace1.mkdir()
        workspace2 = tmp_path / "ws2"
        workspace2.mkdir()