Skip to content

Commit c4f5a04

Browse files
committed
add globbing env and package version
1 parent 23b7174 commit c4f5a04

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
1.5 (dev)
22
---------
3-
* dropped python 3.3 support
4-
* add python 3.5 suport
3+
* add support for 'globbing' package version and enviromnent variables
4+
* dropped python 2.6 support
5+
* add python 3.5 and 3.6 support
56

67
1.4
78
---

README.md renamed to README.rst

File renamed without changes.

pytest_echo.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# -*- coding: utf-8 -*-
2+
import fnmatch
23
import os
34
from pprint import pformat
45

6+
import pip
57

6-
__version__ = '1.4'
8+
__version__ = '1.5'
79

810

911
class RetrieveException(Exception):
@@ -88,6 +90,28 @@ def get_module_attribute(path):
8890
return str(e)
8991

9092

93+
def get_env(var_name):
94+
if '*' in var_name:
95+
targets = [(key, value)
96+
for key, value in os.environ.items()
97+
if fnmatch.fnmatch(key, var_name)]
98+
else:
99+
targets = [(var_name, os.environ.get(var_name, "<not set>"))]
100+
101+
return targets
102+
103+
104+
def get_version(package_name):
105+
if '*' in package_name:
106+
targets = [(i.key, i.version)
107+
for i in pip.get_installed_distributions()
108+
if fnmatch.fnmatch(i.key, package_name)]
109+
else:
110+
targets = [(package_name, _get_version(package_name))]
111+
112+
return targets
113+
114+
91115
def _get_version(package_name):
92116
try:
93117
import pkg_resources
@@ -113,12 +137,22 @@ def pytest_report_header(config):
113137
ret = []
114138
if config.option.echo_envs:
115139
ret.append("Environment:")
116-
ret.append("\n".join([" %s: %s" % (k, os.environ.get(k, "<not set>"))
117-
for k in config.option.echo_envs]))
140+
data = []
141+
for k in config.option.echo_envs:
142+
data.extend(get_env(k))
143+
ret.append("\n".join([" %s: %s" % (k, v)
144+
for k,v in sorted(data)]))
145+
146+
# ret.append("\n".join([" %s: %s" % (k, os.environ.get(k, "<not set>"))
147+
# for k in config.option.echo_envs]))
118148
if config.option.echo_versions:
119149
ret.append("Package version:")
120-
ret.append("\n".join([" %s: %s" % (k, _get_version(k))
121-
for k in config.option.echo_versions]))
150+
data = []
151+
for k in config.option.echo_versions:
152+
data.extend(get_version(k))
153+
ret.append("\n".join([" %s: %s" % (k, v)
154+
for k,v in sorted(data)]))
155+
122156
if config.option.echo_attribues:
123157
ret.append("Inspections:")
124158
ret.append("\n".join([" %s: %s" % (k, get_module_attribute(k))

test_echo.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
from contextlib import contextmanager
34

45
import pytest
56

@@ -29,17 +30,53 @@ def test_version():
2930
assert pytest_echo.__version__
3031

3132

33+
@contextmanager
34+
def env(**kwargs):
35+
backup = {}
36+
for k, v in kwargs.items():
37+
if k in os.environ:
38+
backup[k] = v
39+
os.environ[k] = v
40+
yield
41+
for k, v in kwargs.items():
42+
if k in backup:
43+
os.environ[k] = backup[k]
44+
else:
45+
del os.environ[k]
46+
47+
3248
def test_echo_env(testdir):
33-
os.environ['PYTESTECHO'] = '123'
34-
result = testdir.runpytest('--echo-env=PYTESTECHO')
35-
result.stdout.fnmatch_lines([
36-
" PYTESTECHO: 123"
37-
])
49+
# os.environ['PYTESTECHO'] = '123'
50+
with env(PYTESTECHO='123'):
51+
result = testdir.runpytest('--echo-env=PYTESTECHO')
52+
result.stdout.fnmatch_lines([
53+
" PYTESTECHO: 123"
54+
])
55+
56+
57+
def test_echo_env_glob(testdir):
58+
# os.environ['PYTESTECHO-a'] = '1'
59+
# os.environ['PYTESTECHO-b'] = '2'
60+
with env(**{'PYTESTECHO-a': '1', 'PYTESTECHO-b': '2'}):
61+
result = testdir.runpytest('--echo-env=PYTESTECHO*')
62+
result.stdout.fnmatch_lines([
63+
" PYTESTECHO-a: 1",
64+
" PYTESTECHO-b: 2"
65+
])
3866

3967

4068
def test_echo_version(testdir):
41-
result = testdir.runpytest('--echo-version=pytest_echo')
42-
result.stdout.fnmatch_lines([" pytest_echo: %s" % pytest_echo.__version__])
69+
result = testdir.runpytest('--echo-version=pytest-echo')
70+
result.stdout.fnmatch_lines([" pytest-echo: %s" % pytest_echo.__version__])
71+
72+
73+
def test_echo_version_glob(testdir):
74+
result = testdir.runpytest('--echo-version=pytest*')
75+
result.stdout.fnmatch_lines([" pytest: %s" % pytest.__version__,
76+
" pytest-echo: %s" % pytest_echo.__version__,
77+
])
78+
# result.stdout.fnmatch_lines([" pytest-echo: %s" % pytest_echo.__version__])
79+
# result.stdout.fnmatch_lines([" pytest: %s" % pytest.__version__])
4380

4481

4582
def test_echo_all(testdir):
@@ -102,7 +139,6 @@ def test_echo_attr_module_object_attr(testdir):
102139

103140

104141
def test_django_settings(testdir):
105-
106142
pytest.importorskip("django")
107143
testdir.makeconftest("""
108144
def pytest_configure(config):
@@ -115,6 +151,7 @@ def pytest_configure(config):
115151
" django.conf.settings.DEBUG: False",
116152
])
117153

154+
118155
def test_django_settings_extended(testdir):
119156
pytest.importorskip("django")
120157
testdir.makeconftest("""
@@ -128,4 +165,3 @@ def pytest_configure(config):
128165
result.stdout.fnmatch_lines([
129166
" django.conf.settings.DATABASES.default.ENGINE: 'sqlite3'"
130167
])
131-

tox.ini

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[tox]
2-
envlist=py26,py27,py33,py34,py35,pypy
2+
envlist=py27,py33,py34,py35,py36,pypy
33

44
[testenv]
5-
deps = pytest
6-
pytest-cache
5+
deps =
6+
; py27: pytest>=3.3.2
7+
py{33,34}: pytest<3
8+
py{33,34}: py<1.5
79

810
commands =
911
pip install {toxinidir}

0 commit comments

Comments
 (0)