def test_get_pkg_config_handle_versions(tmp_dir): pkg_cfg = dict(default_cfg) pkg_cfg["_pkglts"]["version"] = 0 json.dump(pkg_cfg, open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w')) cfg = get_pkg_config(tmp_dir) assert cfg["_pkglts"]['version'] == current_pkg_cfg_version
def test_regenerate_fail_if_permanent_section_ids_have_been_modified(): env = get_pkg_config(tmp_dir) with open(init_file, 'a') as f: f.write("\n# {# pkglts, test\na = 1\n# #}\n") assert_raises(KeyError, lambda: regenerate_package(env, tmp_dir, overwrite=True))
def test_get_pkg_config_handle_versions(tmp_dir): pkg_cfg = dict(DEFAULT_CFG) pkg_cfg["_pkglts"]["version"] = 0 json.dump(pkg_cfg, open(tmp_dir / pkglts_dir / pkg_cfg_file, 'w')) cfg = get_pkg_config(tmp_dir) assert cfg["_pkglts"]['version'] == CURRENT_PKG_CFG_VERSION
def test_get_pkg_config_handle_versions(): cfg = dict(default_cfg) cfg["_pkglts"]["version"] = 0 with open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w') as f: json.dump(cfg, f) env = get_pkg_config(tmp_dir) assert env.globals["_pkglts"].version == current_pkg_cfg_version
def test_regenerate_option_fails_if_option_not_available(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) regenerate_option(cfg, 'base', tmp_dir) with pytest.raises(KeyError): regenerate_option(cfg, "toto", tmp_dir)
def test_get_pkg_config_read_cfg(tmp_dir): pkg_cfg = dict(DEFAULT_CFG) pkg_cfg['base'] = dict(pkgname="toto", namespace="nm", url=None, authors=[("moi", "moi@aussi")]) json.dump(pkg_cfg, open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w')) cfg = get_pkg_config(tmp_dir) assert 'base' in cfg
def test_regenerate_handle_global_overwrite(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) addendum(init_file) regenerate_package(cfg, tmp_dir, overwrite=True) assert "modified" not in init_file.read_text()
def test_regenerate_fail_if_permanent_section_ids_have_been_modified(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) with open(init_file, 'a') as f: f.write("\n# {# pkglts, test\na = 1\n# #}\n") with pytest.raises(KeyError): regenerate_package(cfg, tmp_dir, overwrite=True)
def test_get_pkg_config_read_cfg(): cfg = dict(default_cfg) cfg['base'] = dict(pkgname="toto", namespace="nm", url=None, authors=[("moi", "moi@aussi")]) with open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w') as f: json.dump(cfg, f) env = get_pkg_config(tmp_dir) assert 'base' in env.globals
def test_regenerate_handle_conflicts_keep(tmp_pths, mocker): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) init_file.write_text("modified") mocker.patch('pkglts.manage.get_user_permission', return_value=False) regenerate_package(cfg, tmp_dir) assert init_file.read_text() == "modified"
def test_regenerate_remove_tpl_files_do_not_generate_conflicts(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) regenerate_package(cfg, tmp_dir) init_file.unlink() regenerate_package(cfg, tmp_dir) assert init_file.exists()
def test_regenerate_handle_global_overwrite(): env = get_pkg_config(tmp_dir) addendum() regenerate_package(env, tmp_dir, overwrite=True) with open(init_file, 'r') as f: txt = f.read() assert "modified" not in txt
def test_regenerate_handle_conflicts_overwrite(tmp_pths, mocker): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) addendum(init_file) mocker.patch('pkglts.manage.get_user_permission', return_value=True) regenerate_package(cfg, tmp_dir) assert "modified" not in init_file.read_text()
def test_pkg_cfg_read_write_maintains_templates(tmp_dir): pkg_cfg = dict(DEFAULT_CFG) pkg_cfg['base'] = dict(pkgname="toto", namespace="nm", url=None, authors=[("moi", "moi@aussi")]) pkg_cfg['license'] = dict(name="CeCILL-C", organization="org", project="{{ base.pkgname }}", year="2015") json.dump(pkg_cfg, open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w')) cfg = get_pkg_config(tmp_dir) assert cfg['license']['project'] == "toto" write_pkg_config(cfg, tmp_dir) pkg_cfg = json.load(open(pj(tmp_dir, ".pkglts/pkg_cfg.json"))) pkg_cfg["base"]["pkgname"] = "tutu" json.dump(pkg_cfg, open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w')) cfg = get_pkg_config(tmp_dir) assert cfg['license']['project'] == "tutu"
def action_example(*args, **kwds): """Install example files associated with options. """ del kwds # unused if len(args) == 0: raise UserWarning("need to specify at least one option name") logger.info("install examples") cfg = get_pkg_config() for name in args: install_example_files(name, cfg)
def test_regenerate_new_files_do_not_generate_conflicts(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) new_pth = tmp_dir / "src/toto/new_file.py" new_pth.write_text("txt = 'addendum'") regenerate_package(cfg, tmp_dir) assert new_pth.exists() assert new_pth.read_text() == "txt = 'addendum'"
def test_regenerate_handle_global_overwrite(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) addendum(init_file) regenerate_package(cfg, tmp_dir, overwrite=True) with open(init_file, 'r') as f: txt = f.read() assert "modified" not in txt
def test_regenerate_handle_conflicts_keep(tmp_pths, mocker): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) with open(init_file, 'w') as f: f.write("modified") with mocker.patch('pkglts.manage.get_user_permission', return_value=False): regenerate_package(cfg, tmp_dir) with open(init_file, 'r') as f: assert f.read() == "modified"
def test_regenerate_handle_conflicts_overwrite(): env = get_pkg_config(tmp_dir) addendum() with mock.patch('pkglts.manage.get_user_permission', return_value=True): regenerate_package(env, tmp_dir) with open(init_file, 'r') as f: txt = f.read() assert "modified" not in txt
def test_regenerate_handle_conflicts_keep(): env = get_pkg_config(tmp_dir) with open(init_file, 'w') as f: f.write("modified") with mock.patch('pkglts.manage.get_user_permission', return_value=False): regenerate_package(env, tmp_dir) with open(init_file, 'r') as f: assert f.read() == "modified"
def action_add(*args, **kwds): """Add new options in the package. """ del kwds # unused if len(args) == 0: raise UserWarning("need to specify at least one option name") logger.info("add option") cfg = get_pkg_config() for name in args: cfg = add_option(name, cfg) write_pkg_config(cfg)
def test_regenerate_handle_conflicts_overwrite(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) addendum(init_file) with mock.patch('pkglts.manage.get_user_permission', return_value=True): regenerate_package(cfg, tmp_dir) with open(init_file, 'r') as f: txt = f.read() assert "modified" not in txt
def test_regenerate_remove_user_files_do_not_generate_conflicts(): env = get_pkg_config(tmp_dir) new_pth = pj(tmp_dir, "src", "toto", "new_file.py") with open(new_pth, 'w') as f: f.write("txt = 'addendum'") regenerate_package(env, tmp_dir) remove(new_pth) regenerate_package(env, tmp_dir) assert not exists(new_pth)
def test_regenerate_remove_user_files_do_not_generate_conflicts(tmp_pths): tmp_dir, init_file = tmp_pths cfg = get_pkg_config(tmp_dir) new_pth = pj(tmp_dir, "src", "toto", "new_file.py") with open(new_pth, 'w') as f: f.write("txt = 'addendum'") regenerate_package(cfg, tmp_dir) remove(new_pth) regenerate_package(cfg, tmp_dir) assert not exists(new_pth)
def action_regenerate(*args, **kwds): """Regenerate all files in the package. """ overwrite = 'overwrite' in kwds cfg = get_pkg_config() clean() if len(args) == 0: logger.info("regenerate package") regenerate_package(cfg, overwrite=overwrite) else: for name in args: logger.info("regenerate '%s'" % name) regenerate_option(cfg, name, overwrite=overwrite)
def action_info(*args, **kwds): """Display info on package for debug purpose. """ del args # unused del kwds # unused logger.info("package info") from pkglts.option_tools import available_options print("available_options:") for opt_name in available_options: print(" ", opt_name) from pkglts.config_management import get_pkg_config cfg = get_pkg_config() print("current config (after resolution)") for opt_name, opt_params in cfg.items(): print(opt_name, opt_params)
def test_pkg_cfg_read_write_maintains_templates(): cfg = dict(default_cfg) cfg['base'] = dict(pkgname="toto", namespace="nm", url=None, authors=[("moi", "moi@aussi")]) cfg['license'] = dict(name="CeCILL-C", organization="org", project="{{ base.pkgname }}", year="2015") with open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w') as f: json.dump(cfg, f) env = get_pkg_config(tmp_dir) assert env.globals['license'].project == "toto" write_pkg_config(env, tmp_dir) with open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'r') as f: cfg = json.load(f) cfg["base"]["pkgname"] = "tutu" with open(pj(tmp_dir, ".pkglts/pkg_cfg.json"), 'w') as f: json.dump(cfg, f) env = get_pkg_config(tmp_dir) assert env.globals['license'].project == "tutu"