Skip to content

Commit

Permalink
Test pre-commit hook with black and flake8 #142
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 28, 2019
1 parent 09335d5 commit 97eb6bb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
4 changes: 2 additions & 2 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ def long_form_one_format(jupytext_format, metadata=None):
if not ext.startswith('.'):
ext = '.' + ext

if ext == '.auto' and metadata:
ext = auto_ext_from_metadata(metadata)
if ext == '.auto':
ext = auto_ext_from_metadata(metadata) or '.py'

fmt['extension'] = ext
return fmt
Expand Down
15 changes: 2 additions & 13 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
from shutil import copyfile
from testfixtures import compare
from nbformat.v4.nbbase import new_notebook, new_code_cell
from .utils import list_notebooks
from .utils import list_notebooks, requires_black, requires_flake8, requires_autopep8

from jupytext import readf, writef
from jupytext.cli import system, jupytext, pipe_notebook
from jupytext.combine import black_invariant


def tool_version(tool):
try:
return system(tool, '--version')
except OSError: # pragma: no cover
return None


requires_black = pytest.mark.skipif(not tool_version('black'), reason='black not found')
requires_flake8 = pytest.mark.skipif(not tool_version('flake8'), reason='flake8 not found')
requires_autopep8 = pytest.mark.skipif(not tool_version('autopep8'), reason='autopep8 not found')


@requires_black
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py'))
def test_apply_black_on_python_notebooks(nb_file, tmpdir):
Expand Down
45 changes: 43 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from shutil import copyfile
from testfixtures import compare
from argparse import ArgumentTypeError
from nbformat.v4.nbbase import new_notebook, new_markdown_cell
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
from jupytext import __version__
from jupytext import readf, writef, writes
from jupytext.cli import parse_jupytext_args, jupytext, jupytext_cli, system, str2bool
from jupytext.compare import compare_notebooks
from jupytext.paired_paths import paired_paths
from .utils import list_notebooks
from .utils import list_notebooks, requires_black, requires_flake8


def test_str2bool():
Expand Down Expand Up @@ -350,6 +350,47 @@ def git(*args):
assert os.path.isfile(tmp_md)


@requires_black
@requires_flake8
def test_pre_commit_hook_sync_black_flake8(tmpdir):
def git(*args):
print(system('git', *args, cwd=str(tmpdir)))

git('init')
git('status')
hook = str(tmpdir.join('.git/hooks/pre-commit'))
with open(hook, 'w') as fp:
fp.write('#!/bin/sh\n'
'# Pair ipynb notebooks to a python file, reformat content with black, and run flake8\n'
'# Note: this hook only acts on ipynb files. When pulling, run jupytext --sync manually to '
'update the ipynb file.\n'
'jupytext --pre-commit --from ipynb --set-formats ipynb,py --sync --pipe black --check flake8\n')

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
nb = new_notebook(cells=[new_code_cell(source='1+ 1')])

writef(nb, tmp_ipynb)
git('add', 'notebook.ipynb')
git('status')
git('commit', '-m', 'created')
git('status')
assert os.path.isfile(tmp_py)
assert os.path.isfile(tmp_ipynb)
with open(tmp_py) as fp:
assert fp.read().splitlines()[-1] == '1 + 1'

nb = new_notebook(cells=[new_code_cell(source='"""trailing \nwhitespace"""')])
writef(nb, tmp_ipynb)
git('add', 'notebook.ipynb')
git('status')
with pytest.raises(SystemExit): # not flake8
git('commit', '-m', 'created')


def test_manual_call_of_pre_commit_hook(tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
Expand Down
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@
import sys
import re
import pytest
from jupytext.cli import system

skip_if_dict_is_not_ordered = pytest.mark.skipif(
sys.version_info < (3, 6),
reason="unordered dict result in changes in chunk options")


def tool_version(tool):
try:
return system(tool, '--version')
except OSError: # pragma: no cover
return None


requires_black = pytest.mark.skipif(not tool_version('black'), reason='black not found')
requires_flake8 = pytest.mark.skipif(not tool_version('flake8'), reason='flake8 not found')
requires_autopep8 = pytest.mark.skipif(not tool_version('autopep8'), reason='autopep8 not found')


def list_notebooks(path='ipynb', skip='World'):
"""All notebooks in the directory notebooks/path,
or in the package itself"""
Expand Down

0 comments on commit 97eb6bb

Please sign in to comment.