New fixture, refactor test_repo_dir_structure
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
parent
872b6bad3f
commit
125994ecd7
12
tests/lib/cloner_test_fixtures.py
Normal file
12
tests/lib/cloner_test_fixtures.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import pytest
|
||||
from pathlib import Path
|
||||
|
||||
@pytest.fixture
|
||||
def cloner_dir_struct(tmp_path: Path) -> Path:
|
||||
tmp_path.joinpath("config").mkdir()
|
||||
tmp_path.joinpath("cache").mkdir()
|
||||
tmp_path.joinpath("repos").mkdir()
|
||||
return tmp_path
|
||||
|
||||
|
||||
|
|
@ -3,36 +3,37 @@ 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
|
||||
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"
|
||||
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"
|
||||
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)
|
||||
xx = RepoDirStructure(x)
|
||||
with pytest.raises(DirNotFoundError) as excinfo:
|
||||
X.base_dir_exists
|
||||
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)
|
||||
x = RepoDirStructure(tmp_path.__str__())
|
||||
with pytest.raises(DirNotFoundError) as excinfo:
|
||||
X.conf_dir_exists
|
||||
assert x.conf_dir_exists
|
||||
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
|
||||
assert excinfo.exconly().endswith("/config")
|
||||
|
||||
|
@ -41,7 +42,7 @@ def test_no_cache(tmp_path: PosixPath):
|
|||
tmp_path.joinpath("config").mkdir()
|
||||
X = RepoDirStructure(tmp_path)
|
||||
with pytest.raises(DirNotFoundError) as excinfo:
|
||||
X.cache_dir_exists
|
||||
assert X.cache_dir_exists
|
||||
assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
|
||||
assert excinfo.exconly().endswith("/cache")
|
||||
|
||||
|
@ -49,19 +50,15 @@ def test_no_cache(tmp_path: PosixPath):
|
|||
def test_no_repos(tmp_path: PosixPath):
|
||||
tmp_path.joinpath("config").mkdir()
|
||||
tmp_path.joinpath("cache").mkdir()
|
||||
X = RepoDirStructure(tmp_path)
|
||||
x = RepoDirStructure(tmp_path)
|
||||
with pytest.raises(DirNotFoundError) as excinfo:
|
||||
X.repos_dir_exists
|
||||
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(tmp_path: PosixPath):
|
||||
tmp_path.joinpath("config").mkdir()
|
||||
tmp_path.joinpath("cache").mkdir()
|
||||
tmp_path.joinpath("repos").mkdir()
|
||||
|
||||
X = RepoDirStructure(tmp_path)
|
||||
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
|
||||
|
@ -89,18 +86,14 @@ def test_repos_dir():
|
|||
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)
|
||||
def test_has_config(cloner_dir_struct: Path):
|
||||
X = RepoDirStructure(cloner_dir_struct.__str__())
|
||||
with pytest.raises(ConfigFileNotFoundError) as excinfo:
|
||||
X.has_config
|
||||
assert 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()
|
||||
cloner_dir_struct.joinpath("config", "cloner.cfg").touch()
|
||||
assert X.has_config
|
||||
|
||||
|
||||
|
@ -109,18 +102,14 @@ def test_config_file(tmp_path: PosixPath):
|
|||
assert X.config_file == tmp_path.joinpath("config", "cloner.cfg").__str__()
|
||||
|
||||
|
||||
def test_get_config(tmp_path: PosixPath):
|
||||
tmp_path.joinpath("config").mkdir()
|
||||
tmp_path.joinpath("cache").mkdir()
|
||||
tmp_path.joinpath("repos").mkdir()
|
||||
|
||||
X = RepoDirStructure(tmp_path)
|
||||
def test_get_config(cloner_dir_struct: Path):
|
||||
x = RepoDirStructure(cloner_dir_struct.__str__())
|
||||
# no file provided
|
||||
with pytest.raises(ConfigFileNotFoundError) as excinfo:
|
||||
X.config
|
||||
assert x.config
|
||||
|
||||
# create config file
|
||||
tmp_path.joinpath("config", "cloner.cfg").touch()
|
||||
X = RepoDirStructure(tmp_path)
|
||||
assert X.has_config == True
|
||||
assert 0 == X.config.cloner_interval
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user