13
13
# limitations under the License.
14
14
15
15
import os
16
+ import sys
16
17
import unittest
17
18
18
19
import _jsonnet
19
20
20
21
21
22
# Returns content if worked, None if file not found, or throws an exception
22
- def try_path (dir , rel ):
23
+ def try_path (dir , rel , do_encode ):
23
24
if not rel :
24
25
raise RuntimeError ('Got invalid filename (empty string).' )
25
26
if rel [0 ] == '/' :
@@ -32,15 +33,31 @@ def try_path(dir, rel):
32
33
if not os .path .isfile (full_path ):
33
34
return full_path , None
34
35
with open (full_path ) as f :
35
- return full_path , f .read ().encode ()
36
+ if do_encode :
37
+ return full_path , f .read ().encode ()
38
+ else :
39
+ return full_path , f .read ()
36
40
37
41
38
- def import_callback (dir , rel ):
39
- full_path , content = try_path (dir , rel )
42
+ def import_callback_encode (dir , rel ):
43
+ full_path , content = try_path (dir , rel , do_encode = True )
40
44
if content :
41
45
return full_path , content
42
46
raise RuntimeError ('File not found' )
43
47
48
+ def import_callback_no_encode (dir , rel ):
49
+ full_path , content = try_path (dir , rel , do_encode = False )
50
+ if content :
51
+ return full_path , content
52
+ raise RuntimeError ('File not found' )
53
+
54
+ def import_callback_empty_file_encode (dir , rel ):
55
+ return dir , b''
56
+
57
+
58
+ def import_callback_empty_file_no_encode (dir , rel ):
59
+ return dir , ''
60
+
44
61
45
62
# Test native extensions
46
63
def concat (a , b ):
@@ -75,39 +92,178 @@ def setUp(self):
75
92
with open (self .input_filename , "r" ) as infile :
76
93
self .input_snippet = infile .read ()
77
94
78
- def test_evaluate_file (self ):
95
+ def test_evaluate_file_encode (self ):
96
+ json_str = _jsonnet .evaluate_file (
97
+ self .input_filename ,
98
+ import_callback = import_callback_encode ,
99
+ native_callbacks = native_callbacks ,
100
+ )
101
+ self .assertEqual (json_str , "true\n " )
102
+
103
+ def test_evaluate_file_no_encode (self ):
79
104
json_str = _jsonnet .evaluate_file (
80
105
self .input_filename ,
81
- import_callback = import_callback ,
106
+ import_callback = import_callback_no_encode ,
107
+ native_callbacks = native_callbacks ,
108
+ )
109
+ self .assertEqual (json_str , "true\n " )
110
+
111
+ def test_evaluate_snippet_encode (self ):
112
+ json_str = _jsonnet .evaluate_snippet (
113
+ self .test_filename ,
114
+ self .input_snippet ,
115
+ import_callback = import_callback_encode ,
82
116
native_callbacks = native_callbacks ,
83
117
)
84
118
self .assertEqual (json_str , "true\n " )
85
119
86
- def test_evaluate_snippet (self ):
120
+ def test_evaluate_snippet_no_encode (self ):
87
121
json_str = _jsonnet .evaluate_snippet (
88
122
self .test_filename ,
89
123
self .input_snippet ,
90
- import_callback = import_callback ,
124
+ import_callback = import_callback_no_encode ,
91
125
native_callbacks = native_callbacks ,
92
126
)
93
127
self .assertEqual (json_str , "true\n " )
94
128
95
- def test_import (self ):
129
+ def test_evaluate_snippet_encode (self ):
130
+ json_str = _jsonnet .evaluate_snippet (
131
+ self .test_filename ,
132
+ self .input_snippet ,
133
+ import_callback = import_callback_encode ,
134
+ native_callbacks = native_callbacks ,
135
+ )
136
+ self .assertEqual (json_str , "true\n " )
137
+
138
+ def test_evaluate_snippet_no_encode (self ):
139
+ json_str = _jsonnet .evaluate_snippet (
140
+ self .test_filename ,
141
+ self .input_snippet ,
142
+ import_callback = import_callback_no_encode ,
143
+ native_callbacks = native_callbacks ,
144
+ )
145
+ self .assertEqual (json_str , "true\n " )
146
+
147
+ def test_import_encode (self ):
96
148
json_str = _jsonnet .evaluate_snippet (
97
149
self .test_filename ,
98
150
"import 'trivial.jsonnet'" ,
99
- import_callback = import_callback ,
151
+ import_callback = import_callback_encode ,
152
+ native_callbacks = native_callbacks ,
153
+ )
154
+ self .assertEqual (json_str , "42\n " )
155
+
156
+ def test_import_no_encode (self ):
157
+ json_str = _jsonnet .evaluate_snippet (
158
+ self .test_filename ,
159
+ "import 'trivial.jsonnet'" ,
160
+ import_callback = import_callback_no_encode ,
161
+ native_callbacks = native_callbacks ,
162
+ )
163
+ self .assertEqual (json_str , "42\n " )
164
+
165
+ def test_import_no_eol_encode (self ):
166
+ json_str = _jsonnet .evaluate_snippet (
167
+ self .test_filename ,
168
+ "import 'trivial_no_eol.jsonnet'" ,
169
+ import_callback = import_callback_encode ,
100
170
native_callbacks = native_callbacks ,
101
171
)
102
172
self .assertEqual (json_str , "42\n " )
103
173
174
+ def test_import_no_eol_no_encode (self ):
175
+ json_str = _jsonnet .evaluate_snippet (
176
+ self .test_filename ,
177
+ "import 'trivial_no_eol.jsonnet'" ,
178
+ import_callback = import_callback_no_encode ,
179
+ native_callbacks = native_callbacks ,
180
+ )
181
+ self .assertEqual (json_str , "42\n " )
182
+
183
+ def test_import_binary_encode (self ):
184
+ json_str = _jsonnet .evaluate_snippet (
185
+ self .test_filename ,
186
+ "importbin 'binary123.bin'" ,
187
+ import_callback = import_callback_encode ,
188
+ native_callbacks = native_callbacks ,
189
+ )
190
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
191
+
192
+ def test_import_binary_no_encode (self ):
193
+ json_str = _jsonnet .evaluate_snippet (
194
+ self .test_filename ,
195
+ "importbin 'binary123.bin'" ,
196
+ import_callback = import_callback_no_encode ,
197
+ native_callbacks = native_callbacks ,
198
+ )
199
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
200
+
201
+ def test_import_binary_sentinel_encode (self ):
202
+ json_str = _jsonnet .evaluate_snippet (
203
+ self .test_filename ,
204
+ "importbin 'binary1230123.bin'" ,
205
+ import_callback = import_callback_encode ,
206
+ native_callbacks = native_callbacks ,
207
+ )
208
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3,\n 0,\n 1,\n 2,\n 3\n ]\n " )
209
+
210
+ def test_import_binary_sentinel_no_encode (self ):
211
+ json_str = _jsonnet .evaluate_snippet (
212
+ self .test_filename ,
213
+ "importbin 'binary1230123.bin'" ,
214
+ import_callback = import_callback_no_encode ,
215
+ native_callbacks = native_callbacks ,
216
+ )
217
+ if sys .version_info .major < 3 :
218
+ # In Python2, the string can actually have a 0 in the middle
219
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3,\n 0,\n 1,\n 2,\n 3\n ]\n " )
220
+ else :
221
+ # In Pyton3, the string is truncated
222
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
223
+
224
+ def test_import_str_empty_file_encode (self ):
225
+ json_str = _jsonnet .evaluate_snippet (
226
+ self .test_filename ,
227
+ "importstr 'binary123.bin'" ,
228
+ import_callback = import_callback_empty_file_encode ,
229
+ native_callbacks = native_callbacks ,
230
+ )
231
+ self .assertEqual (json_str , "\" \" \n " )
232
+
233
+ def test_import_str_empty_file_no_encode (self ):
234
+ json_str = _jsonnet .evaluate_snippet (
235
+ self .test_filename ,
236
+ "importstr 'binary123.bin'" ,
237
+ import_callback = import_callback_empty_file_no_encode ,
238
+ native_callbacks = native_callbacks ,
239
+ )
240
+ self .assertEqual (json_str , "\" \" \n " )
241
+
242
+ def test_import_binary_empty_file_encode (self ):
243
+ json_str = _jsonnet .evaluate_snippet (
244
+ self .test_filename ,
245
+ "importbin 'binary123.bin'" ,
246
+ import_callback = import_callback_empty_file_encode ,
247
+ native_callbacks = native_callbacks ,
248
+ )
249
+ self .assertEqual (json_str , "[ ]\n " )
250
+
251
+ def test_import_binary_empty_file_no_encode (self ):
252
+ json_str = _jsonnet .evaluate_snippet (
253
+ self .test_filename ,
254
+ "importbin 'binary123.bin'" ,
255
+ import_callback = import_callback_empty_file_no_encode ,
256
+ native_callbacks = native_callbacks ,
257
+ )
258
+ self .assertEqual (json_str , "[ ]\n " )
259
+
104
260
def test_double_import (self ):
105
261
json_str = _jsonnet .evaluate_snippet (
106
262
self .test_filename ,
107
263
"local x = import 'trivial.jsonnet';\n " +
108
264
"local y = import 'trivial.jsonnet';\n " +
109
265
"x + y" ,
110
- import_callback = import_callback ,
266
+ import_callback = import_callback_encode ,
111
267
native_callbacks = native_callbacks ,
112
268
)
113
269
self .assertEqual (json_str , "84\n " )
0 commit comments