|
| 1 | +from contextlib import redirect_stderr, redirect_stdout |
1 | 2 | from io import StringIO
|
2 | 3 | from json import JSONDecodeError
|
3 | 4 | from pathlib import Path
|
|
23 | 24 | SchemaError,
|
24 | 25 | ValidationError,
|
25 | 26 | )
|
26 |
| -from jsonschema.tests._helpers import captured_output |
27 | 27 | from jsonschema.validators import _LATEST_VERSION, validate
|
28 | 28 |
|
29 | 29 |
|
@@ -856,35 +856,32 @@ def test_find_validator_in_jsonschema(self):
|
856 | 856 | )
|
857 | 857 | self.assertIs(arguments["validator"], Draft4Validator)
|
858 | 858 |
|
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): |
862 | 862 | 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) |
871 | 873 |
|
872 | 874 | 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 | + ) |
883 | 880 | self.assertIn(
|
884 | 881 | "--error-format can only be used with --output plain",
|
885 |
| - stderr.getvalue(), |
| 882 | + stderr, |
886 | 883 | )
|
887 |
| - self.assertFalse(stdout.getvalue()) |
| 884 | + self.assertFalse(stdout) |
888 | 885 |
|
889 | 886 |
|
890 | 887 | class TestCLIIntegration(TestCase):
|
|
0 commit comments