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

Code before:


def test_non_python_notebook() -> None:
    with open(DATA_DIR / "non_python_notebook.ipynb", "rb") as fd:
        content_bytes = fd.read()
    content = content_bytes.decode()
    with pytest.raises(NothingChanged):
        format_file_contents(content, fast=True, mode=JUPYTER_MODE)


def test_empty_string() -> None:
    with pytest.raises(NothingChanged):
        format_file_contents("", fast=True, mode=JUPYTER_MODE)


def test_unparseable_notebook() -> None:
    msg = (
        rf"File '{DATA_DIR}[/\\]notebook_which_cant_be_parsed.ipynb' "
        r"cannot be parsed as valid Jupyter notebook\."
    )
    with pytest.raises(ValueError, match=msg):
        format_file_in_place(
            DATA_DIR / "notebook_which_cant_be_parsed.ipynb",
            fast=True,
            mode=JUPYTER_MODE,
        )


def test_ipynb_diff_with_change() -> None:
    result = runner.invoke(
        main,
        [
            str(DATA_DIR / "notebook_trailing_newline.ipynb"),
            "--diff",
        ],
    )
    expected = "@@ -1,3 +1,3 @@\n %%time\n \n-print('foo')\n" '+print("foo")\n'
    assert expected in result.output


def test_ipynb_diff_with_no_change() -> None:
    result = runner.invoke(
        main,
        [
            str(DATA_DIR / "notebook_without_changes.ipynb"),
            "--diff",
        ],
    )
    expected = "1 file would be left unchanged."
    assert expected in result.output


Review comment (bug):
this seems to fail the Windows checks...perhaps writing it as
```python
    path = DATA_DIR / "notebook_which_cant_be_parsed.ipynb"
    msg = rf"File '{re.escape(path)}' cannot be parsed as valid Jupyter notebook\."
    with pytest.raises(ValueError, match=msg):
        format_file_in_place(path, fast=True, mode=JUPYTER_MODE)
```
(if you import `re` somewhere) works?

Code after:
    with pytest.raises(NothingChanged):
        format_file_contents(content, fast=True, mode=JUPYTER_MODE)


def test_non_python_notebook() -> None:
    with open(DATA_DIR / "non_python_notebook.ipynb", "rb") as fd:
        content_bytes = fd.read()
    content = content_bytes.decode()
    with pytest.raises(NothingChanged):
        format_file_contents(content, fast=True, mode=JUPYTER_MODE)


def test_empty_string() -> None:
    with pytest.raises(NothingChanged):
        format_file_contents("", fast=True, mode=JUPYTER_MODE)


def test_unparseable_notebook() -> None:
    path = DATA_DIR / "notebook_which_cant_be_parsed.ipynb"
    msg = rf"File '{re.escape(str(path))}' cannot be parsed as valid Jupyter notebook\."
    with pytest.raises(ValueError, match=msg):
        format_file_in_place(path, fast=True, mode=JUPYTER_MODE)


def test_ipynb_diff_with_change() -> None:
    result = runner.invoke(
        main,
        [
            str(DATA_DIR / "notebook_trailing_newline.ipynb"),
            "--diff",
        ],
    )
    expected = "@@ -1,3 +1,3 @@\n %%time\n \n-print('foo')\n" '+print("foo")\n'
    assert expected in result.output


def test_ipynb_diff_with_no_change() -> None:
    result = runner.invoke(
        main,
        [
            str(DATA_DIR / "notebook_without_changes.ipynb"),
            "--diff",
        ],
    )
    expected = "1 file would be left unchanged."
    assert expected in result.output


def test_cache_isnt_written_if_no_jupyter_deps_single(
    monkeypatch: MonkeyPatch, tmpdir: local
) -> None: