def test_overwrite_prohibited(envvars): """ Prohibition of overwrite causes ValueError in case of collision. """ check_unset(envvars) with TmpEnv(**envvars), pytest.raises(ValueError): TmpEnv(overwrite=False, **{k: v + "_EXTRA" for k, v in envvars.items()}) check_unset(envvars)
def test_overwrite_allowed(envvars): """ Verify that overwriting existing value with different one works if permitted. """ check_unset(envvars) with TmpEnv(**envvars): modified = {k: v + "_EXTRA" for k, v in envvars.items()} with TmpEnv(overwrite=True, **modified): for k, v in modified.items(): assert v == os.getenv(k) check_unset(envvars)
def test_non_PathExAttMap_preserves_all_variables(path, fetch, env): """ Only a PathExAttMap eagerly attempts expansion of text as a path. """ m = AttMap() k = random.choice(string.ascii_letters) with TmpEnv(**env): m[k] = path assert path == fetch(m, k)
def test_select_local_config_file(tmpdir, setup, expect): """Selection of local filepath hinges on its existence as a file""" with TmpEnv(overwrite=True, **{ev: "" for ev in CFG_ENV_VARS}): _check_no_env_vars() path = setup(tmpdir) print("Path: {}".format(path)) with ExpectContext(expect(path), select_genome_config) as ctx: ctx(path)
def test_select_via_env_var_implicit(env_var, tmpdir): """Config file selection can leverage default environmanent variables.""" conf_file = tmpdir.join("test-refgenconf-conf.yaml").strpath assert not os.path.exists(conf_file) with open(conf_file, "w"): pass assert os.path.isfile(conf_file) with TmpEnv(overwrite=True, **{env_var: conf_file}): assert conf_file == select_genome_config(None)
def test_PathExAttMap_substitution_is_selective(path, pres, repl, env, pam, fetch): """ Values that are environment variables are replaced; others aren't. """ k = random.choice(string.ascii_lowercase) with TmpEnv(**env): pam[k] = path res = fetch(pam, k) print("Inserted path: {}".format(path)) print("Retrieved path: {}".format(res)) assert all(map(lambda s: s in res, pres)) assert all(map(lambda s: s not in res, repl))
def test_multiple_syntax_path_expansion(varname, path_parts, var_idx, tmpdir, store, fetch): """ Test the different combinations of setting and retrieving an env var path. """ key = "arbitrary" parts = copy.copy(path_parts) env_var = "$" + varname env_var_val = "set-via-env-var" parts.insert(var_idx, env_var) final_parts = [tmpdir.strpath] + parts print("FINAL PARTS: {}".format(final_parts)) path = os.path.join(*final_parts) m = PathExAttMap() store(m, key, path) with TmpEnv(**{varname: env_var_val}): assert env_var_val == os.getenv(varname) assert path != expandpath(path) assert expandpath(path) == fetch(m, key)
def test_select_null(): """Test prioritized selection of genome configuration file.""" with TmpEnv(overwrite=True, **{ev: "" for ev in CFG_ENV_VARS}): _check_no_env_vars() assert select_genome_config(None) is None
def test_PathExAttMap_expands_available_variables(pam, path, env, fetch): """ Insertion of text encoding environment variables should expand. """ k = random.choice(string.ascii_lowercase) with TmpEnv(**env): pam[k] = path assert expandpath(path) == fetch(pam, k)
def test_no_intersection(overwrite, envvars): """ Verify that overwrite permission is irrelevant if no variable name collision occurs. """ check_unset(envvars) with TmpEnv(overwrite=overwrite, **envvars): for k, v in envvars.items(): assert v == os.getenv(k)