add has_config

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-06-26 01:24:40 +02:00
parent 7293289a1a
commit 525c27795c
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
2 changed files with 36 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import os.path
from repo_cloner.lib.dir_not_found_error import DirNotFoundError
from repo_cloner.lib.config_file_not_found_error import ConfigFileNotFoundError
class RepoDirStructure():
@ -15,25 +16,29 @@ class RepoDirStructure():
self._repos_dir = os.path.join(self._base_dir, "repos")
@property
def base_dir_exists(self):
def __config_filename(self) -> str:
return os.path.join(self.conf_dir, "cloner.cfg")
@property
def base_dir_exists(self) -> bool:
if not os.path.isdir(self._base_dir):
raise DirNotFoundError(self._base_dir)
return True
@property
def conf_dir_exists(self):
def conf_dir_exists(self) -> bool:
if not os.path.isdir(self._conf_dir):
raise DirNotFoundError(self._conf_dir)
return True
@property
def cache_dir_exists(self):
def cache_dir_exists(self) -> bool:
if not os.path.isdir(self._cache_dir):
raise DirNotFoundError(self._cache_dir)
return True
@property
def repos_dir_exists(self):
def repos_dir_exists(self) -> bool:
if not os.path.isdir(self._repos_dir):
raise DirNotFoundError(self._repos_dir)
return True
@ -64,3 +69,9 @@ class RepoDirStructure():
@property
def repos_dir(self) -> str:
return self._repos_dir
@property
def has_config(self) -> bool:
if not os.path.exists(self.__config_filename):
raise ConfigFileNotFoundError(self.__config_filename)
return True

View File

@ -2,6 +2,7 @@ import xml.dom.expatbuilder
from repo_cloner.lib.repo_dir_structure import RepoDirStructure
from repo_cloner.lib.dir_not_found_error import DirNotFoundError
from repo_cloner.lib.config_file_not_found_error import ConfigFileNotFoundError
from pathlib import PosixPath
import pytest
@ -14,6 +15,11 @@ def test_init():
assert X._cache_dir == "/tmp/cache"
def test_config_filename():
X = RepoDirStructure("/tmp")
assert X._RepoDirStructure__config_filename == "/tmp/config/cloner.cfg"
def test_no_base_dir(tmp_path: PosixPath):
x = tmp_path.joinpath("nonexistent")
X = RepoDirStructure(x)
@ -81,3 +87,18 @@ def test_config_dir():
def test_repos_dir():
X = RepoDirStructure("/a/c")
assert X.repos_dir == "/a/c/repos"
def test_has_config(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir()
tmp_path.joinpath("cache").mkdir()
tmp_path.joinpath("repos").mkdir()
X = RepoDirStructure(tmp_path)
with pytest.raises(ConfigFileNotFoundError) as excinfo:
X.has_config
assert "Config file does not exist: " in excinfo.exconly()
assert "config/cloner.cfg" in excinfo.exconly()
tmp_path.joinpath("config", "cloner.cfg").touch()
assert X.has_config