13
13
# limitations under the License.
14
14
15
15
import os
16
+ import sys
16
17
import unittest
17
18
18
19
import _gojsonnet
19
20
21
+
20
22
# Returns (full_path, contents) if the file was successfully retrieved,
21
23
# (full_path, None) if file not found, or throws an exception when the path
22
24
# is invalid or an IO error occured.
@@ -39,14 +41,16 @@ def try_path_cached(cache, dir, rel):
39
41
cache [full_path ] = f .read ().encode ()
40
42
return full_path , cache [full_path ]
41
43
42
-
43
- def import_callback (dir , rel ):
44
+ def import_callback_encode (dir , rel ):
44
45
cache = {}
45
46
full_path , content = try_path_cached (cache , dir , rel )
46
47
if content :
47
48
return full_path , content
48
49
raise RuntimeError ('File not found' )
49
50
51
+ def import_callback_empty_file_encode (dir , rel ):
52
+ return dir , b''
53
+
50
54
51
55
# Test native extensions
52
56
def concat (a , b ):
@@ -81,39 +85,96 @@ def setUp(self):
81
85
with open (self .input_filename , "r" ) as infile :
82
86
self .input_snippet = infile .read ()
83
87
84
- def test_evaluate_file (self ):
88
+ def test_version (self ):
89
+ self .assertEqual (type (_gojsonnet .version ), str )
90
+
91
+ def test_evaluate_file_encode (self ):
85
92
json_str = _gojsonnet .evaluate_file (
86
93
self .input_filename ,
87
- import_callback = import_callback ,
94
+ import_callback = import_callback_encode ,
88
95
native_callbacks = native_callbacks ,
89
96
)
90
97
self .assertEqual (json_str , "true\n " )
91
98
92
- def test_evaluate_snippet (self ):
99
+ def test_evaluate_snippet_encode (self ):
93
100
json_str = _gojsonnet .evaluate_snippet (
94
101
self .test_filename ,
95
102
self .input_snippet ,
96
- import_callback = import_callback ,
103
+ import_callback = import_callback_encode ,
97
104
native_callbacks = native_callbacks ,
98
105
)
99
106
self .assertEqual (json_str , "true\n " )
100
107
101
- def test_import (self ):
108
+ def test_evaluate_snippet_encode (self ):
109
+ json_str = _gojsonnet .evaluate_snippet (
110
+ self .test_filename ,
111
+ self .input_snippet ,
112
+ import_callback = import_callback_encode ,
113
+ native_callbacks = native_callbacks ,
114
+ )
115
+ self .assertEqual (json_str , "true\n " )
116
+
117
+ def test_import_encode (self ):
102
118
json_str = _gojsonnet .evaluate_snippet (
103
119
self .test_filename ,
104
120
"import 'trivial.jsonnet'" ,
105
- import_callback = import_callback ,
121
+ import_callback = import_callback_encode ,
106
122
native_callbacks = native_callbacks ,
107
123
)
108
124
self .assertEqual (json_str , "42\n " )
109
125
126
+ def test_import_no_eol_encode (self ):
127
+ json_str = _gojsonnet .evaluate_snippet (
128
+ self .test_filename ,
129
+ "import 'trivial_no_eol.jsonnet'" ,
130
+ import_callback = import_callback_encode ,
131
+ native_callbacks = native_callbacks ,
132
+ )
133
+ self .assertEqual (json_str , "42\n " )
134
+
135
+ def test_import_binary_encode (self ):
136
+ json_str = _gojsonnet .evaluate_snippet (
137
+ self .test_filename ,
138
+ "importbin 'binary123.bin'" ,
139
+ import_callback = import_callback_encode ,
140
+ native_callbacks = native_callbacks ,
141
+ )
142
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3\n ]\n " )
143
+
144
+ def test_import_binary_sentinel_encode (self ):
145
+ json_str = _gojsonnet .evaluate_snippet (
146
+ self .test_filename ,
147
+ "importbin 'binary1230123.bin'" ,
148
+ import_callback = import_callback_encode ,
149
+ native_callbacks = native_callbacks ,
150
+ )
151
+ self .assertEqual (json_str , "[\n 1,\n 2,\n 3,\n 0,\n 1,\n 2,\n 3\n ]\n " )
152
+
153
+ def test_import_str_empty_file_encode (self ):
154
+ json_str = _gojsonnet .evaluate_snippet (
155
+ self .test_filename ,
156
+ "importstr 'binary123.bin'" ,
157
+ import_callback = import_callback_empty_file_encode ,
158
+ native_callbacks = native_callbacks ,
159
+ )
160
+ self .assertEqual (json_str , "\" \" \n " )
161
+
162
+ def test_import_binary_empty_file_encode (self ):
163
+ json_str = _gojsonnet .evaluate_snippet (
164
+ self .test_filename ,
165
+ "importbin 'binary123.bin'" ,
166
+ import_callback = import_callback_empty_file_encode ,
167
+ native_callbacks = native_callbacks ,
168
+ )
169
+ self .assertEqual (json_str , "[ ]\n " )
170
+
110
171
def test_double_import (self ):
111
172
json_str = _gojsonnet .evaluate_snippet (
112
173
self .test_filename ,
113
174
"local x = import 'trivial.jsonnet';\n " +
114
175
"local y = import 'trivial.jsonnet';\n " +
115
176
"x + y" ,
116
- import_callback = import_callback ,
177
+ import_callback = import_callback_encode ,
117
178
native_callbacks = native_callbacks ,
118
179
)
119
180
self .assertEqual (json_str , "84\n " )
0 commit comments