Skip to content

Commit 978c00d

Browse files
committed
Kill an old testing helper now that it mostly lives in contextlib.
1 parent 52b1f04 commit 978c00d

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

jsonschema/tests/_helpers.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
from contextlib import contextmanager
2-
from io import StringIO
3-
import sys
4-
5-
61
def bug(issue=None):
72
message = "A known bug."
83
if issue is not None:
94
message += " See issue #{issue}.".format(issue=issue)
105
return message
11-
12-
13-
@contextmanager
14-
def captured_output():
15-
new_out, new_err = StringIO(), StringIO()
16-
old_out, old_err = sys.stdout, sys.stderr
17-
try:
18-
sys.stdout, sys.stderr = new_out, new_err
19-
yield sys.stdout, sys.stderr
20-
finally:
21-
sys.stdout, sys.stderr = old_out, old_err

jsonschema/tests/test_cli.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import redirect_stderr, redirect_stdout
12
from io import StringIO
23
from json import JSONDecodeError
34
from pathlib import Path
@@ -23,7 +24,6 @@
2324
SchemaError,
2425
ValidationError,
2526
)
26-
from jsonschema.tests._helpers import captured_output
2727
from jsonschema.validators import _LATEST_VERSION, validate
2828

2929

@@ -856,35 +856,32 @@ def test_find_validator_in_jsonschema(self):
856856
)
857857
self.assertIs(arguments["validator"], Draft4Validator)
858858

859-
def test_unknown_output(self):
860-
# Avoid the help message on stdout
861-
with captured_output() as (stdout, stderr):
859+
def cli_output_for(self, *argv):
860+
stdout, stderr = StringIO(), StringIO()
861+
with redirect_stdout(stdout), redirect_stderr(stderr):
862862
with self.assertRaises(SystemExit):
863-
cli.parse_args(
864-
[
865-
"--output", "foo",
866-
"mem://some/schema",
867-
],
868-
)
869-
self.assertIn("invalid choice: 'foo'", stderr.getvalue())
870-
self.assertFalse(stdout.getvalue())
863+
cli.parse_args(argv)
864+
return stdout.getvalue(), stderr.getvalue()
865+
866+
def test_unknown_output(self):
867+
stdout, stderr = self.cli_output_for(
868+
"--output", "foo",
869+
"mem://some/schema",
870+
)
871+
self.assertIn("invalid choice: 'foo'", stderr)
872+
self.assertFalse(stdout)
871873

872874
def test_useless_error_format(self):
873-
# Avoid the help message on stdout
874-
with captured_output() as (stdout, stderr):
875-
with self.assertRaises(SystemExit):
876-
cli.parse_args(
877-
[
878-
"--output", "pretty",
879-
"--error-format", "foo",
880-
"mem://some/schema",
881-
],
882-
)
875+
stdout, stderr = self.cli_output_for(
876+
"--output", "pretty",
877+
"--error-format", "foo",
878+
"mem://some/schema",
879+
)
883880
self.assertIn(
884881
"--error-format can only be used with --output plain",
885-
stderr.getvalue(),
882+
stderr,
886883
)
887-
self.assertFalse(stdout.getvalue())
884+
self.assertFalse(stdout)
888885

889886

890887
class TestCLIIntegration(TestCase):

0 commit comments

Comments
 (0)