From 978c00d7157205a4260b843bde3dcf78b5daee98 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Wed, 25 Aug 2021 09:08:21 +0100 Subject: Kill an old testing helper now that it mostly lives in contextlib. --- jsonschema/tests/_helpers.py | 16 ---------------- jsonschema/tests/test_cli.py | 45 +++++++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/jsonschema/tests/_helpers.py b/jsonschema/tests/_helpers.py index 51fff4f..70f291f 100644 --- a/jsonschema/tests/_helpers.py +++ b/jsonschema/tests/_helpers.py @@ -1,21 +1,5 @@ -from contextlib import contextmanager -from io import StringIO -import sys - - def bug(issue=None): message = "A known bug." if issue is not None: message += " See issue #{issue}.".format(issue=issue) return message - - -@contextmanager -def captured_output(): - new_out, new_err = StringIO(), StringIO() - old_out, old_err = sys.stdout, sys.stderr - try: - sys.stdout, sys.stderr = new_out, new_err - yield sys.stdout, sys.stderr - finally: - sys.stdout, sys.stderr = old_out, old_err diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py index 7ec1aa6..45f942f 100644 --- a/jsonschema/tests/test_cli.py +++ b/jsonschema/tests/test_cli.py @@ -1,3 +1,4 @@ +from contextlib import redirect_stderr, redirect_stdout from io import StringIO from json import JSONDecodeError from pathlib import Path @@ -23,7 +24,6 @@ from jsonschema.exceptions import ( SchemaError, ValidationError, ) -from jsonschema.tests._helpers import captured_output from jsonschema.validators import _LATEST_VERSION, validate @@ -856,35 +856,32 @@ class TestParser(TestCase): ) self.assertIs(arguments["validator"], Draft4Validator) - def test_unknown_output(self): - # Avoid the help message on stdout - with captured_output() as (stdout, stderr): + def cli_output_for(self, *argv): + stdout, stderr = StringIO(), StringIO() + with redirect_stdout(stdout), redirect_stderr(stderr): with self.assertRaises(SystemExit): - cli.parse_args( - [ - "--output", "foo", - "mem://some/schema", - ], - ) - self.assertIn("invalid choice: 'foo'", stderr.getvalue()) - self.assertFalse(stdout.getvalue()) + cli.parse_args(argv) + return stdout.getvalue(), stderr.getvalue() + + def test_unknown_output(self): + stdout, stderr = self.cli_output_for( + "--output", "foo", + "mem://some/schema", + ) + self.assertIn("invalid choice: 'foo'", stderr) + self.assertFalse(stdout) def test_useless_error_format(self): - # Avoid the help message on stdout - with captured_output() as (stdout, stderr): - with self.assertRaises(SystemExit): - cli.parse_args( - [ - "--output", "pretty", - "--error-format", "foo", - "mem://some/schema", - ], - ) + stdout, stderr = self.cli_output_for( + "--output", "pretty", + "--error-format", "foo", + "mem://some/schema", + ) self.assertIn( "--error-format can only be used with --output plain", - stderr.getvalue(), + stderr, ) - self.assertFalse(stdout.getvalue()) + self.assertFalse(stdout) class TestCLIIntegration(TestCase): -- cgit v1.2.1