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

This commit is contained in:
Kodi Arfer 2017-04-19 12:26:56 -07:00
parent 581b463b60
commit dcefb287fc
4 changed files with 36 additions and 2 deletions

View File

@ -85,6 +85,7 @@ Justyna Janczyszyn
Kale Kundert Kale Kundert
Katarzyna Jachim Katarzyna Jachim
Kevin Cox Kevin Cox
Kodi B. Arfer
Lee Kamentsky Lee Kamentsky
Lev Maximov Lev Maximov
Loic Esteve Loic Esteve

View File

@ -12,6 +12,9 @@
* Added documentation related to issue (`#1937`_) * Added documentation related to issue (`#1937`_)
Thanks `@skylarjhdownes`_ for the PR. Thanks `@skylarjhdownes`_ for the PR.
* Allow collecting files with any file extension as Python modules (`#2369`_).
Thanks `@Kodiologist`_ for the PR.
* *
* *
@ -23,11 +26,13 @@
.. _@skylarjhdownes: https://github.com/skylarjhdownes .. _@skylarjhdownes: https://github.com/skylarjhdownes
.. _@fabioz: https://github.com/fabioz .. _@fabioz: https://github.com/fabioz
.. _@metasyn: https://github.com/metasyn .. _@metasyn: https://github.com/metasyn
.. _@Kodiologist: https://github.com/Kodiologist
.. _#1937: https://github.com/pytest-dev/pytest/issues/1937 .. _#1937: https://github.com/pytest-dev/pytest/issues/1937
.. _#2276: https://github.com/pytest-dev/pytest/issues/2276 .. _#2276: https://github.com/pytest-dev/pytest/issues/2276
.. _#2336: https://github.com/pytest-dev/pytest/issues/2336 .. _#2336: https://github.com/pytest-dev/pytest/issues/2336
.. _#2369: https://github.com/pytest-dev/pytest/issues/2369
3.0.7 (2017-03-14) 3.0.7 (2017-03-14)

View File

@ -3,6 +3,7 @@
import fnmatch import fnmatch
import inspect import inspect
import sys import sys
import os
import collections import collections
import math import math
from itertools import count from itertools import count
@ -235,8 +236,7 @@ class PyobjMixin(PyobjContext):
continue continue
name = node.name name = node.name
if isinstance(node, Module): if isinstance(node, Module):
assert name.endswith(".py") name = os.path.splitext(name)[0]
name = name[:-3]
if stopatmodule: if stopatmodule:
if includemodule: if includemodule:
parts.append(name) parts.append(name)

View File

@ -841,6 +841,34 @@ class TestConftestCustomization:
l = modcol.collect() l = modcol.collect()
assert '_hello' not in l assert '_hello' not in l
def test_issue2369_collect_module_fileext(self, testdir):
"""Ensure we can collect files with weird file extensions as Python
modules (#2369)"""
# We'll implement a little finder and loader to import files containing
# Python source code whose file extension is ".narf".
testdir.makeconftest("""
import sys, os, imp
from _pytest.python import Module
class Loader:
def load_module(self, name):
return imp.load_source(name, name + ".narf")
class Finder:
def find_module(self, name, path=None):
if os.path.exists(name + ".narf"):
return Loader()
sys.meta_path.append(Finder())
def pytest_collect_file(path, parent):
if path.ext == ".narf":
return Module(path, parent)""")
testdir.makefile(".narf", """
def test_something():
assert 1 + 1 == 2""")
# Use runpytest_subprocess, since we're futzing with sys.meta_path.
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines('*1 passed*')
def test_setup_only_available_in_subdir(testdir): def test_setup_only_available_in_subdir(testdir):
sub1 = testdir.mkpydir("sub1") sub1 = testdir.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2") sub2 = testdir.mkpydir("sub2")