Skip to content

Commit

Permalink
CLI: reject module_paths that smell like FS paths
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 28, 2018
1 parent a00c4b5 commit af6e8e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions monkeytype/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import difflib
import importlib
import inspect
import os
import os.path
import runpy
import subprocess
Expand Down Expand Up @@ -44,6 +45,11 @@ def module_path(path: str) -> Tuple[str, Optional[str]]:
parts = path.split(':', 1)
module = parts.pop(0)
qualname = parts[0] if parts else None
if os.sep in module: # Smells like a path
raise argparse.ArgumentTypeError(
f'{module} does not look like a valid Python import path'
)

return module, qualname


Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,12 @@ def test_cli_context_manager_activated(capsys, stdout, stderr):
assert out == "IN SETUP: stub\nIN TEARDOWN: stub\n"
assert err == ""
assert ret == 0


def test_pathlike_parameter(store_data, capsys):
store, db_file = store_data
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
with pytest.raises(SystemExit):
cli.main(['stub', 'test/foo.py:bar'], stdout, stderr)
out, err = capsys.readouterr()
assert "test/foo.py does not look like a valid Python import path" in err

0 comments on commit af6e8e6

Please sign in to comment.