Skip to content

Commit

Permalink
cat=x is not a magic command
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 12, 2019
1 parent 54bb632 commit 147acae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
# Commands starting with a question or exclamation mark have to be escaped
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]")

_PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})(\s|$)".format('|'.join(
# A bash command not followed by an equal sign or a parenthesis is a magic command
_PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})($|\s$|\s[^=,])".format('|'.join(
# posix
['cat', 'cp', 'mv', 'rm', 'rmdir', 'mkdir'] +
# windows
Expand Down
6 changes: 4 additions & 2 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ def test_force_comment_using_contents_manager(tmpdir):
assert '%pylab inline' in stream.read().splitlines()


@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp'])
@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp',
'cat', 'cat ', 'cat hello.txt', 'cat --option=value hello.txt'])
def test_comment_bash_commands_in_python(magic_cmd):
assert comment_magic([magic_cmd]) == ['# ' + magic_cmd]
assert uncomment_magic(['# ' + magic_cmd]) == [magic_cmd]


@pytest.mark.parametrize('not_magic_cmd', ['copy(a)', 'copy.deepcopy'])
@pytest.mark.parametrize('not_magic_cmd',
['copy(a)', 'copy.deepcopy', 'cat = 3', 'cat=5', 'cat, other = 5,3', 'cat(5)'])
def test_do_not_comment_python_cmds(not_magic_cmd):
assert comment_magic([not_magic_cmd]) == [not_magic_cmd]
assert uncomment_magic([not_magic_cmd]) == [not_magic_cmd]
Expand Down

0 comments on commit 147acae

Please sign in to comment.