Skip to content

Don't crash with --pyargs and a filename that looks like a module #5503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,10 @@ def _tryconvertpyarg(self, x):
"""Convert a dotted module name to path."""
try:
spec = importlib.util.find_spec(x)
except (ValueError, ImportError):
# AttributeError: looks like package module, but actually filename
# ImportError: module does not exist
# ValueError: not a module name
except (AttributeError, ImportError, ValueError):
return x
if spec is None or spec.origin in {None, "namespace"}:
return x
Expand Down
6 changes: 6 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,12 @@ def test_pyargs_only_imported_once(self, testdir):
# should only configure once
assert result.outlines.count("configuring") == 1

def test_pyargs_filename_looks_like_module(self, testdir):
testdir.tmpdir.join("conftest.py").ensure()
testdir.tmpdir.join("t.py").write("def test(): pass")
result = testdir.runpytest("--pyargs", "t.py")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm what happens here? Does it run the file? Is that what's expected?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it runs the file, yes -- it's at least consistent with the 4.6.x behaviour -- I didn't really consider whether this is sane / correct or not 😆

I've never really worked with --pyargs so I'm not sure what the expected thing is here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if it works like 4.6.x then that's fine by me!

Thanks for the quick fix!

assert result.ret == ExitCode.OK

def test_cmdline_python_package(self, testdir, monkeypatch):
import warnings

Expand Down