def test_guess_format(self): with cfgs.App('test', format='yaml').data.open('special') as fp: f = fp.contents f['foo'] = 'bar' f['baz'] = [2, 3, 4] del f['foo'] f.update(zip='zap') with cfgs.App('test', format='yaml').data.open('special') as f: self.assertEqual(f.as_dict(), {'baz': [2, 3, 4], 'zip': 'zap'}) self.assertNotIn('"', open(f.filename).read()) self.assertIn('zip', open(f.filename).read())
def test_read_write(self): with cfgs.App('test').config.open() as fp: f = fp.contents f['foo'] = 'bar' f['baz'] = [2, 3, 4] del f['foo'] f.update(zip='zap') with cfgs.App('test').config.open() as f: self.assertEqual(f.contents['zip'], 'zap') self.assertEqual(f.as_dict(), {'baz': [2, 3, 4], 'zip': 'zap'}) f.clear() self.assertEqual(f.as_dict(), {})
def test_configfile(self): pr = cfgs.App('test', format='configparser') with pr.config.open() as fp: f = fp.contents f['foo'] = {'a': 1, 'b': 2} f['bar'] = {} pr = cfgs.App('test', format='configparser') with pr.config.open() as f: actual = f.as_dict() expected = { 'DEFAULT': {}, 'foo': {'a': '1', 'b': '2'}, 'bar': {}, } self.assertEqual(expected, actual) f.clear() self.assertEqual(f.as_dict(), {'DEFAULT': {}})
def compute_sizes(roots): def default(o): if isinstance(o, set): return list(o) raise TypeError write_kwds = {'default': default, 'indent': 4, 'sort_keys': True} app = cfgs.App('swirly-dedupe', write_kwds=write_kwds) for root in roots: with app.data.open('%s/size.json' % root) as f: fingerprint('size', files.walk(root), f.contents)
def test_simple(self): with cfgs.App('test').config.open() as fp: f = fp.contents f['foo'] = 'bar' f['baz'] = [2, 3, 4] del f['foo'] f.update(zip='zap') expected = {'baz': [2, 3, 4], 'zip': 'zap'} actual = json.load(open('/usr/fake/.config/test/test.json')) self.assertEqual(actual, expected)
def test_data(self): files = ( '/usr/share/wombat.json', '/etc/xdg/test/wombat.json', '/wombat.json', ) for f in files: self.fs.create_file(f) cfg = cfgs.App('test') actual = list(cfg.config.all_files('wombat.json')) expected = ['/etc/xdg/test/wombat.json'] self.assertEqual(actual, expected)
def test_kwds(self): def object_hook(x): print('object_hook', x) return set(x) if isinstance(x, list) else x read_kwds = {'object_hook': object_hook} write_kwds = {'sort_keys': True, 'indent': 1} app = cfgs.App('test', read_kwds=read_kwds, write_kwds=write_kwds) with app.config.open() as fp: f = fp.contents f['foo'] = 'bar' f['baz'] = [2, 3, 4] del f['foo'] f.update(zip='zap') filename = fp.filename c = open(filename).read().count('\n') self.assertEqual(c, 7)
def _create_cache(self, cache_size=0, use_size_guess=True): p = cfgs.App('test') cache = p.cache.directory(cache_size=cache_size) for file, contents in self.FILE_CONTENTS: size_guess = len(contents) if use_size_guess else 0 with cache.open(file, size_guess=size_guess) as f: f.write(contents) for file, contents in self.FILE_CONTENTS: self.assertEqual(cache.open(file).read(), contents) self.assertEqual(cache.dirname, '/usr/fake/.cache/test/cache') self.assertEqual(set(self.fs.listdir(cache.dirname)), self.EXPECTED) with self.assertRaises(ValueError): cache.open('foo/bar') size_guess = 7 if use_size_guess else 0 with cache.open('seven', size_guess=size_guess) as f: f.write('7777777') return cache, self.fs.listdir(cache.dirname)
def test_error(self): with self.assertRaises(ValueError) as cm: cfgs.App('/\\?:|*<"%') self.assertIn('"%*/:<?\\|', cm.exception.args[0])
def test_bad_format(self): with self.assertRaises(ValueError): cfgs.App('test', format='wombat')