Skip to content

Commit 6b16b09

Browse files
authored
cmd simplify, use pytest stderr/stdout management, and use regex instead of fnmatch (#711)
* Python 2.6 support dropped, this line can never be reached * main args is required, set at entry level * fix pypy failure * remove dead code
1 parent 087ed06 commit 6b16b09

15 files changed

+389
-508
lines changed

tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from tox._pytestplugin import * # noqa
1+
from __future__ import unicode_literals
2+
3+
pytest_plugins = ['tox._pytestplugin']

tests/test_config.py

Lines changed: 54 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
from textwrap import dedent
45

@@ -2085,18 +2086,16 @@ def test_parse_recreate(self, newconfig):
20852086

20862087
class TestCmdInvocation:
20872088
def test_help(self, cmd):
2088-
result = cmd.run("tox", "-h")
2089+
result = cmd("-h")
20892090
assert not result.ret
2090-
result.stdout.fnmatch_lines([
2091-
"*help*"
2092-
])
2091+
assert not result.err
2092+
assert re.match(r'usage:.*help.*', result.out, re.DOTALL)
20932093

20942094
def test_version_simple(self, cmd):
2095-
result = cmd.run("tox", "--version")
2095+
result = cmd("--version")
20962096
assert not result.ret
2097-
stdout = result.stdout.str()
2098-
assert tox.__version__ in stdout
2099-
assert "imported from" in stdout
2097+
from tox import __version__
2098+
assert "{} imported from".format(__version__) in result.out
21002099

21012100
def test_version_no_plugins(self):
21022101
pm = PluginManager('fakeprject')
@@ -2163,14 +2162,8 @@ def test_listenvs(self, cmd, initproj):
21632162
changedir = docs
21642163
'''
21652164
})
2166-
result = cmd.run("tox", "-l")
2167-
result.stdout.fnmatch_lines("""
2168-
py36
2169-
py27
2170-
py34
2171-
pypy
2172-
docs
2173-
""")
2165+
result = cmd("-l")
2166+
assert result.outlines == ['py36', 'py27', 'py34', 'pypy', 'docs']
21742167

21752168
def test_listenvs_verbose_description(self, cmd, initproj):
21762169
initproj('listenvs_verbose_description', filedefs={
@@ -2193,15 +2186,14 @@ def test_listenvs_verbose_description(self, cmd, initproj):
21932186
description = let me overwrite that
21942187
'''
21952188
})
2196-
result = cmd.run("tox", "-lv")
2197-
result.stdout.fnmatch_lines("""
2198-
default environments:
2199-
py36 -> run pytest on Python 3.6
2200-
py27 -> run pytest on Python 2.7
2201-
py34 -> run pytest on Python 3.4
2202-
pypy -> publish to pypy
2203-
docs -> let me overwrite that
2204-
""")
2189+
result = cmd("-lv")
2190+
assert result.outlines[2:] == [
2191+
'default environments:',
2192+
'py36 -> run pytest on Python 3.6',
2193+
'py27 -> run pytest on Python 2.7',
2194+
'py34 -> run pytest on Python 3.4',
2195+
'pypy -> publish to pypy',
2196+
'docs -> let me overwrite that']
22052197

