Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22import unittest 

23import os 

24import contextlib 

25import collections 

26import itertools 

27import shutil 

28import tempfile 

29 

30from lsst.daf.butler import ConfigSubset, Config 

31 

32 

33@contextlib.contextmanager 

34def modified_environment(**environ): 

35 """ 

36 Temporarily set environment variables. 

37 

38 >>> with modified_environment(DAF_BUTLER_CONFIG_PATHS="/somewhere"): 

39 ... os.environ["DAF_BUTLER_CONFIG_PATHS"] == "/somewhere" 

40 True 

41 

42 >>> "DAF_BUTLER_CONFIG_PATHS" != "/somewhere" 

43 True 

44 

45 Parameters 

46 ---------- 

47 environ : `dict` 

48 Key value pairs of environment variables to temporarily set. 

49 """ 

50 old_environ = dict(os.environ) 

51 os.environ.update(environ) 

52 try: 

53 yield 

54 finally: 

55 os.environ.clear() 

56 os.environ.update(old_environ) 

57 

58 

59class ExampleWithConfigFileReference: 

60 defaultConfigFile = "viacls.yaml" 

61 

62 

63class ExampleWithConfigFileReference2: 

64 defaultConfigFile = "viacls2.yaml" 

65 

66 

67class ConfigTest(ConfigSubset): 

68 component = "comp" 

69 requiredKeys = ("item1", "item2") 

70 defaultConfigFile = "testconfig.yaml" 

71 

72 

73class ConfigTestEmpty(ConfigTest): 

74 defaultConfigFile = "testconfig_empty.yaml" 

75 requiredKeys = () 

76 

77 

78class ConfigTestButlerDir(ConfigTest): 

79 defaultConfigFile = "testConfigs/testconfig.yaml" 

80 

81 

82class ConfigTestNoDefaults(ConfigTest): 

83 defaultConfigFile = None 

84 requiredKeys = () 

85 

86 

87class ConfigTestAbsPath(ConfigTest): 

88 defaultConfigFile = None 

89 requiredKeys = () 

90 

91 

92class ConfigTestCls(ConfigTest): 

93 defaultConfigFile = "withcls.yaml" 

94 

95 

96class ConfigTestCase(unittest.TestCase): 

97 """Tests of simple Config""" 

98 

99 def testBadConfig(self): 

100 for badArg in ([], # Bad argument 

101 __file__, # Bad file extension for existing file 

102 ): 

103 with self.assertRaises(RuntimeError): 

104 Config(badArg) 

105 for badArg in ("file.fits", # File that does not exist with bad extension 

106 "b/c/d/", # Directory that does not exist 

107 "file.yaml", # Good extension for missing file 

108 ): 

109 with self.assertRaises(FileNotFoundError): 

110 Config(badArg) 

111 

112 def testBasics(self): 

113 c = Config({"1": 2, "3": 4, "key3": 6, "dict": {"a": 1, "b": 2}}) 

114 pretty = c.ppprint() 

115 self.assertIn("key3", pretty) 

116 r = repr(c) 

117 self.assertIn("key3", r) 

118 regex = r"^Config\(\{.*\}\)$" 

119 self.assertRegex(r, regex) 

120 c2 = eval(r) 

121 self.assertIn("1", c) 

122 for n in c.names(): 

123 self.assertEqual(c2[n], c[n]) 

124 self.assertEqual(c, c2) 

125 s = str(c) 

126 self.assertIn("\n", s) 

127 self.assertNotRegex(s, regex) 

128 

129 self.assertCountEqual(c.keys(), ["1", "3", "key3", "dict"]) 

130 self.assertEqual(list(c), list(c.keys())) 

131 self.assertEqual(list(c.values()), [c[k] for k in c.keys()]) 

132 self.assertEqual(list(c.items()), [(k, c[k]) for k in c.keys()]) 

133 

134 newKeys = ("key4", ".dict.q", ("dict", "r"), "5") 

135 oldKeys = ("key3", ".dict.a", ("dict", "b"), "3") 

136 remainingKey = "1" 

137 

