diff --git a/kiwi_scp/config.py b/kiwi_scp/config.py index d9e0368..44fc137 100644 --- a/kiwi_scp/config.py +++ b/kiwi_scp/config.py @@ -95,7 +95,7 @@ class Config(BaseModel): Path("/bin/bash"), ] - projects: Optional[List[_Project]] + projects: List[_Project] = [] environment: Dict[str, Optional[str]] = {} @@ -152,6 +152,40 @@ class Config(BaseModel): return yml_string + @validator("projects", pre=True) + @classmethod + def unify_projects(cls, value): + """parse different projects notations""" + + if value is None: + # empty projects list + return [] + + elif isinstance(value, list): + # handle projects list + + result = [] + for entry in value: + # ignore empties + if entry is not None: + if isinstance(entry, dict): + # handle single project dict + result.append(entry) + + else: + # handle single project name + result.append({"name": str(entry)}) + + return result + + elif isinstance(value, dict): + # handle single project dict + return [value] + + else: + # handle single project name + return [{"name": str(value)}] + @validator("environment", pre=True) @classmethod def unify_environment(cls, value) -> Dict[str, Optional[str]]: diff --git a/tests/test_config.py b/tests/test_config.py index fa08258..4571946 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -15,13 +15,78 @@ def test_default(): assert c.version == version assert len(c.shells) == 1 assert c.shells[0] == Path("/bin/bash") - assert c.projects is None + assert c.projects == [] assert c.environment == {} assert c.storage.directory == Path("/var/local/kiwi") assert c.network.name == "kiwi_hub" assert c.network.cidr == IPv4Network("10.22.46.0/24") +########## +# PROJECTS +########## + +def test_proj_empty(): + c = Config(projects=None) + + assert c.projects == [] + + c = Config(projects=[]) + + assert c.projects == [] + + +def test_proj_long(): + c = Config(projects=[{ + "name": "project", + "enabled": False, + "override_storage": {"directory": "/test/directory"}, + }]) + + assert len(c.projects) == 1 + p = c.projects[0] + assert p.name == "project" + assert not p.enabled + assert p.override_storage is not None + assert p.override_storage.directory == Path("/test/directory") + + +def test_proj_short(): + c = Config(projects=[{ + "project": False, + }]) + + assert len(c.projects) == 1 + p = c.projects[0] + assert p.name == "project" + assert not p.enabled + assert p.override_storage is None + + +def test_proj_dict(): + c = Config(projects={"name": "project"}) + + assert c == Config(projects=[{"name": "project"}]) + + assert len(c.projects) == 1 + p = c.projects[0] + assert p.name == "project" + assert p.enabled + assert p.override_storage is None + + +def test_proj_name(): + c = Config(projects="project") + + assert c == Config(projects=["project"]) + + assert len(c.projects) == 1 + p = c.projects[0] + assert p.name == "project" + assert p.enabled + assert p.override_storage is None + + ############# # ENVIRONMENT ############# @@ -37,9 +102,7 @@ def test_env_dict(): assert c.environment == {} - c = Config(environment={ - "variable": "value" - }) + c = Config(environment={"variable": "value"}) assert len(c.environment) == 1 assert "variable" in c.environment