| 1 |
# Copyright 2015 Google Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
import unittest |
| 18 |
|
| 19 |
import _jsonnet |
| 20 |
|
| 21 |
|
| 22 |
# Returns (full_path, contents) if the file was successfully retrieved, |
| 23 |
# (full_path, None) if file not found, or throws an exception when the path |
| 24 |
# is invalid or an IO error occurred. |
| 25 |
# It caches both hits and misses in the `cache` dict. Exceptions |
| 26 |
# do not need to be cached, because they abort the computation anyway. |
| 27 |
def try_path_cached(cache, dir, rel): |
| 28 |
if not rel: |
| 29 |
raise RuntimeError('Got invalid filename (empty string).') |
| 30 |
if rel[0] == '/': |
| 31 |
full_path = rel |
| 32 |
else: |
| 33 |
full_path = dir + rel |
| 34 |
if full_path[-1] == '/': |
| 35 |
raise RuntimeError('Attempted to import a directory') |
| 36 |
if full_path not in cache: |
| 37 |
if not os.path.isfile(full_path): |
| 38 |
cache[full_path] = None |
| 39 |
else: |
| 40 |
with open(full_path) as f: |
| 41 |
cache[full_path] = f.read().encode() |
| 42 |
return full_path, cache[full_path] |
| 43 |
|
| 44 |
def import_callback_encode(dir, rel): |
| 45 |
cache = {} |
| 46 |
full_path, content = try_path_cached(cache, dir, rel) |
| 47 |
if content: |
| 48 |
return full_path, content |
| 49 |
raise RuntimeError('File not found') |
| 50 |
|
| 51 |
def import_callback_empty_file_encode(dir, rel): |
| 52 |
return dir, b'' |
| 53 |
|
| 54 |
|
| 55 |
# Test native extensions |
| 56 |
def concat(a, b): |
| 57 |
return a + b |
| 58 |
|
| 59 |
|
| 60 |
def return_types(): |
| 61 |
return { |
| 62 |
'a': [1, 2, 3, None, []], |
| 63 |
'b': 1.0, |
| 64 |
'c': True, |
| 65 |
'd': None, |
| 66 |
'e': { |
| 67 |
'x': 1, |
| 68 |
'y': 2, |
| 69 |
'z': ['foo'] |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
native_callbacks = { |
| 74 |
'concat': (('a', 'b'), concat), |
| 75 |
'return_types': ((), return_types), |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
class JsonnetTests(unittest.TestCase): |
| 80 |
def setUp(self): |
| 81 |
base_dir = os.path.join(os.path.dirname(__file__), "testdata") |
| 82 |
self.input_filename = os.path.join(base_dir, "basic_check.jsonnet") |
| 83 |
self.trivial_filename = os.path.join(base_dir, "trivial.jsonnet") |
| 84 |
self.test_filename = os.path.join(base_dir, "test.jsonnet") |
| 85 |
with open(self.input_filename, "r") as infile: |
| 86 |
self.input_snippet = infile.read() |
| 87 |
|
| 88 |
def test_version(self): |
| 89 |
self.assertEqual(type(_jsonnet.version), str) |
| 90 |
|
| 91 |
def test_evaluate_file_encode(self): |
| 92 |
json_str = _jsonnet.evaluate_file( |
| 93 |
self.input_filename, |
| 94 |
import_callback=import_callback_encode, |
| 95 |
native_callbacks=native_callbacks, |
| 96 |
) |
| 97 |
self.assertEqual(json_str, "true\n") |
| 98 |
|
| 99 |
def test_evaluate_snippet_encode(self): |
| 100 |
json_str = _jsonnet.evaluate_snippet( |
| 101 |
self.test_filename, |
| 102 |
self.input_snippet, |
| 103 |
import_callback=import_callback_encode, |
| 104 |
native_callbacks=native_callbacks, |
| 105 |
) |
| 106 |
self.assertEqual(json_str, "true\n") |
| 107 |
|
| 108 |
def test_evaluate_snippet_encode(self): |
| 109 |
json_str = _jsonnet.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): |
| 118 |
json_str = _jsonnet.evaluate_snippet( |
| 119 |
self.test_filename, |
| 120 |
"import 'trivial.jsonnet'", |
| 121 |
import_callback=import_callback_encode, |
| 122 |
native_callbacks=native_callbacks, |
| 123 |
) |
| 124 |
self.assertEqual(json_str, "42\n") |
| 125 |
|
| 126 |
def test_import_no_eol_encode(self): |
| 127 |
json_str = _jsonnet.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 = _jsonnet.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 = _jsonnet.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 = _jsonnet.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 = _jsonnet.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 |
|
| 171 |
def test_double_import(self): |
| 172 |
json_str = _jsonnet.evaluate_snippet( |
| 173 |
self.test_filename, |
| 174 |
"local x = import 'trivial.jsonnet';\n" + |
| 175 |
"local y = import 'trivial.jsonnet';\n" + |
| 176 |
"x + y", |
| 177 |
import_callback=import_callback_encode, |
| 178 |
native_callbacks=native_callbacks, |
| 179 |
) |
| 180 |
self.assertEqual(json_str, "84\n") |
| 181 |
|
| 182 |
if __name__ == '__main__': |
| 183 |
unittest.main() |