138 # Check get with existing key 

139 for k in oldKeys: 

140 self.assertEqual(c.get(k, "missing"), c[k]) 

141 

142 # Check get, pop with nonexistent key 

143 for k in newKeys: 

144 self.assertEqual(c.get(k, "missing"), "missing") 

145 self.assertEqual(c.pop(k, "missing"), "missing") 

146 

147 # Check setdefault with existing key 

148 for k in oldKeys: 

149 c.setdefault(k, 8) 

150 self.assertNotEqual(c[k], 8) 

151 

152 # Check setdefault with nonexistent key (mutates c, adding newKeys) 

153 for k in newKeys: 

154 c.setdefault(k, 8) 

155 self.assertEqual(c[k], 8) 

156 

157 # Check pop with existing key (mutates c, removing newKeys) 

158 for k in newKeys: 

159 v = c[k] 

160 self.assertEqual(c.pop(k, "missing"), v) 

161 

162 # Check deletion (mutates c, removing oldKeys) 

163 for k in ("key3", ".dict.a", ("dict", "b"), "3"): 

164 self.assertIn(k, c) 

165 del c[k] 

166 self.assertNotIn(k, c) 

167 

168 # Check that `dict` still exists, but is now empty (then remove 

169 # it, mutatic c) 

170 self.assertIn("dict", c) 

171 del c["dict"] 

172 

173 # Check popitem (mutates c, removing remainingKey) 

174 v = c[remainingKey] 

175 self.assertEqual(c.popitem(), (remainingKey, v)) 

176 

177 # Check that c is now empty 

178 self.assertFalse(c) 

179 

180 def testDict(self): 

181 """Test toDict()""" 

182 c1 = Config({"a": {"b": 1}, "c": 2}) 

183 self.assertIsInstance(c1["a"], Config) 

184 d1 = c1.toDict() 

185 self.assertIsInstance(d1["a"], dict) 

186 self.assertEqual(d1["a"], c1["a"]) 

187 

188 # Modifying one does not change the other 

189 d1["a"]["c"] = 2 

190 self.assertNotEqual(d1["a"], c1["a"]) 

191 

192 def assertSplit(self, answer, *args): 

193 """Helper function to compare string splitting""" 

194 for s in (answer, *args): 

195 split = Config._splitIntoKeys(s) 

196 self.assertEqual(split, answer) 

197 

198 def testSplitting(self): 

199 """Test of the internal splitting API.""" 

200 # Try lots of keys that will return the same answer 

201 answer = ["a", "b", "c", "d"] 

202 self.assertSplit(answer, ".a.b.c.d", ":a:b:c:d", "\ta\tb\tc\td", "\ra\rb\rc\rd") 

203 

204 answer = ["a", "calexp.wcs", "b"] 

205 self.assertSplit(answer, r".a.calexp\.wcs.b", ":a:calexp.wcs:b") 

206 

207 self.assertSplit(["a.b.c"]) 

208 self.assertSplit(["a", r"b\.c"], r"_a_b\.c") 

209 

210 # Escaping a backslash before a delimiter currently fails 

211 with self.assertRaises(ValueError): 

212 Config._splitIntoKeys(r".a.calexp\\.wcs.b") 

213 

214 # The next two fail because internally \r is magic when escaping 

215 # a delimiter. 

216 with self.assertRaises(ValueError): 

217 Config._splitIntoKeys("\ra\rcalexp\\\rwcs\rb") 

218 

219 with self.assertRaises(ValueError): 

220 Config._splitIntoKeys(".a.cal\rexp\\.wcs.b") 

221 

222 def testEscape(self): 

223 c = Config({"a": {"foo.bar": 1}, "b😂c": {"bar_baz": 2}}) 

224 self.assertEqual(c[r".a.foo\.bar"], 1) 

225 self.assertEqual(c[":a:foo.bar"], 1) 

226 self.assertEqual(c[".b😂c.bar_baz"], 2) 

227 self.assertEqual(c[r"😂b\😂c😂bar_baz"], 2) 

228 self.assertEqual(c[r"\a\foo.bar"], 1) 

