Skip to content

Commit de242d0

Browse files
authored
Merge pull request #6351 from cjerdonek/tighten-test-stderr-checks
Tighten up the test suite's stderr checks
2 parents e5353f2 + 66ae681 commit de242d0

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def script(tmpdir, virtualenv, deprecated_python):
303303
assert_no_temp=True,
304304

305305
# Deprecated python versions produce an extra deprecation warning
306-
pip_expect_stderr=deprecated_python,
306+
pip_expect_warning=deprecated_python,
307307
)
308308

309309

tests/functional/test_install.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,7 @@ def test_install_conflict_results_in_warning(script, data):
14741474

14751475
# Then install an incorrect version of the dependency
14761476
result2 = script.pip(
1477-
'install', '--no-index', pkgB_path,
1478-
expect_stderr=True,
1477+
'install', '--no-index', pkgB_path, allow_stderr_error=True,
14791478
)
14801479
assert "pkga 1.0 has requirement pkgb==1.0" in result2.stderr, str(result2)
14811480
assert "Successfully installed pkgB-2.0" in result2.stdout, str(result2)

tests/lib/__init__.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def __init__(self, base_path, *args, **kwargs):
378378

379379
# Whether all pip invocations should expect stderr
380380
# (useful for Python version deprecation)
381-
self.pip_expect_stderr = kwargs.pop('pip_expect_stderr', None)
381+
self.pip_expect_warning = kwargs.pop('pip_expect_warning', None)
382382

383383
# Call the TestFileEnvironment __init__
384384
super(PipTestEnvironment, self).__init__(base_path, *args, **kwargs)
@@ -423,9 +423,9 @@ def run(self, *args, **kw):
423423
:param allow_stderr_error: whether a logged error is allowed in
424424
stderr. Passing True for this argument implies
425425
`allow_stderr_warning` since warnings are weaker than errors.
426-
:param expect_stderr: allow any stderr (equivalent to passing
427-
`allow_stderr_error`). This argument is an abbreviated version
428-
of `allow_stderr_error` and is also kept for backwards
426+
:param expect_stderr: whether to allow warnings in stderr (equivalent
427+
to `allow_stderr_warning`). This argument is an abbreviated
428+
version of `allow_stderr_warning` and is also kept for backwards
429429
compatibility.
430430
"""
431431
if self.verbose:
@@ -443,9 +443,22 @@ def run(self, *args, **kw):
443443
allow_stderr_error = kw.pop('allow_stderr_error', None)
444444
allow_stderr_warning = kw.pop('allow_stderr_warning', None)
445445

446-
if kw.get('expect_error') or kw.get('expect_stderr'):
446+
if kw.get('expect_error'):
447447
# Then default to allowing logged errors.
448+
if allow_stderr_error is not None and not allow_stderr_error:
449+
raise RuntimeError(
450+
'cannot pass allow_stderr_error=False with '
451+
'expect_error=True'
452+
)
448453
allow_stderr_error = True
454+
elif kw.get('expect_stderr'):
455+
# Then default to allowing logged warnings.
456+
if allow_stderr_warning is not None and not allow_stderr_warning:
457+
raise RuntimeError(
458+
'cannot pass allow_stderr_warning=False with '
459+
'expect_stderr=True'
460+
)
461+
allow_stderr_warning = True
449462

450463
# Pass expect_stderr=True to allow any stderr. We do this because
451464
# we do our checking of stderr further on in check_stderr().
@@ -460,14 +473,8 @@ def run(self, *args, **kw):
460473
return TestPipResult(result, verbose=self.verbose)
461474

462475
def pip(self, *args, **kwargs):
463-
if self.pip_expect_stderr:
464-
kwargs['expect_stderr'] = True
465-
# On old versions of Python, urllib3/requests will raise a warning
466-
# about the lack of an SSLContext. Expect it when running commands
467-
# that will touch the outside world.
468-
if (pyversion_tuple < (2, 7, 9) and
469-
args and args[0] in ('search', 'install', 'download')):
470-
kwargs['expect_stderr'] = True
476+
if self.pip_expect_warning:
477+
kwargs['allow_stderr_warning'] = True
471478
if kwargs.pop('use_module', True):
472479
exe = 'python'
473480
args = ('-m', 'pip') + args

tests/lib/test_lib.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,33 @@ def test_run__unexpected_stderr(self, script, prefix, expected_start):
139139
with assert_error_startswith(RuntimeError, expected_start):
140140
self.run_stderr_with_prefix(script, prefix)
141141

142+
def test_run__allow_stderr_error_false_error_with_expect_error(
143+
self, script,
144+
):
145+
"""
146+
Test passing allow_stderr_error=False with expect_error=True.
147+
"""
148+
expected_start = (
149+
'cannot pass allow_stderr_error=False with expect_error=True'
150+
)
151+
with assert_error_startswith(RuntimeError, expected_start):
152+
script.run('python', allow_stderr_error=False, expect_error=True)
153+
154+
def test_run__allow_stderr_warning_false_error_with_expect_stderr(
155+
self, script,
156+
):
157+
"""
158+
Test passing allow_stderr_warning=False with expect_stderr=True.
159+
"""
160+
expected_start = (
161+
'cannot pass allow_stderr_warning=False with expect_stderr=True'
162+
)
163+
with assert_error_startswith(RuntimeError, expected_start):
164+
script.run(
165+
'python', allow_stderr_warning=False, expect_stderr=True,
166+
)
167+
142168
@pytest.mark.parametrize('arg_name', (
143-
'expect_stderr',
144169
'expect_error',
145170
'allow_stderr_error',
146171
))

0 commit comments

Comments
 (0)