def test_construct(): cfg = Config() assert cfg.filename is None assert cfg.text == '' assert len(cfg) == 0 assert cfg._cfg_dict == {} with pytest.raises(TypeError): Config([0, 1]) cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test') format_text = json.dumps(cfg_dict, indent=2) for filename in ['a.py', 'b.json', 'c.yaml']: cfg_file = osp.join(osp.dirname(__file__), 'data/config', filename) cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() if sys.version_info >= (3, 6): assert cfg.dump() == format_text else: loaded = json.loads(cfg.dump()) assert set(loaded.keys()) == set(cfg_dict)
def test_construct(): cfg = Config() assert cfg.filename is None assert cfg.text == '' assert len(cfg) == 0 assert cfg._cfg_dict == {} with pytest.raises(TypeError): Config([0, 1]) cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test') # test a.py cfg_file = osp.join(osp.dirname(__file__), 'data/config/a.py') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'a.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test b.json cfg_file = osp.join(osp.dirname(__file__), 'data/config/b.json') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'b.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test c.yaml cfg_file = osp.join(osp.dirname(__file__), 'data/config/c.yaml') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'c.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file)
def test_construct(): cfg = Config() assert cfg.filename is None assert cfg.text == '' assert len(cfg) == 0 assert cfg._cfg_dict == {} with pytest.raises(TypeError): Config([0, 1]) cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test') # test a.py cfg_file = osp.join(osp.dirname(__file__), 'data/config/a.py') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'a.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test b.json cfg_file = osp.join(osp.dirname(__file__), 'data/config/b.json') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'b.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test c.yaml cfg_file = osp.join(osp.dirname(__file__), 'data/config/c.yaml') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'c.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test h.py cfg_file = osp.join(osp.dirname(__file__), 'data/config/h.py') cfg_dict = dict(item1='h.py', item2=f'{osp.dirname(__file__)}/data/config', item3='abc_h') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'h.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] assert Config.fromfile(dump_file)['item2'] == cfg_dict['item2'] assert Config.fromfile(dump_file)['item3'] == cfg_dict['item3'] # test no use_predefined_variable cfg_dict = dict(item1='{{fileBasename}}', item2='{{ fileDirname}}', item3='abc_{{ fileBasenameNoExtension }}') assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == cfg_dict['item1'] assert Config.fromfile(cfg_file, False)['item2'] == cfg_dict['item2'] assert Config.fromfile(cfg_file, False)['item3'] == cfg_dict['item3'] # test p.yaml cfg_file = osp.join(osp.dirname(__file__), 'data/config/p.yaml') cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'p.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}' # test o.json cfg_file = osp.join(osp.dirname(__file__), 'data/config/o.json') cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'o.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}'
def test_construct(): cfg = Config() assert cfg.filename is None assert cfg.text == '' assert len(cfg) == 0 assert cfg._cfg_dict == {} with pytest.raises(TypeError): Config([0, 1]) cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test') # test a.py cfg_file = osp.join(data_path, 'config/a.py') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'a.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test b.json cfg_file = osp.join(data_path, 'config/b.json') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'b.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test c.yaml cfg_file = osp.join(data_path, 'config/c.yaml') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'c.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test h.py cfg_file = osp.join(data_path, 'config/h.py') path = osp.join(osp.dirname(__file__), 'data', 'config') # the value of osp.dirname(__file__) may be `D:\a\xxx` in windows # environment. When dumping the cfg_dict to file, `D:\a\xxx` will be # converted to `D:\x07\xxx` and it will cause unexpected result when # checking whether `D:\a\xxx` equals to `D:\x07\xxx`. Therefore, we forcely # convert a string representation of the path with forward slashes (/) path = Path(path).as_posix() cfg_dict = dict(item1='h.py', item2=path, item3='abc_h') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'h.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] assert Config.fromfile(dump_file)['item2'] == cfg_dict['item2'] assert Config.fromfile(dump_file)['item3'] == cfg_dict['item3'] # test no use_predefined_variable cfg_dict = dict( item1='{{fileBasename}}', item2='{{ fileDirname}}', item3='abc_{{ fileBasenameNoExtension }}') assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == cfg_dict['item1'] assert Config.fromfile(cfg_file, False)['item2'] == cfg_dict['item2'] assert Config.fromfile(cfg_file, False)['item3'] == cfg_dict['item3'] # test p.yaml cfg_file = osp.join(data_path, 'config/p.yaml') cfg_dict = dict(item1=osp.join(osp.dirname(__file__), 'data', 'config')) cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'p.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}' # test o.json cfg_file = osp.join(data_path, 'config/o.json') cfg_dict = dict(item1=osp.join(osp.dirname(__file__), 'data', 'config')) cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'o.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}'