229 self.assertEqual(c["\ra\rfoo.bar"], 1) 

230 with self.assertRaises(ValueError): 

231 c[".a.foo\\.bar\r"] 

232 

233 def testOperators(self): 

234 c1 = Config({"a": {"b": 1}, "c": 2}) 

235 c2 = c1.copy() 

236 self.assertEqual(c1, c2) 

237 self.assertIsInstance(c2, Config) 

238 c2[".a.b"] = 5 

239 self.assertNotEqual(c1, c2) 

240 

241 def testUpdate(self): 

242 c = Config({"a": {"b": 1}}) 

243 c.update({"a": {"c": 2}}) 

244 self.assertEqual(c[".a.b"], 1) 

245 self.assertEqual(c[".a.c"], 2) 

246 c.update({"a": {"d": [3, 4]}}) 

247 self.assertEqual(c[".a.d.0"], 3) 

248 c.update({"z": [5, 6, {"g": 2, "h": 3}]}) 

249 self.assertEqual(c[".z.1"], 6) 

250 

251 # This is detached from parent 

252 c2 = c[".z.2"] 

253 self.assertEqual(c2["g"], 2) 

254 c2.update({"h": 4, "j": 5}) 

255 self.assertEqual(c2["h"], 4) 

256 self.assertNotIn(".z.2.j", c) 

257 self.assertNotEqual(c[".z.2.h"], 4) 

258 

259 with self.assertRaises(RuntimeError): 

260 c.update([1, 2, 3]) 

261 

262 def testHierarchy(self): 

263 c = Config() 

264 

265 # Simple dict 

266 c["a"] = {"z": 52, "x": "string"} 

267 self.assertIn(".a.z", c) 

268 self.assertEqual(c[".a.x"], "string") 

269 

270 # Try different delimiters 

271 self.assertEqual(c["⇛a⇛z"], 52) 

272 self.assertEqual(c[("a", "z")], 52) 

273 self.assertEqual(c["a", "z"], 52) 

274 

275 c[".b.new.thing1"] = "thing1" 

276 c[".b.new.thing2"] = "thing2" 

277 c[".b.new.thing3.supp"] = "supplemental" 

278 self.assertEqual(c[".b.new.thing1"], "thing1") 

279 tmp = c[".b.new"] 

280 self.assertEqual(tmp["thing2"], "thing2") 

281 self.assertEqual(c[".b.new.thing3.supp"], "supplemental") 

282 

283 # Test that we can index into lists 

284 c[".a.b.c"] = [1, "7", 3, {"1": 4, "5": "Five"}, "hello"] 

285 self.assertIn(".a.b.c.3.5", c) 

286 self.assertNotIn(".a.b.c.10", c) 

287 self.assertNotIn(".a.b.c.10.d", c) 

288 self.assertEqual(c[".a.b.c.3.5"], "Five") 

289 # Is the value in the list? 

290 self.assertIn(".a.b.c.hello", c) 

291 self.assertNotIn(".a.b.c.hello.not", c) 

292 

293 # And assign to an element in the list 

294 self.assertEqual(c[".a.b.c.1"], "7") 

295 c[".a.b.c.1"] = 8 

296 self.assertEqual(c[".a.b.c.1"], 8) 

297 self.assertIsInstance(c[".a.b.c"], collections.abc.Sequence) 

298 

299 # Test we do get lists back from asArray 

300 a = c.asArray(".a.b.c") 

301 self.assertIsInstance(a, list) 

302 

303 # Is it the *same* list as in the config 

304 a.append("Sentinel") 

305 self.assertIn("Sentinel", c[".a.b.c"]) 

306 self.assertIn(".a.b.c.Sentinel", c) 

307 

308 # Test we always get a list 

309 for k in c.names(): 

310 a = c.asArray(k) 

311 self.assertIsInstance(a, list) 

312 

313 # Check we get the same top level keys 

314 self.assertEqual(set(c.names(topLevelOnly=True)), set(c._data.keys())) 

315 

316 # Check that we can iterate through items 

