repo-cloner/tests/lib/test_repo_dir_structure.py
Václav Valíček 125994ecd7
New fixture, refactor test_repo_dir_structure
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
2022-07-24 23:56:32 +02:00

116 lines
3.4 KiB
Python

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, Path
from cloner_test_fixtures import cloner_dir_struct
import pytest
def test_init():
x = RepoDirStructure("/tmp")
assert x._base_dir == "/tmp"
assert x._conf_dir == "/tmp/config"
assert x._repos_dir == "/tmp/repos"
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")
xx = RepoDirStructure(x)
with pytest.raises(DirNotFoundError) as excinfo:
assert xx.base_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/nonexistent")
def test_no_config(tmp_path: PosixPath):
x = RepoDirStructure(tmp_path.__str__())
with pytest.raises(DirNotFoundError) as excinfo:
assert x.conf_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/config")
def test_no_cache(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir()
X = RepoDirStructure(tmp_path)
with pytest.raises(DirNotFoundError) as excinfo:
assert X.cache_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/cache")
def test_no_repos(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir()
tmp_path.joinpath("cache").mkdir()
x = RepoDirStructure(tmp_path)
with pytest.raises(DirNotFoundError) as excinfo:
assert x.repos_dir_exists
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/repos")
def test_dirs_exist_okay(cloner_dir_struct: Path):
X = RepoDirStructure(cloner_dir_struct)
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():
X = RepoDirStructure("/a/b")
assert X.base_dir == "/a/b"
def test_cache_dir():
X = RepoDirStructure("/a/b")
assert X.cache_dir == "/a/b/cache"
def test_config_dir():
X = RepoDirStructure("/a/b")
assert X.conf_dir == "/a/b/config"
def test_repos_dir():
X = RepoDirStructure("/a/c")
assert X.repos_dir == "/a/c/repos"
def test_has_config(cloner_dir_struct: Path):
X = RepoDirStructure(cloner_dir_struct.__str__())
with pytest.raises(ConfigFileNotFoundError) as excinfo:
assert X.has_config
assert "Config file does not exist: " in excinfo.exconly()
assert "config/cloner.cfg" in excinfo.exconly()
cloner_dir_struct.joinpath("config", "cloner.cfg").touch()
assert X.has_config
def test_config_file(tmp_path: PosixPath):
X = RepoDirStructure(tmp_path)
assert X.config_file == tmp_path.joinpath("config", "cloner.cfg").__str__()
def test_get_config(cloner_dir_struct: Path):
x = RepoDirStructure(cloner_dir_struct.__str__())
# no file provided
with pytest.raises(ConfigFileNotFoundError) as excinfo:
assert x.config
# create config file
cloner_dir_struct.joinpath("config", "cloner.cfg").touch()
x = RepoDirStructure(cloner_dir_struct.__str__())
assert x.has_config == True
assert 0 == x.config.cloner_interval