Skip to content

Commit

Permalink
fixup shell (#786)
Browse files Browse the repository at this point in the history
* fixup shell

* dont need to cover repr

Co-authored-by: MarcoGorelli <>
  • Loading branch information
MarcoGorelli committed Jan 9, 2023
1 parent c0d6f01 commit 1409871
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion nbqa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import itertools
import os
import re
import shlex
import string
import subprocess
import sys
Expand Down Expand Up @@ -301,7 +302,8 @@ def _run_command(

if shell:
# We already checked that which does not return None
cmd = [cast(str, which(main_command)), *sub_commands]

cmd = [cast(str, which(main_command)), *shlex.split(" ".join(sub_commands))]
else:
python_module = COMMAND_TO_PYTHON_MODULE.get(main_command, main_command)
cmd = [sys.executable, "-m", python_module, *sub_commands]
Expand Down
4 changes: 4 additions & 0 deletions nbqa/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def __init__(self, args: argparse.Namespace, cmd_args: Sequence[str]) -> None:
self.md = args.nbqa_md or None
self.shell = args.nbqa_shell or None

def __repr__(self) -> str: # pragma: nocover
"""Print prettily."""
return str(self.__dict__)

@staticmethod
def parse_args(argv: Optional[Sequence[str]]) -> "CLIArgs":
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_nbqa_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ def test_nbqa_shell_not_found(monkeypatch: MonkeyPatch) -> None:
msg = "\x1b\\[1mnbqa was unable to find some-fictional-command.\x1b\\[0m"
with pytest.raises(CommandNotFoundError, match=msg):
main(args)


@pytest.mark.skipif(sys.platform != "linux", reason="needs grep")
def test_grep(capsys: CaptureFixture) -> None:
"""Check grep with string works."""
main(["grep 'import pandas'", ".", "--nbqa-shell"])
out, _ = capsys.readouterr()
assert out == "tests/data/notebook_for_autoflake.ipynb:import pandas as pd\n"

0 comments on commit 1409871

Please sign in to comment.