317 for k, v in c.items(): 

318 self.assertEqual(c[k], v) 

319 

320 # Check that lists still work even if assigned a dict 

321 c = Config({"cls": "lsst.daf.butler", 

322 "formatters": {"calexp.wcs": "{component}", 

323 "calexp": "{datasetType}"}, 

324 "datastores": [{"datastore": {"cls": "datastore1"}}, 

325 {"datastore": {"cls": "datastore2"}}]}) 

326 c[".datastores.1.datastore"] = {"cls": "datastore2modified"} 

327 self.assertEqual(c[".datastores.0.datastore.cls"], "datastore1") 

328 self.assertEqual(c[".datastores.1.datastore.cls"], "datastore2modified") 

329 self.assertIsInstance(c["datastores"], collections.abc.Sequence) 

330 

331 # Test that we can get all the listed names. 

332 # and also that they are marked as "in" the Config 

333 # Try delimited names and tuples 

334 for n in itertools.chain(c.names(), c.nameTuples()): 

335 val = c[n] 

336 self.assertIsNotNone(val) 

337 self.assertIn(n, c) 

338 

339 names = c.names() 

340 nameTuples = c.nameTuples() 

341 self.assertEqual(len(names), len(nameTuples)) 

342 self.assertEqual(len(names), 11) 

343 self.assertEqual(len(nameTuples), 11) 

344 

345 # Test that delimiter escaping works 

346 names = c.names(delimiter=".") 

347 for n in names: 

348 self.assertIn(n, c) 

349 self.assertIn(".formatters.calexp\\.wcs", names) 

350 

351 # Use a name that includes the internal default delimiter 

352 # to test automatic adjustment of delimiter 

353 strangeKey = f"calexp{c._D}wcs" 

354 c["formatters", strangeKey] = "dynamic" 

355 names = c.names() 

356 self.assertIn(strangeKey, "-".join(names)) 

357 self.assertFalse(names[0].startswith(c._D)) 

358 for n in names: 

359 self.assertIn(n, c) 

360 

361 top = c.nameTuples(topLevelOnly=True) 

362 self.assertIsInstance(top[0], tuple) 

363 

364 # Investigate a possible delimeter in a key 

365 c = Config({"formatters": {"calexp.wcs": 2, "calexp": 3}}) 

366 self.assertEqual(c[":formatters:calexp.wcs"], 2) 

367 self.assertEqual(c[":formatters:calexp"], 3) 

368 for k, v in c["formatters"].items(): 

369 self.assertEqual(c["formatters", k], v) 

370 

371 # Check internal delimiter inheritance 

372 c._D = "." 

373 c2 = c["formatters"] 

374 self.assertEqual(c._D, c2._D) # Check that the child inherits 

375 self.assertNotEqual(c2._D, Config._D) 

376 

377 def testSerializedString(self): 

378 """Test that we can create configs from strings""" 

379 

380 serialized = { 

381 "yaml": """ 

382testing: hello 

383formatters: 

384 calexp: 3""", 

385 "json": '{"testing": "hello", "formatters": {"calexp": 3}}' 

386 } 

387 

388 for format, string in serialized.items(): 

389 c = Config.fromString(string, format=format) 

390 self.assertEqual(c["formatters", "calexp"], 3) 

391 self.assertEqual(c["testing"], "hello") 

392 

393 with self.assertRaises(ValueError): 

394 Config.fromString("", format="unknown") 

395 

396 with self.assertRaises(ValueError): 

397 Config.fromString(serialized["yaml"], format="json") 

398 

399 # This JSON can be parsed by YAML parser 

400 j = Config.fromString(serialized["json"]) 

401 y = Config.fromString(serialized["yaml"]) 

402 self.assertEqual(j["formatters", "calexp"], 3) 

403 self.assertEqual(j.toDict(), y.toDict()) 

404 

405 # Round trip JSON -> Config -> YAML -> Config -> JSON -> Config 

406 c1 = Config.fromString(serialized["json"], format="json") 

407 yaml = c1.dump(format="yaml") 

408 c2 = Config.fromString(yaml, format="yaml") 