22062198
def test_listenvs_all(self, cmd, initproj):
22072199
initproj('listenvs_all', filedefs={
@@ -2216,15 +2208,8 @@ def test_listenvs_all(self, cmd, initproj):
22162208
changedir = docs
22172209
'''
22182210
})
2219-
result = cmd.run("tox", "-a")
2220-
result.stdout.fnmatch_lines("""
2221-
py36
2222-
py27
2223-
py34
2224-
pypy
2225-
docs
2226-
notincluded
2227-
""")
2211+
result = cmd("-a")
2212+
assert result.outlines == ['py36', 'py27', 'py34', 'pypy', 'docs', 'notincluded']
22282213

22292214
def test_listenvs_all_verbose_description(self, cmd, initproj):
22302215
initproj('listenvs_all_verbose_description', filedefs={
@@ -2241,19 +2226,19 @@ def test_listenvs_all_verbose_description(self, cmd, initproj):
22412226
22422227
[testenv:docs]
22432228
changedir = docs
2244-
'''
2229+
''',
22452230
})
2246-
result = cmd.run("tox", "-av")
2247-
result.stdout.fnmatch_lines("""
2248-
default environments:
2249-
py27-windows -> run pytest on Python 2.7 on Windows platform
2250-
py27-linux -> run pytest on Python 2.7 on Linux platform
2251-
py36-windows -> run pytest on Python 3.6 on Windows platform
2252-
py36-linux -> run pytest on Python 3.6 on Linux platform
2253-
2254-
additional environments:
2255-
docs -> generate documentation
2256-
""")
2231+
result = cmd("-av")
2232+
expected = [
2233+
"default environments:",
2234+
"py27-windows -> run pytest on Python 2.7 on Windows platform",
2235+
"py27-linux -> run pytest on Python 2.7 on Linux platform",
2236+
"py36-windows -> run pytest on Python 3.6 on Windows platform",
2237+
"py36-linux -> run pytest on Python 3.6 on Linux platform",
2238+
"",
2239+
"additional environments:",
2240+
"docs -> generate documentation"]
2241+
assert result.outlines[-len(expected):] == expected
22572242

22582243
def test_listenvs_all_verbose_description_no_additional_environments(self, cmd, initproj):
22592244
initproj('listenvs_all_verbose_description', filedefs={
@@ -2262,29 +2247,25 @@ def test_listenvs_all_verbose_description_no_additional_environments(self, cmd,
22622247
envlist=py27,py36
22632248
'''
22642249
})
2265-
result = cmd.run("tox", "-av")
2266-
result.stdout.fnmatch_lines("""
2267-
default environments:
2268-
py27 -> [no description]
2269-
py36 -> [no description]
2270-
""")
2271-
assert 'additional environments' not in result.stdout.str()
2250+
result = cmd("-av")
2251+
expected = ["default environments:",
2252+
"py27 -> [no description]",
2253+
"py36 -> [no description]"]
2254+
assert result.out.splitlines()[-3:] == expected
2255+
assert 'additional environments' not in result.out
22722256

22732257
def test_config_specific_ini(self, tmpdir, cmd):
22742258
ini = tmpdir.ensure("hello.ini")
2275-
result = cmd.run("tox", "-c", ini, "--showconfig")
2259+
result = cmd("-c", ini, "--showconfig")
22762260
assert not result.ret
2277-
result.stdout.fnmatch_lines([
2278-
"*config-file*hello.ini*"
2279-
])
2261+
assert result.outlines[1] == 'config-file: {}'.format(ini)
22802262

22812263
def test_no_tox_ini(self, cmd, initproj):
2282-
initproj("noini-0.5")
2283-
result = cmd.run("tox")
2264+
initproj("noini-0.5", )
2265+
result = cmd()
22842266
assert result.ret
2285-
result.stderr.fnmatch_lines([
2286-
"*ERROR*tox.ini*not*found*"
2287-
])
2267+
assert result.out == ''
2268+
assert result.err == "ERROR: toxini file 'tox.ini' not found\n"
22882269

