Skip to content

Commit 8ce3302

Browse files
Talha MalikTalha Malik
Talha Malik
authored and
Talha Malik
committed
Refactor to not leak global state
1 parent 5690c71 commit 8ce3302

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

jsonschema/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def _json_file(path):
2222
return json.load(file)
2323

2424

25-
def _read_from_stdin():
26-
return json.loads(sys.stdin.read())
25+
def _read_from_stdin(stdin):
26+
return json.loads(stdin.read())
2727

2828

2929
parser = argparse.ArgumentParser(
@@ -80,14 +80,14 @@ def main(args=sys.argv[1:]):
8080
sys.exit(run(arguments=parse_args(args=args)))
8181

8282

83-
def run(arguments, stdout=sys.stdout, stderr=sys.stderr):
83+
def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin):
8484
error_format = arguments["error_format"]
8585
validator = arguments["validator"](schema=arguments["schema"])
8686

8787
validator.check_schema(arguments["schema"])
8888

8989
errored = False
90-
for instance in arguments["instances"] or (_read_from_stdin(),):
90+
for instance in arguments["instances"] or (_read_from_stdin(stdin),):
9191
for error in validator.iter_errors(instance):
9292
stderr.write(error_format.format(error=error))
9393
errored = True

jsonschema/tests/test_cli.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import subprocess
44
import sys
55

6-
try:
7-
from StringIO import StringIO
8-
except ImportError:
9-
from io import StringIO
10-
116
from jsonschema import Draft4Validator, ValidationError, cli, __version__
127
from jsonschema.compat import NativeIO
138
from jsonschema.exceptions import SchemaError
@@ -156,8 +151,7 @@ def test_version(self):
156151
self.assertEqual(version, __version__)
157152

158153
def test_piping(self):
159-
sys.stdin = StringIO("{}")
160-
stdout, stderr = NativeIO(), NativeIO()
154+
stdout, stderr, stdin = NativeIO(), NativeIO(), NativeIO("{}")
161155
exit_code = cli.run(
162156
{
163157
"validator": fake_validator(),
@@ -167,6 +161,7 @@ def test_piping(self):
167161
},
168162
stdout=stdout,
169163
stderr=stderr,
164+
stdin=stdin,
170165
)
171166
self.assertFalse(stdout.getvalue())
172167
self.assertFalse(stderr.getvalue())

0 commit comments

Comments
 (0)