def test_with_default(self): c = ConfigObj() c['a'] = 3 self.assertEqual(c.pop('a'), 3) self.assertEqual(c.pop('b', 3), 3) self.assertRaises(KeyError, c.pop, 'c')
def test_validate_extra_values(self): conf = ConfigObj(inipath, configspec=specpath) conf.validate(Validator(), preserve_errors=True) self.assertEqual(conf.extra_values, ['extra', 'extra-section']) self.assertEqual(conf['section'].extra_values, ['extra-sub-section']) self.assertEqual(conf['section']['sub-section'].extra_values, ['extra'])
def test_get_extra_values(self): conf = ConfigObj(inipath, configspec=specpath) conf.validate(Validator(), preserve_errors=True) extra_values = get_extra_values(conf) expected = sorted([ ((), 'extra'), ((), 'extra-section'), (('section', 'sub-section'), 'extra'), (('section',), 'extra-sub-section'), ]) self.assertEqual(sorted(extra_values), expected)
def test_validate_preserve_errors(self): conf = ConfigObj(inipath, configspec=specpath) validator = Validator() result = conf.validate(validator, preserve_errors=True) self.assertFalse(result['value']) self.assertFalse(result['missing-section']) section = result['section'] self.assertFalse(section['value']) self.assertFalse(section['sub-section']['value']) self.assertFalse(section['missing-subsection'])
def test_interoplation_repr(self): c = ConfigObj(['foo = $bar'], interpolation='Template') c['baz'] = {} c['baz']['spam'] = '%(bar)s' # This raises a MissingInterpolationOption exception in 4.7.1 and earlier repr(c)
def test_options_deprecation(self): with catch_warnings(record=True) as log: ConfigObj(options={}) # unpack the only member of log # skip the test for Python > 2.6 if log: warning, = log self.assertEqual(warning.category, DeprecationWarning)
def test_order_preserved(self): c = ConfigObj() c['a'] = 1 c['b'] = 2 c['c'] = 3 c['section'] = {} c['section']['a'] = 1 c['section']['b'] = 2 c['section']['c'] = 3 c['section']['section'] = {} c['section']['section2'] = {} c['section']['section3'] = {} c['section2'] = {} c['section3'] = {} c2 = ConfigObj(c) self.assertEqual(c2.scalars, ['a', 'b', 'c']) self.assertEqual(c2.sections, ['section', 'section2', 'section3']) self.assertEqual(c2['section'].scalars, ['a', 'b', 'c']) self.assertEqual(c2['section'].sections, ['section', 'section2', 'section3']) self.assertFalse(c['section'] is c2['section']) self.assertFalse(c['section']['section'] is c2['section']['section'])
def test_interpolation_with_section_names(self): cfg = """ item1 = 1234 [section] [[item1]] foo='bar' [[DEFAULT]] [[[item1]]] why = would you do this? [[other-subsection]] item2 = '$item1'""".splitlines() c = ConfigObj(cfg, interpolation='Template') # This raises an exception in 4.7.1 and earlier due to the section # being found as the interpolation value repr(c)
def test_set_timezone_sles(self): cfg = { 'timezone': 'Tatooine/Bestine', } # Create a dummy timezone file dummy_contents = '0123456789abcdefgh' zone_file = "/".join([self.new_root, cfg['timezone']]) utils.write_file(zone_file, dummy_contents) clock_conf = self.new_root + "/etc/sysconfig/clock" local_tz = self.new_root + "/etc/localtime" utils.set_timezone(tz=cfg['timezone'], tz_zone_dir=self.new_root, clock_conf_fn=clock_conf, tz_local_fn=local_tz) contents = utils.load_file(clock_conf) n_cfg = ConfigObj(StringIO(contents)) self.assertEquals({'ZONE': cfg['timezone']}, dict(n_cfg)) contents = utils.load_file(local_tz) self.assertEquals(dummy_contents, contents.strip())
def test_list_interpolation_with_pop(self): c = ConfigObj() c['a'] = [] c['a'].append('%(b)s') c['b'] = 'bar' self.assertEqual(c.pop('a'), ['bar'])
def test_validate_no_valid_entries(self): conf = ConfigObj(inipath, configspec=specpath) validator = Validator() result = conf.validate(validator) self.assertFalse(result)
def test_list_members(self): c = ConfigObj() c['a'] = [] c['a'].append('foo') self.assertEqual(c['a'], ['foo'])