1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import contextlib
import os
import shutil
import tempfile
import unittest
from json5_generator import Json5File, Writer
@contextlib.contextmanager
def tmp_dir():
tmp = tempfile.mkdtemp(prefix='json5_generator_')
try:
yield tmp
finally:
shutil.rmtree(tmp)
class CleanupWriter(Writer):
def __init__(self, output_dir, cleanup):
super(CleanupWriter, self).__init__([], output_dir)
self._cleanup = cleanup
class Json5FileTest(unittest.TestCase):
def path_of_test_file(self, file_name):
return os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'tests', file_name)
def test_valid_dict_value_parse(self):
actual = Json5File.load_from_files([
self.path_of_test_file('json5_generator_valid_dict_value.json5')
]).name_dictionaries
expected = [{
'name': 'item1',
'param1': {
'keys': 'valid',
'random': 'values'
}
}, {
'name': 'item2',
'param1': {
'random': 'values',
'default': 'valid'
}
}, {
'name': 'item3',
'param1': {
'keys': 'valid',
'default': 'values'
}
}]
self.assertEqual(len(actual), len(expected))
for exp, act in zip(expected, actual):
self.assertDictEqual(exp['param1'], act['param1'])
self.assertIsNone(act['param2'])
def test_valid_dict_value_parse_override(self):
json5_file = Json5File.load_from_files(
[self.path_of_test_file('json5_generator_valid_dict_value.json5')])
json5_file.load_override_file(
self.path_of_test_file(
'json5_generator_valid_dict_value.override.json5'))
actual = json5_file.name_dictionaries
expected = [{
'name': 'item1',
'param1': {
'keys': 'valid',
'default': 'values'
}
}, {
'name': 'item2',
'param1': {
'random': 'values',
'default': 'valid'
}
}, {
'name': 'item3',
'param2': {
'key': 'single',
'default': 'value'
}
}, {
'name': 'item4',
'param1': {
'keys': 'valid',
'random': 'values'
}
}]
self.assertEqual(len(actual), len(expected))
for exp, act in zip(expected, actual):
param_name = 'param1'
if exp['name'] == 'item3':
self.assertIsNone(act['param1'])
param_name = 'param2'
self.assertDictEqual(exp[param_name], act[param_name])
def test_no_valid_keys(self):
with self.assertRaises(AssertionError):
Json5File.load_from_files([
self.path_of_test_file('json5_generator_no_valid_keys.json5')
])
def test_value_not_in_valid_values(self):
with self.assertRaises(Exception):
Json5File.load_from_files([
self.path_of_test_file('json5_generator_invalid_value.json5')
])
def test_key_not_in_valid_keys(self):
with self.assertRaises(Exception):
Json5File.load_from_files(
[self.path_of_test_file('json5_generator_invalid_key.json5')])
def test_cleanup_multiple_files(self):
with tmp_dir() as tmp:
path1 = os.path.join(tmp, 'file1.h')
path2 = os.path.join(tmp, 'file2.h')
with open(path1, 'wb') as f:
f.write(b'File1')
with open(path2, 'wb') as f:
f.write(b'File2')
self.assertTrue(os.path.exists(path1))
self.assertTrue(os.path.exists(path2))
CleanupWriter(tmp, set(['file1.h', 'file2.h'])).cleanup_files(tmp)
self.assertFalse(os.path.exists(path1))
self.assertFalse(os.path.exists(path2))
def test_cleanup_partial_files(self):
with tmp_dir() as tmp:
path1 = os.path.join(tmp, 'file1.h')
path2 = os.path.join(tmp, 'file2.h')
with open(path1, 'wb') as f:
f.write(b'File1')
with open(path2, 'wb') as f:
f.write(b'File2')
self.assertTrue(os.path.exists(path1))
self.assertTrue(os.path.exists(path2))
CleanupWriter(tmp, set(['file2.h'])).cleanup_files(tmp)
self.assertTrue(os.path.exists(path1))
self.assertFalse(os.path.exists(path2))
def test_cleanup_nonexisting(self):
with tmp_dir() as tmp:
path1 = os.path.join(tmp, 'file1.h')
with open(path1, 'wb') as f:
f.write(b'File1')
self.assertTrue(os.path.exists(path1))
# Don't throw when trying to clean up something that doesn't exist.
CleanupWriter(tmp, set(['file1.h', 'file2.h'])).cleanup_files(tmp)
self.assertFalse(os.path.exists(path1))
if __name__ == "__main__":
unittest.main()
|