409 json = c2.dump(format="json") 

410 c3 = Config.fromString(json, format="json") 

411 self.assertEqual(c3.toDict(), c1.toDict()) 

412 

413 

414class ConfigSubsetTestCase(unittest.TestCase): 

415 """Tests for ConfigSubset 

416 """ 

417 

418 def setUp(self): 

419 self.testDir = os.path.abspath(os.path.dirname(__file__)) 

420 self.configDir = os.path.join(self.testDir, "config", "testConfigs") 

421 self.configDir2 = os.path.join(self.testDir, "config", "testConfigs", "test2") 

422 self.configDir3 = os.path.join(self.testDir, "config", "testConfigs", "test3") 

423 

424 def testEmpty(self): 

425 """Ensure that we can read an empty file.""" 

426 c = ConfigTestEmpty(searchPaths=(self.configDir,)) 

427 self.assertIsInstance(c, ConfigSubset) 

428 

429 def testDefaults(self): 

430 """Read of defaults""" 

431 

432 # Supply the search path explicitly 

433 c = ConfigTest(searchPaths=(self.configDir,)) 

434 self.assertIsInstance(c, ConfigSubset) 

435 self.assertIn("item3", c) 

436 self.assertEqual(c["item3"], 3) 

437 

438 # Use environment 

439 with modified_environment(DAF_BUTLER_CONFIG_PATH=self.configDir): 

440 c = ConfigTest() 

441 self.assertIsInstance(c, ConfigSubset) 

442 self.assertEqual(c["item3"], 3) 

443 

444 # No default so this should fail 

445 with self.assertRaises(KeyError): 

446 c = ConfigTest() 

447 

448 def testExternalOverride(self): 

449 """Ensure that external values win""" 

450 c = ConfigTest({"item3": "newval"}, searchPaths=(self.configDir,)) 

451 self.assertIn("item3", c) 

452 self.assertEqual(c["item3"], "newval") 

453 

454 def testSearchPaths(self): 

455 """Two search paths""" 

456 c = ConfigTest(searchPaths=(self.configDir2, self.configDir)) 

457 self.assertIsInstance(c, ConfigSubset) 

458 self.assertIn("item3", c) 

459 self.assertEqual(c["item3"], "override") 

460 self.assertEqual(c["item4"], "new") 

461 

462 c = ConfigTest(searchPaths=(self.configDir, self.configDir2)) 

463 self.assertIsInstance(c, ConfigSubset) 

464 self.assertIn("item3", c) 

465 self.assertEqual(c["item3"], 3) 

466 self.assertEqual(c["item4"], "new") 

467 

468 def testExternalHierarchy(self): 

469 """Test that we can provide external config parameters in hierarchy""" 

470 c = ConfigTest({"comp": {"item1": 6, "item2": "a", "a": "b", 

471 "item3": 7}, "item4": 8}) 

472 self.assertIn("a", c) 

473 self.assertEqual(c["a"], "b") 

474 self.assertNotIn("item4", c) 

475 

476 def testNoDefaults(self): 

477 """Ensure that defaults can be turned off.""" 

478 

479 # Mandatory keys but no defaults 

480 c = ConfigTest({"item1": "a", "item2": "b", "item6": 6}) 

481 self.assertEqual(len(c.filesRead), 0) 

482 self.assertIn("item1", c) 

483 self.assertEqual(c["item6"], 6) 

484 

485 c = ConfigTestNoDefaults() 

486 self.assertEqual(len(c.filesRead), 0) 

487 

488 def testAbsPath(self): 

489 """Read default config from an absolute path""" 

490 # Force the path to be absolute in the class 

491 ConfigTestAbsPath.defaultConfigFile = os.path.join(self.configDir, "abspath.yaml") 

492 c = ConfigTestAbsPath() 

493 self.assertEqual(c["item11"], "eleventh") 

494 self.assertEqual(len(c.filesRead), 1) 

495 

496 # Now specify the normal config file with an absolute path 

497 ConfigTestAbsPath.defaultConfigFile = os.path.join(self.configDir, ConfigTest.defaultConfigFile) 

