Merge pull request #829 from nicoddemus/pep302-get-data-support
Add support for PEP302 get_data API
This commit is contained in:
commit
194581ab5f
|
@ -1,6 +1,11 @@
|
|||
2.8.0.dev (compared to 2.7.X)
|
||||
-----------------------------
|
||||
|
||||
- fix issue 808: pytest's internal assertion rewrite hook now implements the
|
||||
optional PEP302 get_data API so tests can access data files next to them.
|
||||
Thanks xmo-odoo for request and example and Bruno Oliveira for
|
||||
the PR.
|
||||
|
||||
- rootdir and inifile are now displayed during usage errors to help
|
||||
users diagnose problems such as unexpected ini files which add
|
||||
unknown options being picked up by pytest. Thanks to Pavel Savchenko for
|
||||
|
|
|
@ -197,6 +197,12 @@ class AssertionRewritingHook(object):
|
|||
# DefaultProvider is appropriate.
|
||||
pkg_resources.register_loader_type(cls, pkg_resources.DefaultProvider)
|
||||
|
||||
def get_data(self, pathname):
|
||||
"""Optional PEP302 get_data API.
|
||||
"""
|
||||
with open(pathname, 'rb') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def _write_pyc(state, co, source_stat, pyc):
|
||||
# Technically, we don't have to have the same pyc format as
|
||||
|
|
|
@ -664,6 +664,21 @@ class TestAssertionRewriteHookDetails(object):
|
|||
"* 1 passed*",
|
||||
])
|
||||
|
||||
def test_get_data_support(self, testdir):
|
||||
"""Implement optional PEP302 api (#808).
|
||||
"""
|
||||
path = testdir.mkpydir("foo")
|
||||
path.join("test_foo.py").write(py.code.Source("""
|
||||
class Test:
|
||||
def test_foo(self):
|
||||
import pkgutil
|
||||
data = pkgutil.get_data('foo.test_foo', 'data.txt')
|
||||
assert data == b'Hey'
|
||||
"""))
|
||||
path.join('data.txt').write('Hey')
|
||||
result = testdir.runpytest()
|
||||
result.stdout.fnmatch_lines('*1 passed*')
|
||||
|
||||
|
||||
def test_issue731(testdir):
|
||||
testdir.makepyfile("""
|
||||
|
|
Loading…
Reference in New Issue