Skip to content

Commit b27228e

Browse files
authored
#1218 fix -ve cannot be passed compound (#1224)
1 parent 7253464 commit b27228e

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

docs/changelog/1218.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
using -v and -e connected (as -ve) fails - by :user:`gaborbernat`

src/tox/session/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from tox.util import set_os_env_var
2626
from tox.util.graph import stable_topological_sort
2727
from tox.util.path import ensure_empty_dir
28+
from tox.util.stdlib import suppress_output
2829
from tox.venv import VirtualEnv
2930

3031
from .commands.help import show_help
@@ -48,8 +49,12 @@ def setup_reporter(args):
4849

4950
parser = ArgumentParser(add_help=False)
5051
add_verbosity_commands(parser)
51-
options, _ = parser.parse_known_args(args)
52-
update_default_reporter(options.quiet_level, options.verbose_level)
52+
with suppress_output():
53+
try:
54+
options, _ = parser.parse_known_args(args)
55+
update_default_reporter(options.quiet_level, options.verbose_level)
56+
except SystemExit:
57+
pass
5358

5459

5560
def main(args):

src/tox/util/stdlib.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
import sys
22
import threading
3+
from contextlib import contextmanager
4+
from tempfile import TemporaryFile
35

46

57
def is_main_thread():
8+
"""returns true if we are within the main thread"""
69
cur_thread = threading.current_thread()
710
if sys.version_info >= (3, 4):
811
return cur_thread is threading.main_thread()
912
else:
1013
# noinspection PyUnresolvedReferences
1114
return isinstance(cur_thread, threading._MainThread)
15+
16+
17+
# noinspection PyPep8Naming
18+
@contextmanager
19+
def suppress_output():
20+
"""suppress both stdout and stderr outputs"""
21+
if sys.version_info > (3, 4):
22+
from contextlib import redirect_stdout, redirect_stderr
23+
else:
24+
25+
class _RedirectStream(object):
26+
27+
_stream = None
28+
29+
def __init__(self, new_target):
30+
self._new_target = new_target
31+
self._old_targets = []
32+
33+
def __enter__(self):
34+
self._old_targets.append(getattr(sys, self._stream))
35+
setattr(sys, self._stream, self._new_target)
36+
return self._new_target
37+
38+
def __exit__(self, exctype, excinst, exctb):
39+
setattr(sys, self._stream, self._old_targets.pop())
40+
41+
class redirect_stdout(_RedirectStream):
42+
_stream = "stdout"
43+
44+
class redirect_stderr(_RedirectStream):
45+
_stream = "stderr"
46+
47+
with TemporaryFile("wt") as file:
48+
with redirect_stdout(file):
49+
with redirect_stderr(file):
50+
yield

tests/unit/session/test_session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,15 @@ def test_command_prev_fail_command_skip_post_run(cmd, initproj, mock_venv):
356356
have = result.out.replace(os.linesep, "\n")
357357
actual = have[len(have) - len(expected) :]
358358
assert actual == expected
359+
360+
361+
def test_help_compound_ve_works(cmd, initproj, monkeypatch):
362+
initproj("test-0.1", {"tox.ini": ""})
363+
result = cmd("-ve", "py", "-a")
364+
result.assert_success(is_run_test_env=False)
365+
assert not result.err
366+
assert result.outlines[0].startswith("using")
367+
assert result.outlines[1].startswith("using")
368+
assert result.outlines[2] == "default environments:"
369+
assert result.outlines[3] == "py -> [no description]"
370+
assert len(result.outlines) == 4

0 commit comments

Comments
 (0)