Mercurial > hgrepos > Python > libs > ConfigMix
comparison tests/test.py @ 144:7e6ec99d5ff5
Allow comments as keys and filter them by default
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Fri, 13 Apr 2018 09:51:02 +0200 |
| parents | d8d47893df5b |
| children | bbf47bfb48a2 |
comparison
equal
deleted
inserted
replaced
| 143:252645c69c7b | 144:7e6ec99d5ff5 |
|---|---|
| 41 self.assertTrue(isinstance(cfg.get("key5"), bool)) | 41 self.assertTrue(isinstance(cfg.get("key5"), bool)) |
| 42 self.assertEqual(255, cfg.get("key6")) | 42 self.assertEqual(255, cfg.get("key6")) |
| 43 self.assertEqual(u("Umlaute: ÄÖÜäöüß"), | 43 self.assertEqual(u("Umlaute: ÄÖÜäöüß"), |
| 44 cfg.get("key7")) | 44 cfg.get("key7")) |
| 45 | 45 |
| 46 def __check_comment(self, cfg): | |
| 47 # Check comments: low level comments are *not* filtered | |
| 48 self.assertEqual(u("Comment 1"), cfg.get("__comment1")) | |
| 49 self.assertEqual(u("Comment no 2"), cfg.get("__comment2")) | |
| 50 | |
| 51 def __check_no_comment(self, cfg): | |
| 52 | |
| 53 def _c(name): | |
| 54 def _f(): | |
| 55 cfg[u(name)] | |
| 56 return _f | |
| 57 | |
| 58 # Variables with leading underscores are *not* imported by default | |
| 59 self.assertRaises(KeyError, _c("__comment1")) | |
| 60 self.assertRaises(KeyError, _c("__comment2")) | |
| 61 | |
| 46 def __check_tree(self, cfg): | 62 def __check_tree(self, cfg): |
| 47 self.assertEqual(u("in the root namespace"), | 63 self.assertEqual(u("in the root namespace"), |
| 48 cfg.get("key1")) | 64 cfg.get("key1")) |
| 49 self.assertEqual(u("in the root namespace -- too"), | 65 self.assertEqual(u("in the root namespace -- too"), |
| 50 cfg.get("key2")) | 66 cfg.get("key2")) |
| 55 self.assertTrue(cfg["tree1"]["tree2"]["key5"]) | 71 self.assertTrue(cfg["tree1"]["tree2"]["key5"]) |
| 56 | 72 |
| 57 def test01_ini_types(self): | 73 def test01_ini_types(self): |
| 58 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | 74 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) |
| 59 self.__check_types(cfg) | 75 self.__check_types(cfg) |
| 76 self.__check_comment(cfg) | |
| 60 | 77 |
| 61 def test02_py_types(self): | 78 def test02_py_types(self): |
| 62 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf1.py")) | 79 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf1.py")) |
| 63 self.__check_types(cfg) | 80 self.__check_types(cfg) |
| 81 self.__check_no_comment(cfg) | |
| 64 | 82 |
| 65 def test03_yaml_types(self): | 83 def test03_yaml_types(self): |
| 66 with io.open(os.path.join(TESTDATADIR, "conf1.yml"), "rt", | 84 with io.open(os.path.join(TESTDATADIR, "conf1.yml"), "rt", |
| 67 encoding="utf-8") as f: | 85 encoding="utf-8") as f: |
| 68 cfg = configmix.yaml.safe_load(f) | 86 cfg = configmix.yaml.safe_load(f) |
| 69 if configmix.yaml.OrderedDict: | 87 if configmix.yaml.OrderedDict: |
| 70 self.assertTrue(isinstance(cfg, configmix.yaml.OrderedDict)) | 88 self.assertTrue(isinstance(cfg, configmix.yaml.OrderedDict)) |
| 71 self.__check_types(cfg) | 89 self.__check_types(cfg) |
| 90 self.__check_comment(cfg) | |
| 72 | 91 |
| 73 def test04_json_types(self): | 92 def test04_json_types(self): |
| 74 cfg = configmix.json.load(os.path.join(TESTDATADIR, "conf1.json")) | 93 cfg = configmix.json.load(os.path.join(TESTDATADIR, "conf1.json")) |
| 75 self.assertTrue(isinstance(cfg, configmix.json.DictImpl)) | 94 self.assertTrue(isinstance(cfg, configmix.json.DictImpl)) |
| 76 self.__check_types(cfg) | 95 self.__check_types(cfg) |
| 96 self.__check_comment(cfg) | |
| 77 | 97 |
| 78 def test05_py_export_all(self): | 98 def test05_py_export_all(self): |
| 79 # When __all__ is given only it's keys are exported | 99 # When __all__ is given only it's keys are exported |
| 80 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf2.py")) | 100 cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf2.py")) |
| 81 self.assertEqual(u("the next value"), cfg.get("key1")) | 101 self.assertEqual(u("the next value"), cfg.get("key1")) |
| 216 def _look(): | 236 def _look(): |
| 217 return cfg.getvar("OS:cwd|upper") | 237 return cfg.getvar("OS:cwd|upper") |
| 218 | 238 |
| 219 self.assertRaises(KeyError, _look) | 239 self.assertRaises(KeyError, _look) |
| 220 | 240 |
| 241 def test05_comments(self): | |
| 242 cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"), | |
| 243 os.path.join(TESTDATADIR, "conf21.yml"), | |
| 244 os.path.join(TESTDATADIR, "conf22.ini"), | |
| 245 os.path.join(TESTDATADIR, "conf23.json")) | |
| 246 | |
| 247 def _c(name): | |
| 248 def _f(): | |
| 249 cfg.getvar_s(name) | |
| 250 return _f | |
| 251 | |
| 252 # Variables with leading underscores are *not* imported by default | |
| 253 self.assertEqual(0o0027, int(cfg.getvar_s("process.umask"), 0)) | |
| 254 self.assertRaises(KeyError, _c("process.__doc1")) | |
| 255 self.assertRaises(KeyError, _c("db.__comment1")) | |
| 256 self.assertRaises(KeyError, _c("db.user.__doc2")) | |
| 257 | |
| 221 | 258 |
| 222 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): | 259 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): |
| 223 | 260 |
| 224 def setUp(self): | 261 def setUp(self): |
| 225 self._load = configmix.load | 262 self._load = configmix.load |
| 226 | 263 |
| 227 def test05_identity(self): | 264 def test06_identity(self): |
| 228 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | 265 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) |
| 229 cfg2 = configmix.merge(cfg, None) | 266 cfg2 = configmix.merge(cfg, None) |
| 230 self.assertEqual(id(cfg), id(cfg2)) | 267 self.assertEqual(id(cfg), id(cfg2)) |
| 231 | 268 |
| 232 def test06_identity(self): | 269 def test07_identity(self): |
| 233 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | 270 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) |
| 234 cfg2 = configmix.merge(cfg, {}) | 271 cfg2 = configmix.merge(cfg, {}) |
| 235 self.assertEqual(id(cfg), id(cfg2)) | 272 self.assertEqual(id(cfg), id(cfg2)) |
| 236 | 273 |
| 237 | 274 |
| 238 class T03SafeLoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): | 275 class T03SafeLoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): |
| 239 | 276 |
| 240 def setUp(self): | 277 def setUp(self): |
| 241 self._load = configmix.safe_load | 278 self._load = configmix.safe_load |
| 242 | 279 |
| 243 def test05_deepcopy(self): | 280 def test06_deepcopy(self): |
| 244 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | 281 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) |
| 245 cfg2 = configmix.safe_merge(cfg, None) | 282 cfg2 = configmix.safe_merge(cfg, None) |
| 246 self.assertNotEqual(id(cfg), id(cfg2)) | 283 self.assertNotEqual(id(cfg), id(cfg2)) |
| 247 | 284 |
| 248 def test06_deepcopy(self): | 285 def test07_deepcopy(self): |
| 249 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) | 286 cfg = configmix.ini.load(os.path.join(TESTDATADIR, "conf1.ini")) |
| 250 cfg2 = configmix.safe_merge(cfg, {}) | 287 cfg2 = configmix.safe_merge(cfg, {}) |
| 251 self.assertNotEqual(id(cfg), id(cfg2)) | 288 self.assertNotEqual(id(cfg), id(cfg2)) |
| 252 | 289 |
| 253 | 290 |
| 288 cfg = configmix.load(os.path.join(TESTDATADIR, "conf30.conf")) | 325 cfg = configmix.load(os.path.join(TESTDATADIR, "conf30.conf")) |
| 289 self.assertEqual(u("new value"), cfg.getvar_s("key-new")) | 326 self.assertEqual(u("new value"), cfg.getvar_s("key-new")) |
| 290 | 327 |
| 291 def _g(): | 328 def _g(): |
| 292 return cfg.getvar_s("key7") | 329 return cfg.getvar_s("key7") |
| 293 | 330 |
| 294 self.assertRaises(KeyError, _g) | 331 self.assertRaises(KeyError, _g) |
| 295 | 332 |
| 296 | 333 |
| 297 if __name__ == "__main__": | 334 if __name__ == "__main__": |
| 298 unittest.main() | 335 unittest.main() |
