24
24
# is invalid or an IO error occured.
25
25
# It caches both hits and misses in the `cache` dict. Exceptions
26
26
# do not need to be cached, because they abort the computation anyway.
27
- # if do_encode is True, then the file is returned as bytes
28
- # instead of as a string (this does not make a difference in Python2).
29
- def try_path_cached (cache , dir , rel , do_encode ):
27
+ def try_path_cached (cache , dir , rel ):
30
28
if not rel :
31
29
raise RuntimeError ('Got invalid filename (empty string).' )
32
30
if rel [0 ] == '/' :
@@ -40,22 +38,12 @@ def try_path_cached(cache, dir, rel, do_encode):
40
38
cache [full_path ] = None
41
39
else :
42
40
with open (full_path ) as f :
43
- if do_encode :
44
- cache [full_path ] = f .read ().encode ()
45
- else :
46
- cache [full_path ] = f .read ()
41
+ cache [full_path ] = f .read ().encode ()
47
42
return full_path , cache [full_path ]
48
43
49
44
def import_callback_encode (dir , rel ):
50
45
cache = {}
51
- full_path , content = try_path_cached (cache , dir , rel , do_encode = True )
52
- if content :
53
- return full_path , content
54
- raise RuntimeError ('File not found' )
55
-
56
- def import_callback_no_encode (dir , rel ):
57
- cache = {}
58
- full_path , content = try_path_cached (cache , dir , rel , do_encode = False )
46
+ full_path , content = try_path_cached (cache , dir , rel )
59
47
if content :
60
48
return full_path , content
61
49
raise RuntimeError ('File not found' )
@@ -64,10 +52,6 @@ def import_callback_empty_file_encode(dir, rel):
64
52
return dir , b''
65
53
66
54
67
- def import_callback_empty_file_no_encode (dir , rel ):
68
- return dir , ''
69
-
70
-
71
55
# Test native extensions
72
56
def concat (a , b ):
73
57
return a + b
@@ -101,6 +85,9 @@ def setUp(self):
101
85
with open (self .input_filename , "r" ) as infile :
102
86
self .input_snippet = infile .read ()
103
87
88
+ def test_version (self ):
89
+ self .assertEqual (type (_jsonnet .version ), str )
90
+
104
91
def test_evaluate_file_encode (self ):
105
92
json_str = _jsonnet .evaluate_file (
106
93
self .input_filename ,
@@ -109,14 +96,6 @@ def test_evaluate_file_encode(self):
109
96
)
110
97
self .assertEqual (json_str , "true\n " )
111
98
112
- def test_evaluate_file_no_encode (self ):
113
- json_str = _jsonnet .evaluate_file (
114
- self .input_filename ,
115
- import_callback = import_callback_no_encode ,
116
- native_callbacks = native_callbacks ,
117
- )
118
- self .assertEqual (json_str , "true\n " )
119
-
120
99
def test_evaluate_snippet_encode (self ):
121
100
json_str = _jsonnet .evaluate_snippet (
122
101
self .test_filename ,
@@ -126,15 +105,6 @@ def test_evaluate_snippet_encode(self):
126
105
)
127
106
self .assertEqual (json_str , "true\n " )
128
107
129
- def test_evaluate_snippet_no_encode (self ):
130
- json_str = _jsonnet .evaluate_snippet (
131
- self .test_filename ,
132
- self .input_snippet ,
133
- import_callback = import_callback_no_encode ,
134
- native_callbacks = native_callbacks ,
135
- )
136
- self .assertEqual (json_str , "true\n " )
137
-
138
108
def test_evaluate_snippet_encode (self ):
139
109
json_str = _jsonnet .evaluate_snippet (
140
110
self .test_filename ,
@@ -144,15 +114,6 @@ def test_evaluate_snippet_encode(self):
144
114
)
145
115
self .assertEqual (json_str , "true\n " )
146
116
147
- def test_evaluate_snippet_no_encode (self ):
148
- json_str = _jsonnet .evaluate_snippet (
149
- self .test_filename ,
150
- self .input_snippet ,
151
- import_callback = import_callback_no_encode ,
152
- native_callbacks = native_callbacks ,
153
- )
154
- self .assertEqual (json_str , "true\n " )
155
-
156
117
def test_import_encode (self ):
157
118
json_str = _jsonnet .evaluate_snippet (
158
119
self .test_filename ,
@@ -162,15 +123,6 @@ def test_import_encode(self):
162
123
)
163
124
self .assertEqual (json_str , "42\n " )
164
125
165
- def test_import_no_encode (self ):
166
- json_str = _jsonnet .evaluate_snippet (
167
- self .test_filename ,
168
- "import 'trivial.jsonnet'" ,
169
- import_callback = import_callback_no_encode ,
170
- native_callbacks = native_callbacks ,
171
- )
172
- self .assertEqual (json_str , "42\n " )
173
-
174
126
def test_import_no_eol_encode (self ):
175
127
json_str = _jsonnet .evaluate_snippet (
176
128
self .test_filename ,
@@ -180,15 +132,6 @@ def test_import_no_eol_encode(self):
180
132
)
181
133
self .assertEqual (json_str , "42\n " )
182
134
183
- def test_import_no_eol_no_encode (self ):
184
- json_str = _jsonnet .evaluate_snippet (
185
- self .test_filename ,
186
- "import 'trivial_no_eol.jsonnet'" ,
187
- import_callback = import_callback_no_encode ,
188
- native_callbacks = native_callbacks ,
189
- )
190
- self .assertEqual (json_str , "42\n " )
191
-
192
135
def test_import_binary_encode (self ):
193
136
json_str = _jsonnet .evaluate_snippet (
194
137
self .test_filename ,
@@ -198,15 +141,6 @@ def test_import_binary_encode(self):
198
141
)
199
142
self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
200
143
201
- def test_import_binary_no_encode (self ):
202
- json_str = _jsonnet .evaluate_snippet (
203
- self .test_filename ,
204
- "importbin 'binary123.bin'" ,
205
- import_callback = import_callback_no_encode ,
206
- native_callbacks = native_callbacks ,
207
- )
208
- self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
209
-
210
144
def test_import_binary_sentinel_encode (self ):
211
145
json_str = _jsonnet .evaluate_snippet (
212
146
self .test_filename ,
@@ -216,20 +150,6 @@ def test_import_binary_sentinel_encode(self):
216
150
)
217
151
self .assertEqual (json_str , "[\n 1,\n 2,\n 3,\n 0,\n 1,\n 2,\n 3\n ]\n " )
218
152
219
- def test_import_binary_sentinel_no_encode (self ):
220
- json_str = _jsonnet .evaluate_snippet (
221
- self .test_filename ,
222
- "importbin 'binary1230123.bin'" ,
223
- import_callback = import_callback_no_encode ,
224
- native_callbacks = native_callbacks ,
225
- )
226
- if sys .version_info .major < 3 :
227
- # In Python2, the string can actually have a 0 in the middle
228
- self .assertEqual (json_str , "[\n 1,\n 2,\n 3,\n 0,\n 1,\n 2,\n 3\n ]\n " )
229
- else :
230
- # In Pyton3, the string is truncated
231
- self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
232
-
233
153
def test_import_str_empty_file_encode (self ):
234
154
json_str = _jsonnet .evaluate_snippet (
235
155
self .test_filename ,
@@ -239,15 +159,6 @@ def test_import_str_empty_file_encode(self):
239
159
)
240
160
self .assertEqual (json_str , "\" \" \n " )
241
161
242
- def test_import_str_empty_file_no_encode (self ):
243
- json_str = _jsonnet .evaluate_snippet (
244
- self .test_filename ,
245
- "importstr 'binary123.bin'" ,
246
- import_callback = import_callback_empty_file_no_encode ,
247
- native_callbacks = native_callbacks ,
248
- )
249
- self .assertEqual (json_str , "\" \" \n " )
250
-
251
162
def test_import_binary_empty_file_encode (self ):
252
163
json_str = _jsonnet .evaluate_snippet (
253
164
self .test_filename ,
@@ -257,15 +168,6 @@ def test_import_binary_empty_file_encode(self):
257
168
)
258
169
self .assertEqual (json_str , "[ ]\n " )
259
170
260
- def test_import_binary_empty_file_no_encode (self ):
261
- json_str = _jsonnet .evaluate_snippet (
262
- self .test_filename ,
263
- "importbin 'binary123.bin'" ,
264
- import_callback = import_callback_empty_file_no_encode ,
265
- native_callbacks = native_callbacks ,
266
- )
267
- self .assertEqual (json_str , "[ ]\n " )
268
-
269
171
def test_double_import (self ):
270
172
json_str = _jsonnet .evaluate_snippet (
271
173
self .test_filename ,
0 commit comments