Skip to content

Commit 53c5293

Browse files
authored
Merge pull request #6474 from cjerdonek/logging-errors-during-tests
Don't allow logging errors during tests even if allow_stderr_error=True
2 parents 3596ad5 + 0b8377d commit 53c5293

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

tests/lib/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,28 @@ def check_stderr(
294294
if allow_stderr_warning is None:
295295
allow_stderr_warning = allow_stderr_error
296296

297-
if allow_stderr_error:
298-
# Then any stderr is acceptable.
299-
if not allow_stderr_warning:
300-
raise RuntimeError(
301-
'cannot pass allow_stderr_warning=False with '
302-
'allow_stderr_error=True'
303-
)
304-
return
297+
if allow_stderr_error and not allow_stderr_warning:
298+
raise RuntimeError(
299+
'cannot pass allow_stderr_warning=False with '
300+
'allow_stderr_error=True'
301+
)
305302

306303
lines = stderr.splitlines()
307304
for line in lines:
308-
# First check for logging errors which are sent directly to stderr
309-
# and so bypass any configured log formatter. The
310-
# "--- Logging error ---" string is used in Python 3.4+, and
305+
# First check for logging errors, which we don't allow during
306+
# tests even if allow_stderr_error=True (since a logging error
307+
# would signal a bug in pip's code).
308+
# Unlike errors logged with logger.error(), these errors are
309+
# sent directly to stderr and so bypass any configured log formatter.
310+
# The "--- Logging error ---" string is used in Python 3.4+, and
311311
# "Logged from file " is used in Python 2.
312312
if (line.startswith('--- Logging error ---') or
313313
line.startswith('Logged from file ')):
314314
reason = 'stderr has a logging error, which is never allowed'
315315
msg = make_check_stderr_message(stderr, line=line, reason=reason)
316316
raise RuntimeError(msg)
317+
if allow_stderr_error:
318+
continue
317319

318320
if line.startswith('ERROR: '):
319321
reason = (

tests/lib/test_lib.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ def test_as_import(script):
8080

8181
class TestPipTestEnvironment:
8282

83-
def run_with_log_command(self, script, sub_string):
83+
def run_stderr_with_prefix(self, script, prefix, **kwargs):
84+
"""
85+
Call run() that prints stderr with the given prefix.
86+
"""
87+
text = '{}: hello, world\\n'.format(prefix)
88+
command = 'import sys; sys.stderr.write("{}")'.format(text)
89+
args = [sys.executable, '-c', command]
90+
script.run(*args, **kwargs)
91+
92+
def run_with_log_command(self, script, sub_string, **kwargs):
8493
"""
8594
Call run() on a command that logs a "%"-style format string using
8695
the given substring as the string's replacement field.
@@ -90,15 +99,6 @@ def run_with_log_command(self, script, sub_string):
9099
"logging.getLogger().info('sub: {}', 'foo')"
91100
).format(sub_string)
92101
args = [sys.executable, '-c', command]
93-
script.run(*args)
94-
95-
def run_stderr_with_prefix(self, script, prefix, **kwargs):
96-
"""
97-
Call run() that prints stderr with the given prefix.
98-
"""
99-
text = '{}: hello, world\\n'.format(prefix)
100-
command = 'import sys; sys.stderr.write("{}")'.format(text)
101-
args = [sys.executable, '-c', command]
102102
script.run(*args, **kwargs)
103103

104104
@pytest.mark.parametrize('prefix', (
@@ -162,8 +162,13 @@ def test_run__logging_error(self, script):
162162

163163
expected_start = 'stderr has a logging error, which is never allowed'
164164
with assert_error_startswith(RuntimeError, expected_start):
165-
# Pass a bad substitution string.
166-
self.run_with_log_command(script, sub_string='{!r}')
165+
# Pass a bad substitution string. Also, pass
166+
# allow_stderr_error=True to check that the RuntimeError occurs
167+
# even under the stricter test condition of when we are allowing
168+
# other types of errors.
169+
self.run_with_log_command(
170+
script, sub_string='{!r}', allow_stderr_error=True,
171+
)
167172

168173
def test_run__allow_stderr_error_false_error_with_expect_error(
169174
self, script,

0 commit comments

Comments
 (0)