def test_config_file(): from astropy.config.configuration import get_config, reload_config apycfg = get_config('astropy') assert apycfg.filename.endswith('astropy.cfg') cfgsec = get_config('astropy.config') assert cfgsec.depth == 1 assert cfgsec.name == 'config' assert cfgsec.parent.filename.endswith('astropy.cfg') reload_config('astropy')
def test_configitem_options(tmpdir): from astropy.config.configuration import ConfigNamespace, ConfigItem, get_config cio = ConfigItem(['op1', 'op2', 'op3']) class Conf(ConfigNamespace): tstnmo = cio conf = Conf() sec = get_config(cio.module) assert isinstance(cio(), str) assert cio() == 'op1' assert sec['tstnmo'] == 'op1' cio.set('op2') with pytest.raises(TypeError): cio.set('op5') assert sec['tstnmo'] == 'op2' # now try saving apycfg = sec while apycfg.parent is not apycfg: apycfg = apycfg.parent f = tmpdir.join('astropy.cfg') with open(f.strpath, 'wb') as fd: apycfg.write(fd) with open(f.strpath, 'r', encoding='utf-8') as fd: lns = [x.strip() for x in f.readlines()] assert 'tstnmo = op2' in lns
def test_configitem(): from astropy.config.configuration import ConfigNamespace, ConfigItem, get_config ci = ConfigItem(34, 'this is a Description') class Conf(ConfigNamespace): tstnm = ci conf = Conf() assert ci.module == 'astropy.config.tests.test_configs' assert ci() == 34 assert ci.description == 'this is a Description' assert conf.tstnm == 34 sec = get_config(ci.module) assert sec['tstnm'] == 34 ci.description = 'updated Descr' ci.set(32) assert ci() == 32 # It's useful to go back to the default to allow other test functions to # call this one and still be in the default configuration. ci.description = 'this is a Description' ci.set(34) assert ci() == 34
def test_configitem_unicode(tmpdir): from astropy.config.configuration import ConfigNamespace, ConfigItem, get_config cio = ConfigItem('ასტრონომიის') class Conf(ConfigNamespace): tstunicode = cio conf = Conf() sec = get_config(cio.module) assert isinstance(cio(), str) assert cio() == 'ასტრონომიის' assert sec['tstunicode'] == 'ასტრონომიის'
def test_configitem(): from astropy.config.configuration import ConfigNamespace, ConfigItem, get_config ci = ConfigItem(34, 'this is a Description') class Conf(ConfigNamespace): tstnm = ci conf = Conf() assert ci.module == 'astropy.config.tests.test_configs' assert ci() == 34 assert ci.description == 'this is a Description' assert conf.tstnm == 34 sec = get_config(ci.module) assert sec['tstnm'] == 34 ci.description = 'updated Descr' ci.set(32) assert ci() == 32 # It's useful to go back to the default to allow other test functions to # call this one and still be in the default configuration. ci.description = 'this is a Description' ci.set(34) assert ci() == 34 # Test iterator for one-item namespace result = [x for x in conf] assert result == ['tstnm'] result = [x for x in conf.keys()] assert result == ['tstnm'] result = [x for x in conf.values()] assert result == [ci] result = [x for x in conf.items()] assert result == [('tstnm', ci)]
def test_config_file(): from astropy.config.configuration import get_config, reload_config apycfg = get_config('astropy') assert apycfg.filename.endswith('astropy.cfg') cfgsec = get_config('astropy.config') assert cfgsec.depth == 1 assert cfgsec.name == 'config' assert cfgsec.parent.filename.endswith('astropy.cfg') # try with a different package name, still inside astropy config dir: testcfg = get_config('testpkg', rootname='astropy') parts = os.path.normpath(testcfg.filename).split(os.sep) assert '.astropy' in parts or 'astropy' in parts assert parts[-1] == 'testpkg.cfg' configuration._cfgobjs['testpkg'] = None # HACK # try with a different package name, no specified root name (should # default to astropy): testcfg = get_config('testpkg') parts = os.path.normpath(testcfg.filename).split(os.sep) assert '.astropy' in parts or 'astropy' in parts assert parts[-1] == 'testpkg.cfg' configuration._cfgobjs['testpkg'] = None # HACK # try with a different package name, specified root name: testcfg = get_config('testpkg', rootname='testpkg') parts = os.path.normpath(testcfg.filename).split(os.sep) assert '.testpkg' in parts or 'testpkg' in parts assert parts[-1] == 'testpkg.cfg' configuration._cfgobjs['testpkg'] = None # HACK # try with a subpackage with specified root name: testcfg_sec = get_config('testpkg.somemodule', rootname='testpkg') parts = os.path.normpath(testcfg_sec.parent.filename).split(os.sep) assert '.testpkg' in parts or 'testpkg' in parts assert parts[-1] == 'testpkg.cfg' configuration._cfgobjs['testpkg'] = None # HACK reload_config('astropy')