Skip to content

Commit

Permalink
add test for blacken-docs on .md file
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Sep 13, 2022
1 parent a72916d commit 44d8ac8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -6,7 +6,7 @@
</h1>

<h3 align="center">
Run isort, pyupgrade, mypy, pylint, flake8, mdformat, black, blacken-docs, and more on Jupyter Notebooks
Run isort, pyupgrade, mypy, pylint, flake8, black, blacken-docs, and more on Jupyter Notebooks
</h3>

<p align="center">
Expand Down Expand Up @@ -102,10 +102,10 @@ $ nbqa pyupgrade my_notebook.ipynb --py37-plus
Rewriting my_notebook.ipynb
```

Format your markdown cells with [mdformat](https://mdformat.readthedocs.io/en/stable/index.html):
Format your markdown cells with [blacken-docs](https://github.com/asottile/blacken-docs):

```console
$ nbqa mdformat my_notebook.ipynb --nbqa-md --nbqa-diff
$ nbqa blacken-docs my_notebook.ipynb --nbqa-md --nbqa-diff
Cell 2
------
--- my_notebook.ipynb
Expand Down
6 changes: 3 additions & 3 deletions docs/configuration.rst
Expand Up @@ -140,20 +140,20 @@ Process markdown cells

You can process markdown cells (instead of code cells) by using the :code:`--nbqa-md` CLI argument.

This is useful when running tools which run on markdown files, such as ``mdformat``.
This is useful when running tools which run on markdown files, such as ``blacken-docs``.

For example, you could add the following to your :code:`pyproject.toml` file:

.. code-block:: toml
[tool.nbqa.md]
mdformat = true
blacken-docs = true
or, from the command-line:

.. code-block:: bash
nbqa mdformat notebook.ipynb --nbqa-md
nbqa blacken-docs notebook.ipynb --nbqa-md
Shell commands
~~~~~~~~~~~~~~
Expand Down
6 changes: 3 additions & 3 deletions docs/examples.rst
Expand Up @@ -85,11 +85,11 @@ Check docstring style with `pydocstyle`_:
$ nbqa pydocstyle my_notebook.ipynb
Format markdown cells with `mdformat`_:
Format markdown cells with `blacken-docs`_:

.. code:: console
$ nbqa mdformat my_notebook.ipynb --nbqa-md
$ nbqa blacken-docs my_notebook.ipynb --nbqa-md
Format ``.md`` file saved via `Jupytext`_:`

Expand All @@ -108,4 +108,4 @@ Format ``.md`` file saved via `Jupytext`_:`
.. _yapf: https://github.com/google/yapf
.. _autopep8: https://github.com/hhatto/autopep8
.. _pydocstyle: http://www.pydocstyle.org/en/stable/
.. _mdformat: https://mdformat.readthedocs.io/en/stable/index.html
.. _blacken-docs: https://github.com/asottile/blacken-docs
24 changes: 14 additions & 10 deletions nbqa/__main__.py
@@ -1,6 +1,5 @@
"""Run third-party tool (e.g. :code:`mypy`) against notebook or directory."""
import itertools
import json
import os
import re
import subprocess
Expand Down Expand Up @@ -115,13 +114,16 @@ def _get_notebooks(root_dir: str) -> Iterator[Path]:
"""
if not os.path.isdir(root_dir):
return iter((Path(root_dir),))
return (
i
for i in itertools.chain(
try:
pass
except ImportError:
iterable = Path(root_dir).rglob("*.ipynb")
else:
iterable = itertools.chain( # type: ignore
Path(root_dir).rglob("*.ipynb"), Path(root_dir).rglob("*.md")
)
if not re.search(EXCLUDES, str(i.resolve().as_posix()))
)

return (i for i in iterable if not re.search(EXCLUDES, str(i.resolve().as_posix())))


def _filter_by_include_exclude(
Expand Down Expand Up @@ -526,21 +528,23 @@ def _save_markdown_sources(
Record which notebooks fail to process.
"""
failed_notebooks = {}
non_python_notebooks = set()
nb_info_mapping: MutableMapping[str, NotebookInfo] = {}

for notebook, (file_descriptor, _) in nb_to_md_mapping.items():
with open(str(notebook), encoding="utf-8") as handle:
content = handle.read()
try:
notebook_json = json.loads(content)
notebook_json, _ = read_notebook(notebook)
if notebook_json is None or _is_non_python_notebook(notebook_json):
non_python_notebooks.add(notebook)
continue
nb_info_mapping[notebook] = save_markdown_source.main(
notebook_json,
file_descriptor,
skip_celltags,
)
except Exception as exp_repr: # pylint: disable=W0703
failed_notebooks[notebook] = repr(exp_repr)
return SavedSources(nb_info_mapping, failed_notebooks, set())
return SavedSources(nb_info_mapping, failed_notebooks, non_python_notebooks)


SAVE_SOURCES = {False: _save_code_sources, True: _save_markdown_sources}
Expand Down
41 changes: 41 additions & 0 deletions tests/test_jupytext.py
@@ -1,9 +1,13 @@
"""Test files saved via jupytext."""
import os
from pathlib import Path
from typing import TYPE_CHECKING

from nbqa.__main__ import main

if TYPE_CHECKING:
from _pytest.capture import CaptureFixture


def test_myst(tmp_test_data: Path) -> None:
"""
Expand Down Expand Up @@ -188,3 +192,40 @@ def test_jupytext_cant_parse() -> None:
"""Check file jupytext can't parse"""
ret = main(["black", os.path.join("tests", "invalid_data", "tracker.md")])
assert ret == 0


def test_jupytext_with_nbqa_md(capsys: "CaptureFixture") -> None:
"""Should work the same whether running on .md or .ipynb file"""
main(
[
"blacken-docs",
os.path.join("tests", "data", "notebook_for_testing.md"),
"--nbqa-md",
"--nbqa-diff",
]
)
out, _ = capsys.readouterr()
expected = (
"\x1b[1mCell 3\x1b[0m\n"
"------\n"
"\x1b[1;37m--- tests/data/notebook_for_testing.md\n"
"\x1b[0m\x1b[1;37m+++ tests/data/notebook_for_testing.md\n"
"\x1b[0m\x1b[36m@@ -1,3 +1,3 @@\n"
"\x1b[0m\x1b[31m-2 +2\n"
"\x1b[0m\x1b[32m+2 + 2\n"
"\x1b[0m\n"
"tests/data/notebook_for_testing.md: Rewriting...\n"
"To apply these changes, remove the `--nbqa-diff` flag\n"
)
assert out == expected

main(
[
"blacken-docs",
os.path.join("tests", "data", "notebook_for_testing.ipynb"),
"--nbqa-md",
"--nbqa-diff",
]
)
out, _ = capsys.readouterr()
assert out == expected.replace(".md", ".ipynb")
10 changes: 1 addition & 9 deletions tests/tools/test_mdformat.py
Expand Up @@ -13,15 +13,7 @@


def test_mdformat(tmp_notebook_for_testing: Path) -> None:
"""
Check pyupgrade works. Should only reformat code cells.
Parameters
----------
tmp_notebook_for_testing
Temporary copy of :code:`tmp_notebook_for_testing.ipynb`.
"""
# check diff
"""Check mdformat works"""
with open(tmp_notebook_for_testing, encoding="utf-8") as handle:
before = handle.readlines()
path = os.path.join("tests", "data", "notebook_for_testing.ipynb")
Expand Down

0 comments on commit 44d8ac8

Please sign in to comment.