2
2
import json
3
3
import subprocess
4
4
import sys
5
+ import os
5
6
6
7
from jsonschema import Draft4Validator , ValidationError , cli , __version__
7
8
from jsonschema .compat import NativeIO
8
9
from jsonschema .exceptions import SchemaError
9
10
11
+ JSON_DIR_PATH = os .path .join (os .path .dirname (__file__ ), "json_files" )
10
12
11
13
def fake_validator (* errors ):
12
14
errors = list (reversed (errors ))
@@ -70,32 +72,34 @@ def test_find_validator_in_jsonschema(self):
70
72
class TestCLI (TestCase ):
71
73
def test_draft3_schema_draft4_validator (self ):
72
74
stdout , stderr = NativeIO (), NativeIO ()
73
- with self .assertRaises (SchemaError ):
74
- cli .run (
75
- {
76
- "validator" : Draft4Validator ,
77
- "schema" : {
78
- "anyOf" : [
79
- {"minimum" : 20 },
80
- {"type" : "string" },
81
- {"required" : True },
82
- ],
83
- },
84
- "instances" : [1 ],
85
- "error_format" : "{error.message}" ,
86
- },
87
- stdout = stdout ,
88
- stderr = stderr ,
89
- )
75
+ exit_code = cli .run (
76
+ {
77
+ "validator" : Draft4Validator ,
78
+ "schema" : os .path .join (JSON_DIR_PATH , "schema1.json" ),
79
+ "instances" : [
80
+ os .path .join (JSON_DIR_PATH , "instance1.json" ),
81
+ ],
82
+ "error_format" : "{error.message}" ,
83
+ "output" : "plain" ,
84
+ },
85
+ stdout = stdout ,
86
+ stderr = stderr ,
87
+ )
88
+ self .assertFalse (stdout .getvalue ())
89
+ self .assertTrue (stderr .getvalue ())
90
+ self .assertEqual (exit_code , 0 )
90
91
91
92
def test_successful_validation (self ):
92
93
stdout , stderr = NativeIO (), NativeIO ()
93
94
exit_code = cli .run (
94
95
{
95
96
"validator" : fake_validator (),
96
- "schema" : {},
97
- "instances" : [1 ],
97
+ "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
98
+ "instances" : [
99
+ os .path .join (JSON_DIR_PATH , "instance2.json" ),
100
+ ],
98
101
"error_format" : "{error.message}" ,
102
+ "output" : "plain" ,
99
103
},
100
104
stdout = stdout ,
101
105
stderr = stderr ,
@@ -110,15 +114,18 @@ def test_unsuccessful_validation(self):
110
114
exit_code = cli .run (
111
115
{
112
116
"validator" : fake_validator ([error ]),
113
- "schema" : {},
114
- "instances" : [1 ],
117
+ "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
118
+ "instances" : [
119
+ os .path .join (JSON_DIR_PATH , "instance1.json" ),
120
+ ],
115
121
"error_format" : "{error.instance} - {error.message}" ,
122
+ "output" : "plain" ,
116
123
},
117
124
stdout = stdout ,
118
125
stderr = stderr ,
119
126
)
120
127
self .assertFalse (stdout .getvalue ())
121
- self .assertEqual (stderr .getvalue (), "1 - I am an error!" )
128
+ self .assertEqual (stderr .getvalue (), "1 - I am an error!\n " )
122
129
self .assertEqual (exit_code , 1 )
123
130
124
131
def test_unsuccessful_validation_multiple_instances (self ):
@@ -131,15 +138,19 @@ def test_unsuccessful_validation_multiple_instances(self):
131
138
exit_code = cli .run (
132
139
{
133
140
"validator" : fake_validator (first_errors , second_errors ),
134
- "schema" : {},
135
- "instances" : [1 , 2 ],
136
- "error_format" : "{error.instance} - {error.message}\t " ,
141
+ "schema" : os .path .join (JSON_DIR_PATH , "schema2.json" ),
142
+ "instances" : [
143
+ os .path .join (JSON_DIR_PATH , "instance1.json" ),
144
+ os .path .join (JSON_DIR_PATH , "instance2.json" ),
145
+ ],
146
+ "error_format" : "{error.instance} - {error.message}" ,
147
+ "output" : "plain" ,
137
148
},
138
149
stdout = stdout ,
139
150
stderr = stderr ,
140
151
)
141
152
self .assertFalse (stdout .getvalue ())
142
- self .assertEqual (stderr .getvalue (), "1 - 9\t 1 - 8\t 2 - 7\t " )
153
+ self .assertEqual (stderr .getvalue (), "1 - 9\n 1 - 8\n 2 - 7\n " )
143
154
self .assertEqual (exit_code , 1 )
144
155
145
156
def test_license (self ):
@@ -162,9 +173,10 @@ def test_piping(self):
162
173
exit_code = cli .run (
163
174
{
164
175
"validator" : fake_validator (),
165
- "schema" : {} ,
176
+ "schema" : os . path . join ( JSON_DIR_PATH , "schema2.json" ) ,
166
177
"instances" : [],
167
178
"error_format" : "{error.message}" ,
179
+ "output" : "plain" ,
168
180
},
169
181
stdout = stdout ,
170
182
stderr = stderr ,
0 commit comments