Add test condition for every dir possible

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-06-26 00:05:36 +02:00
parent e500c775d4
commit 69151619f7
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
2 changed files with 35 additions and 8 deletions

View File

@ -15,18 +15,40 @@ class RepoDirStructure():
self._repos_dir = os.path.join(self._base_dir, "repos") self._repos_dir = os.path.join(self._base_dir, "repos")
@property @property
def exists(self) -> bool: def base_dir_exists(self):
if not os.path.isdir(self._base_dir): if not os.path.isdir(self._base_dir):
raise DirNotFoundError(self._base_dir) raise DirNotFoundError(self._base_dir)
return True
@property
def conf_dir_exists(self):
if not os.path.isdir(self._conf_dir): if not os.path.isdir(self._conf_dir):
raise DirNotFoundError(self._conf_dir) raise DirNotFoundError(self._conf_dir)
return True
@property
def cache_dir_exists(self):
if not os.path.isdir(self._cache_dir): if not os.path.isdir(self._cache_dir):
raise DirNotFoundError(self._cache_dir) raise DirNotFoundError(self._cache_dir)
return True
@property
def repos_dir_exists(self):
if not os.path.isdir(self._repos_dir): if not os.path.isdir(self._repos_dir):
raise DirNotFoundError(self._repos_dir) raise DirNotFoundError(self._repos_dir)
return True return True
@property
def dirs_exist(self) -> bool:
return all(
[
self.base_dir_exists,
self.conf_dir_exists,
self.cache_dir_exists,
self.repos_dir_exists,
]
)
@property @property
def base_dir(self) -> str: def base_dir(self) -> str:
return self._base_dir return self._base_dir

View File

@ -18,7 +18,7 @@ def test_no_base_dir(tmp_path: PosixPath):
x = tmp_path.joinpath("nonexistent") x = tmp_path.joinpath("nonexistent")
X = RepoDirStructure(x) X = RepoDirStructure(x)
with pytest.raises(DirNotFoundError) as excinfo: with pytest.raises(DirNotFoundError) as excinfo:
X.exists X.base_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/nonexistent") assert excinfo.exconly().endswith("/nonexistent")
@ -26,7 +26,7 @@ def test_no_base_dir(tmp_path: PosixPath):
def test_no_config(tmp_path: PosixPath): def test_no_config(tmp_path: PosixPath):
X = RepoDirStructure(tmp_path) X = RepoDirStructure(tmp_path)
with pytest.raises(DirNotFoundError) as excinfo: with pytest.raises(DirNotFoundError) as excinfo:
X.exists X.conf_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/config") assert excinfo.exconly().endswith("/config")
@ -35,7 +35,7 @@ def test_no_cache(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir() tmp_path.joinpath("config").mkdir()
X = RepoDirStructure(tmp_path) X = RepoDirStructure(tmp_path)
with pytest.raises(DirNotFoundError) as excinfo: with pytest.raises(DirNotFoundError) as excinfo:
X.exists X.cache_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/cache") assert excinfo.exconly().endswith("/cache")
@ -45,17 +45,22 @@ def test_no_repos(tmp_path: PosixPath):
tmp_path.joinpath("cache").mkdir() tmp_path.joinpath("cache").mkdir()
X = RepoDirStructure(tmp_path) X = RepoDirStructure(tmp_path)
with pytest.raises(DirNotFoundError) as excinfo: with pytest.raises(DirNotFoundError) as excinfo:
X.exists X.repos_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/repos") assert excinfo.exconly().endswith("/repos")
def test_exists_okay(tmp_path: PosixPath): def test_dirs_exist_okay(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir() tmp_path.joinpath("config").mkdir()
tmp_path.joinpath("cache").mkdir() tmp_path.joinpath("cache").mkdir()
tmp_path.joinpath("repos").mkdir() tmp_path.joinpath("repos").mkdir()
X = RepoDirStructure(tmp_path) X = RepoDirStructure(tmp_path)
assert True == X.exists assert X.base_dir_exists
assert X.conf_dir_exists
assert X.cache_dir_exists
assert X.repos_dir_exists
assert X.dirs_exist
def test_base_dir(): def test_base_dir():