498 c = ConfigTestAbsPath() 

499 self.assertEqual(c["item11"], 11) 

500 self.assertEqual(len(c.filesRead), 1) 

501 

502 # and a search path that will also include the file 

503 c = ConfigTestAbsPath(searchPaths=(self.configDir, self.configDir2,)) 

504 self.assertEqual(c["item11"], 11) 

505 self.assertEqual(len(c.filesRead), 1) 

506 

507 # Same as above but this time with relative path and two search paths 

508 # to ensure the count changes 

509 ConfigTestAbsPath.defaultConfigFile = ConfigTest.defaultConfigFile 

510 c = ConfigTestAbsPath(searchPaths=(self.configDir, self.configDir2,)) 

511 self.assertEqual(len(c.filesRead), 2) 

512 

513 # Reset the class 

514 ConfigTestAbsPath.defaultConfigFile = None 

515 

516 def testClassDerived(self): 

517 """Read config specified in class determined from config""" 

518 c = ConfigTestCls(searchPaths=(self.configDir,)) 

519 self.assertEqual(c["item50"], 50) 

520 self.assertEqual(c["help"], "derived") 

521 

522 # Same thing but additional search path 

523 c = ConfigTestCls(searchPaths=(self.configDir, self.configDir2)) 

524 self.assertEqual(c["item50"], 50) 

525 self.assertEqual(c["help"], "derived") 

526 self.assertEqual(c["help2"], "second") 

527 

528 # Same thing but reverse the two paths 

529 c = ConfigTestCls(searchPaths=(self.configDir2, self.configDir)) 

530 self.assertEqual(c["item50"], 500) 

531 self.assertEqual(c["help"], "class") 

532 self.assertEqual(c["help2"], "second") 

533 self.assertEqual(c["help3"], "third") 

534 

535 def testInclude(self): 

536 """Read a config that has an include directive""" 

537 c = Config(os.path.join(self.configDir, "testinclude.yaml")) 

538 self.assertEqual(c[".comp1.item1"], 58) 

539 self.assertEqual(c[".comp2.comp.item1"], 1) 

540 self.assertEqual(c[".comp3.1.comp.item1"], "posix") 

541 self.assertEqual(c[".comp4.0.comp.item1"], "posix") 

542 self.assertEqual(c[".comp4.1.comp.item1"], 1) 

543 self.assertEqual(c[".comp5.comp6.comp.item1"], "posix") 

544 

545 # Test a specific name and then test that all 

546 # returned names are "in" the config. 

547 names = c.names() 

548 self.assertIn(c._D.join(("", "comp3", "1", "comp", "item1")), names) 

549 for n in names: 

550 self.assertIn(n, c) 

551 

552 # Test that override delimiter works 

553 delimiter = "-" 

554 names = c.names(delimiter=delimiter) 

555 self.assertIn(delimiter.join(("", "comp3", "1", "comp", "item1")), names) 

556 

557 def testStringInclude(self): 

558 """Using include directives in strings""" 

559 

560 # See if include works for absolute path 

561 c = Config.fromYaml(f"something: !include {os.path.join(self.configDir, 'testconfig.yaml')}") 

562 self.assertEqual(c["something", "comp", "item3"], 3) 

563 

564 with self.assertRaises(FileNotFoundError) as cm: 

565 Config.fromYaml("something: !include /not/here.yaml") 

566 # Test that it really was trying to open the absolute path 

567 self.assertIn("'/not/here.yaml'", str(cm.exception)) 

568 

569 def testIncludeConfigs(self): 

570 """Test the special includeConfigs key for pulling in additional 

571 files.""" 

572 c = Config(os.path.join(self.configDir, "configIncludes.yaml")) 

573 self.assertEqual(c["comp", "item2"], "hello") 

574 self.assertEqual(c["comp", "item50"], 5000) 

575 self.assertEqual(c["comp", "item1"], "first") 

576 self.assertEqual(c["comp", "item10"], "tenth") 

577 self.assertEqual(c["comp", "item11"], "eleventh") 