22892270
def test_override_workdir(self, tmpdir, cmd, initproj):
22902271
baddir = "badworkdir-123"
@@ -2293,13 +2274,12 @@ def test_override_workdir(self, tmpdir, cmd, initproj):
22932274
'tox.ini': '''
22942275
[tox]
22952276
toxworkdir=%s
2296-
''' % baddir
2277+
''' % baddir,
22972278
})
2298-
result = cmd.run("tox", "--workdir", gooddir, "--showconfig")
2279+
result = cmd("--workdir", gooddir, "--showconfig")
22992280
assert not result.ret
2300-
stdout = result.stdout.str()
2301-
assert gooddir in stdout
2302-
assert baddir not in stdout
2281+
assert gooddir in result.out
2282+
assert baddir not in result.out
23032283
assert py.path.local(gooddir).check()
23042284
assert not py.path.local(baddir).check()
23052285

@@ -2312,20 +2292,16 @@ def test_showconfig_with_force_dep_version(self, cmd, initproj):
23122292
deps=
23132293
dep1==2.3
23142294
dep2
2315-
'''
2295+
''',
23162296
})
2317-
result = cmd.run("tox", "--showconfig")
2297+
result = cmd("--showconfig")
23182298
assert result.ret == 0
2319-
result.stdout.fnmatch_lines([
2320-
r'*deps*dep1==2.3, dep2*'
2321-
])
2299+
assert any(re.match(r'.*deps.*dep1==2.3, dep2.*', l) for l in result.outlines)
23222300
# override dep1 specific version, and force version for dep2
2323-
result = cmd.run("tox", "--showconfig", "--force-dep=dep1",
2324-
"--force-dep=dep2==5.0")
2301+
result = cmd("--showconfig", "--force-dep=dep1",
2302+
"--force-dep=dep2==5.0")
23252303
assert result.ret == 0
2326-
result.stdout.fnmatch_lines([
2327-
r'*deps*dep1, dep2==5.0*'
2328-
])
2304+
assert any(re.match(r'.*deps.*dep1, dep2==5.0.*', l) for l in result.outlines)
23292305

23302306
@pytest.mark.xfail(
23312307
"'pypy' not in sys.executable",
@@ -2340,7 +2316,7 @@ def test_colon_symbol_in_directory_name(self, cmd, initproj):
23402316
commands = pip --version
23412317
'''
23422318
})
2343-
result = cmd.run("tox")
2319+
result = cmd()
23442320
assert result.ret == 0
23452321

23462322

tests/test_interpreters.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class proc:
4747
@staticmethod
4848
def communicate():
4949
return sys.executable.encode(), None
50+
5051
return proc
5152

5253
# Monkeypatch modules to return our faked value
@@ -59,6 +60,7 @@ def test_tox_get_python_executable():
5960
class envconfig:
6061
basepython = sys.executable
6162
envname = "pyxx"
63+
6264
p = tox_get_python_executable(envconfig)
6365
assert p == py.path.local(sys.executable)
6466
for ver in "2.7 3.4 3.5 3.6".split():
@@ -86,6 +88,7 @@ def test_find_executable_extra(monkeypatch):
8688
@staticmethod
8789
def sysfind(x):
8890
return "hello"
91+
8992
monkeypatch.setattr(py.path.local, "sysfind", sysfind)
9093

9194
class envconfig:
@@ -122,6 +125,7 @@ def test_get_executable_no_exist(self, interpreters):
122125
class envconfig:
123126
basepython = "1lkj23"
124127
envname = "pyxx"
128+
125129
assert not interpreters.get_executable(envconfig)
126130
info = interpreters.get_info(envconfig)
127131
assert not info.version_info
@@ -133,6 +137,7 @@ def test_get_sitepackagesdir_error(self, interpreters):
133137
class envconfig:
134138
basepython = sys.executable
135139
envname = "123"
140+
136141
info = interpreters.get_info(envconfig)
137142
s = interpreters.get_sitepackagesdir(info, "")
138143
assert s

tests/test_pytest_plugins.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
class TestInitProj:
1414
@pytest.mark.parametrize('kwargs', (
15-
{},
16-
{'src_root': None},
17-
{'src_root': ''},
18-
{'src_root': '.'}))
15+
{},
16+
{'src_root': None},
17+
{'src_root': ''},
18+
{'src_root': '.'}))
1919
def test_no_src_root(self, kwargs, tmpdir, initproj):
2020
initproj('black_knight-42', **kwargs)
2121
init_file = tmpdir.join('black_knight', 'black_knight', '__init__.py')
@@ -64,14 +64,14 @@ def test_broken_py_path_local_join_workaround_on_Windows(
6464

6565
class TestPathParts:
6666
@pytest.mark.parametrize('input, expected', (
67-
('', []),
68-
('/', ['/']),
69-
('//', ['//']),
70-
('/a', ['/', 'a']),
71-
('/a/', ['/', 'a']),
72-
('/a/b', ['/', 'a', 'b']),
73-
('a', ['a']),
74-
('a/b', ['a', 'b'])))
67+
('', []),
68+
('/', ['/']),
69+
('//', ['//']),
70+
('/a', ['/', 'a']),
71+
('/a/', ['/', 'a']),
72+
('/a/b', ['/', 'a', 'b']),
73+
('a', ['a']),
74+
('a/b', ['a', 'b'])))
7575
def test_path_parts(self, input, expected):
7676
assert _path_parts(input) == expected
7777

