Skip to content

Commit

Permalink
add nbqa.ini config file (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jul 18, 2020
1 parent 24b3404 commit 085e2f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nbqa/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import configparser
import os
import re
import subprocess
Expand Down Expand Up @@ -253,6 +254,11 @@ def main(raw_args=None):
replace_magics.main(temp_python_file)
_create_blank_init_files(notebook, tmpdirname)

config = configparser.ConfigParser()
config.read(".nbqa.ini")
if command in config.sections():
configs = [[f"--{key}", val] for key, val in config[command].items()]
kwargs.extend([j for i in configs for j in i])
out, err, output_code = _run_command(
command, root_dir, tmpdirname, nb_to_py_mapping, kwargs
)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_nbqa_ini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import difflib
from pathlib import Path

import pytest

from nbqa.__main__ import main


def test_nbqa_ini_works(tmp_notebook_for_testing, capsys):
"""
Check .nbqa.ini config is picked up works.
"""

with open(".nbqa.ini", "w") as f:
f.write("[flake8]\nignore=F401\nselect=E303\n")

# check diff
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["flake8", ".", "--ignore", "E302"])

Path(".nbqa.ini").unlink()

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
result = "".join(difflib.unified_diff(before, after))
expected = ""
assert result == expected

# check out and err
out, err = capsys.readouterr()
expected_out = "tests/data/notebook_starting_with_md.ipynb:cell_3:0:1: E303 too many blank lines (3)\n"
expected_err = ""
assert sorted(out.splitlines()) == sorted(expected_out.splitlines())
assert sorted(err.splitlines()) == sorted(expected_err.splitlines())

0 comments on commit 085e2f1

Please sign in to comment.