def __init__(self, args: argparse.Namespace): self.env = Env() arg_dict = vars(args) for arg in arg_dict: arg_val = arg_dict.get(arg) if isinstance(arg_val, Path): arg_val = arg_val.resolve() setattr(self, arg, arg_val)
def test_load_env_from_file(env_inst: Env): env_inst.set_env("TEST", "TEST") env_inst.save() env_inst.reset() assert not env_inst.env env_inst.load_env_from_file() assert env_inst.env
def test_load_env_to_os(env_inst: Env): env_key = "CWAS_TEST" env_value = "HELLO" env_inst.set_env(env_key, env_value) env_inst.save() env_inst.load_env_to_os() assert os.getenv(env_key) == env_value env_inst.get_path().unlink() os.environ.pop(env_key)
def test_env_singleton(cwas_env_path: Path): env1 = Env() env2 = Env() assert env1 is env2 new_env_path = Path(str(cwas_env_path) + "_new") env3 = Env(new_env_path) env4 = Env() assert env1 is env3 assert env1 is env4 assert env1.get_path() is new_env_path env1.set_path(cwas_env_path)
def teardown(cwas_workspace): yield env = Env() env.reset() env.remove_file() for f in cwas_workspace.glob("*"): f.unlink() cwas_workspace.rmdir()
def teardown(cwas_workspace: Path, vep_mock: Path): yield vep_mock.unlink() env = Env() env.reset() env.remove_file() for f in cwas_workspace.glob("*"): f.unlink() cwas_workspace.rmdir()
def test_env_save(env_inst: Env): env_key = "TEST" expected = "Hello!" env_inst.set_env(env_key, expected) env_inst.save() cwas_env_path = env_inst.get_path() with cwas_env_path.open() as env_file: env_line = env_file.read() env_line = env_line.strip() assert env_line == "TEST=Hello!" env_inst.get_path().unlink()
class Runnable(ABC): def __init__(self, args: argparse.Namespace): self.env = Env() arg_dict = vars(args) for arg in arg_dict: arg_val = arg_dict.get(arg) if isinstance(arg_val, Path): arg_val = arg_val.resolve() setattr(self, arg, arg_val) @classmethod def get_instance(cls, argv: list = []) -> Runnable: arg_parser = cls._create_arg_parser() args = arg_parser.parse_args(argv) cls._print_args(args) cls._check_args_validity(args) return cls(args) def set_env_path(self, path: Path): self.env.set_path(path) def set_env(self, env_key: str, env_value: Any): self.env.set_env(env_key, env_value) def get_env(self, env_key: str) -> Optional[str]: return self.env.get_env(env_key) def save_env(self): self.env.save() @staticmethod def _create_arg_parser() -> argparse.ArgumentParser: return argparse.ArgumentParser() @staticmethod def _print_args(args: argparse.Namespace): pass @staticmethod def _check_args_validity(args: argparse.Namespace): pass @abstractmethod def run(self): pass
def test_env_prev_set_exists(env_inst: Env): # Test if the environment variable exists. env_key = "TEST" expected = "Hello!" env_value = env_inst.get_env(env_key) assert env_value == expected
def test_env_reset(env_inst: Env): env_inst.reset() env_key = "TEST" env_value = env_inst.get_env(env_key) assert not env_inst.env assert env_value is None
def test_env_setting(env_inst: Env): env_key = "TEST" expected = "Hello!" env_inst.set_env(env_key, expected) env_value = env_inst.get_env(env_key) assert env_value == expected
def test_env_get_nonexist(env_inst: Env): env_value = env_inst.get_env("FAKE") assert env_value is None
def test_env_init(env_inst: Env, cwas_env_path: Path): assert env_inst.get_path() is cwas_env_path assert isinstance(env_inst.env, OrderedDict)
def set_env(cwas_workspace: Path): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.save()
def set_env(cwas_workspace, annotation_dir): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.set_env("VEP", "VEP") env.set_env("ANNOTATION_DATA", annotation_dir) env.set_env("ANNOTATION_BW_KEY", annotation_dir / "bw_key.yaml") env.set_env("MERGED_BED", cwas_workspace / "merged.bed.gz") env.set_env("MERGED_BED_INDEX", cwas_workspace / "merged.bed.gz.tbi") env.save()
def test_run_without_configuration(): inst = PreparationMock.get_instance() Env().reset() with pytest.raises(RuntimeError): inst.run()
def set_env(cwas_workspace: Path, annotation_dir: Path): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.set_env("ANNOTATION_DATA", annotation_dir) env.set_env("ANNOTATION_BED_KEY", cwas_workspace / "bed_key.yaml") env.save()
def test_load_env_to_os_without_save(env_inst: Env): env_key = "CWAS_TEST" env_value = "HELLO" env_inst.set_env(env_key, env_value) env_inst.load_env_to_os() assert os.getenv(env_key) is None
def reset_env(): env = Env() env.reset() env.remove_file()
def env_inst(): return Env()
def teardown(env_inst: Env): yield env_inst.reset() env_inst.remove_file()
def cwas_env_inst(cwas_env_path): env_inst = Env(cwas_env_path) yield env_inst.remove_file()
def _make_env_empty(): env = Env() env.reset() env.save()