@@ -82,18 +82,18 @@ def test_on_py_path(self):
8282

8383

8484
@pytest.mark.parametrize('base, filedefs, target, expected', (
85-
('/base', {}, '', False),
86-
('/base', {}, '/base', False),
87-
('/base', {'a': {'b': 'data'}}, '', True),
88-
('/base', {'a': {'b': 'data'}}, 'a', True),
89-
('/base', {'a': {'b': 'data'}}, 'a/b', True),
90-
('/base', {'a': {'b': 'data'}}, 'a/x', False),
91-
('/base', {'a': {'b': 'data'}}, 'a/b/c', False),
92-
('/base', {'a': {'b': 'data'}}, '/base', True),
93-
('/base', {'a': {'b': 'data'}}, '/base/a', True),
94-
('/base', {'a': {'b': 'data'}}, '/base/a/b', True),
95-
('/base', {'a': {'b': 'data'}}, '/base/a/x', False),
96-
('/base', {'a': {'b': 'data'}}, '/base/a/b/c', False),
97-
('/base', {'a': {'b': 'data'}}, '/a', False)))
85+
('/base', {}, '', False),
86+
('/base', {}, '/base', False),
87+
('/base', {'a': {'b': 'data'}}, '', True),
88+
('/base', {'a': {'b': 'data'}}, 'a', True),
89+
('/base', {'a': {'b': 'data'}}, 'a/b', True),
90+
('/base', {'a': {'b': 'data'}}, 'a/x', False),
91+
('/base', {'a': {'b': 'data'}}, 'a/b/c', False),
92+
('/base', {'a': {'b': 'data'}}, '/base', True),
93+
('/base', {'a': {'b': 'data'}}, '/base/a', True),
94+
('/base', {'a': {'b': 'data'}}, '/base/a/b', True),
95+
('/base', {'a': {'b': 'data'}}, '/base/a/x', False),
96+
('/base', {'a': {'b': 'data'}}, '/base/a/b/c', False),
97+
('/base', {'a': {'b': 'data'}}, '/a', False)))
9898
def test_filedefs_contains(base, filedefs, target, expected):
9999
assert bool(_filedefs_contains(base, filedefs, target)) == expected

tests/test_venv.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tox.venv import tox_testenv_install_deps
1515
from tox.venv import VirtualEnv
1616

17+
1718
# def test_global_virtualenv(capfd):
1819
# v = VirtualEnv()
1920
# assert v.list()
@@ -544,7 +545,7 @@ def test_envbindir_path(self, newmocksession, monkeypatch):
544545
monkeypatch.setenv("PATH", "xyz")
545546
sysfind_calls = []
546547
monkeypatch.setattr("py.path.local.sysfind", classmethod(
547-
lambda *args, **kwargs: sysfind_calls.append(kwargs) or 0 / 0))
548+
lambda *args, **kwargs: sysfind_calls.append(kwargs) or 0 / 0))
548549

549550
with pytest.raises(ZeroDivisionError):
550551
venv._install(list('123'), action=action)
@@ -633,7 +634,7 @@ def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatc
633634
assert pcalls[0].env["YY"] == "456"
634635
assert "YY" not in pcalls[1].env
635636

636-
assert {"ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"}\
637+
assert {"ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"} \
637638
.issubset(pcalls[1].env)
638639

639640
# setenv does not trigger PYTHONPATH warnings

0 commit comments

Comments
 (0)