578 self.assertEqual(c["unrelated"], 1) 

579 self.assertEqual(c["addon", "comp", "item1"], "posix") 

580 self.assertEqual(c["addon", "comp", "item11"], -1) 

581 self.assertEqual(c["addon", "comp", "item50"], 500) 

582 

583 c = Config(os.path.join(self.configDir, "configIncludes.json")) 

584 self.assertEqual(c["comp", "item2"], "hello") 

585 self.assertEqual(c["comp", "item50"], 5000) 

586 self.assertEqual(c["comp", "item1"], "first") 

587 self.assertEqual(c["comp", "item10"], "tenth") 

588 self.assertEqual(c["comp", "item11"], "eleventh") 

589 self.assertEqual(c["unrelated"], 1) 

590 self.assertEqual(c["addon", "comp", "item1"], "posix") 

591 self.assertEqual(c["addon", "comp", "item11"], -1) 

592 self.assertEqual(c["addon", "comp", "item50"], 500) 

593 

594 # Now test with an environment variable in includeConfigs 

595 with modified_environment(SPECIAL_BUTLER_DIR=self.configDir3): 

596 c = Config(os.path.join(self.configDir, "configIncludesEnv.yaml")) 

597 self.assertEqual(c["comp", "item2"], "hello") 

598 self.assertEqual(c["comp", "item50"], 5000) 

599 self.assertEqual(c["comp", "item1"], "first") 

600 self.assertEqual(c["comp", "item10"], "tenth") 

601 self.assertEqual(c["comp", "item11"], "eleventh") 

602 self.assertEqual(c["unrelated"], 1) 

603 self.assertEqual(c["addon", "comp", "item1"], "envvar") 

604 self.assertEqual(c["addon", "comp", "item11"], -1) 

605 self.assertEqual(c["addon", "comp", "item50"], 501) 

606 

607 # This will fail 

608 with modified_environment(SPECIAL_BUTLER_DIR=self.configDir2): 

609 with self.assertRaises(FileNotFoundError): 

610 Config(os.path.join(self.configDir, "configIncludesEnv.yaml")) 

611 

612 def testResource(self): 

613 c = Config("resource://lsst.daf.butler/configs/datastore.yaml") 

614 self.assertIn("datastore", c) 

615 

616 # Test that we can include a resource URI 

617 yaml = """ 

618toplevel: true 

619resource: !include resource://lsst.daf.butler/configs/datastore.yaml 

620""" 

621 c = Config.fromYaml(yaml) 

622 self.assertIn(("resource", "datastore", "cls"), c) 

623 

624 # Test that we can include a resource URI with includeConfigs 

625 yaml = """ 

626toplevel: true 

627resource: 

628 includeConfigs: resource://lsst.daf.butler/configs/datastore.yaml 

629""" 

630 c = Config.fromYaml(yaml) 

631 self.assertIn(("resource", "datastore", "cls"), c) 

632 

633 

634class FileWriteConfigTestCase(unittest.TestCase): 

635 

636 def setUp(self): 

637 self.tmpdir = tempfile.mkdtemp() 

638 

639 def tearDown(self): 

640 if os.path.exists(self.tmpdir): 

641 shutil.rmtree(self.tmpdir, ignore_errors=True) 

642 

643 def testDump(self): 

644 """Test that we can write and read a configuration.""" 

645 

646 c = Config({"1": 2, "3": 4, "key3": 6, "dict": {"a": 1, "b": 2}}) 

647 

648 for format in ("yaml", "json"): 

649 outpath = os.path.join(self.tmpdir, f"test.{format}") 

650 c.dumpToUri(outpath) 

651 

652 c2 = Config(outpath) 

653 self.assertEqual(c2, c) 

654 

655 c.dumpToUri(outpath, overwrite=True) 

656 with self.assertRaises(FileExistsError): 

657 c.dumpToUri(outpath, overwrite=False) 

658 

659 

660if __name__ == "__main__": 660 ↛ 661line 660 didn't jump to line 661, because the condition on line 660 was never true

661 unittest.main()