Skip to content
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

Try not to assume a module's file extension is .py #2374

Merged
merged 1 commit into from
Apr 21, 2017
Merged

Try not to assume a module's file extension is .py #2374

merged 1 commit into from
Apr 21, 2017

Conversation

Kodiologist
Copy link
Contributor

Fixes #2369.

Any hints on how to write a test for this?

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 92.68% when pulling 3c3c6fd on Kodiologist:getmodpath-file-ext into 581b463 on pytest-dev:master.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Awesome, thanks a ton @Kodiologist! Please take a look at my comments and tell me what do you think.

@@ -235,8 +236,7 @@ def getmodpath(self, stopatmodule=True, includemodule=False):
continue
name = node.name
if isinstance(node, Module):
assert name.endswith(".py")
name = name[:-3]
name = os.path.splitext(name)[0]
Copy link
Member

Choose a reason for hiding this comment

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

About a test for this:

I was considering writing a unittest that basically creates a Module instance and calls getmodpath.

But after thinking some more about it I think we need an integration test that demonstrates it is possible to collect a file without a .py extension as a Module object and the entire machinery works, otherwise we might end up introducing a regression later in some other part of the code and breaking this use case without realizing it.

I suggest to create a test that has a conftest.py file which implements the pytest_collect_file hook like this (untested):

def test_collects_different_extensions_as_python(testdir):
    """Ensure we can collect files with different extensions as .py files (#2369)""".
    testdir.makeconftest("""
        def pytest_collect_file(path, parent):
            ext = path.ext
            if ext == ".hy":
                return Module(path, parent)
    """)
    testdir.writefile("""
        # ... 1 test here in Hy ...
    """, ext=".hy")
    result = testdir.runpytest()
    result.stdout.fnmatch_lines('*1 passed*')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All right, thanks, a test is in.

CHANGELOG.rst Outdated
@@ -12,6 +12,10 @@
* Added documentation related to issue (`#1937`_)
Thanks `@skylarjhdownes`_ for the PR.

* In ``python.PyobjMixin.getmodpath``, don't assume the module's file extension
Copy link
Member

Choose a reason for hiding this comment

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

I would write this to reflect the use case instead of the internal implementation detail that changed, perhaps:

Changed some internal details slightly so it is possible to collect files of any extension as Python modules.

import sys, os, imp
from _pytest.python import Module

class Loader:
Copy link
Member

Choose a reason for hiding this comment

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

Clever solution. 😁

I notice though that you are using sys.meta_path.append, and because we run this test in the same process as the main process running pytest, we are leaving that finder there for the rest of pytest's test run.

The simplest solution I can find now is to just use testdir.runpytest_subprocess() to run that test in a separated process to ensure isolation. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me. Done.

Copy link
Member

Choose a reason for hiding this comment

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

for a different protection scheme you can use pytest_configure and pytest_unconfigure to push/pop the extra finder

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Do you think this should be changed, or leaving that as it is is fine?

I'm OK either way TBH.

Copy link
Member

Choose a reason for hiding this comment

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

both ways work, i just wanted to document here

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough! 👍

Would you like to merge this then?

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 92.68% when pulling 836b3ee on Kodiologist:getmodpath-file-ext into 581b463 on pytest-dev:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 92.68% when pulling dcefb28 on Kodiologist:getmodpath-file-ext into 581b463 on pytest-dev:master.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks a lot @Kodiologist! 👍

@nicoddemus nicoddemus merged commit 80225ce into pytest-dev:master Apr 21, 2017
@Kodiologist Kodiologist deleted the getmodpath-file-ext branch April 21, 2017 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants