Skip to content

Commit

Permalink
First test for jupytext + black #154
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 22, 2019
1 parent 718f8ad commit 9ff791f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flake8
black
pytest
pytest-cov==2.5.1
codecov
Expand Down
46 changes: 46 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest
from shutil import copyfile
from testfixtures import compare
from .utils import list_notebooks
from jupytext import readf
from jupytext.cli import system, jupytext


def black_version():
try:
return system('black', ['--version'])
except FileNotFoundError: # pragma: no cover
return None


requires_black = pytest.mark.skipif(not black_version(), reason='black not found')


def remove_spaces_and_commas(text, chars=[' ', '\n', ',', "'", '"']):
for char in chars:
text = text.replace(char, '')
return text


@requires_black
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
def test_apply_black_on_python_notebooks(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
copyfile(nb_file, tmp_ipynb)

jupytext(args=[tmp_ipynb, '--to', 'py:percent'])
system('black', tmp_py)
jupytext(args=[tmp_py, '--to', 'ipynb', '--update'])

nb1 = readf(nb_file)
nb2 = readf(tmp_ipynb)

assert len(nb1.cells) == len(nb2.cells)
for c1, c2 in zip(nb1.cells, nb2.cells):
# same content (almost)
assert remove_spaces_and_commas(c1.source) == remove_spaces_and_commas(c2.source)
# python representation is pep8
assert 'lines_to_next_cell' not in c2.metadata

compare(nb1.metadata, nb2.metadata)

0 comments on commit 9ff791f

Please sign in to comment.