New fixture, refactor test_repo_dir_structure

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-07-24 23:56:32 +02:00
parent 872b6bad3f
commit 125994ecd7
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
2 changed files with 41 additions and 40 deletions

View 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

View File

@ -3,36 +3,37 @@ import xml.dom.expatbuilder
from repo_cloner.lib.repo_dir_structure import RepoDirStructure from repo_cloner.lib.repo_dir_structure import RepoDirStructure
from repo_cloner.lib.dir_not_found_error import DirNotFoundError from repo_cloner.lib.dir_not_found_error import DirNotFoundError
from repo_cloner.lib.config_file_not_found_error import ConfigFileNotFoundError 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 import pytest
def test_init(): def test_init():
X = RepoDirStructure("/tmp") x = RepoDirStructure("/tmp")
assert X._base_dir == "/tmp" assert x._base_dir == "/tmp"
assert X._conf_dir == "/tmp/config" assert x._conf_dir == "/tmp/config"
assert X._repos_dir == "/tmp/repos" assert x._repos_dir == "/tmp/repos"
assert X._cache_dir == "/tmp/cache" assert x._cache_dir == "/tmp/cache"
def test_config_filename(): def test_config_filename():
X = RepoDirStructure("/tmp") x = RepoDirStructure("/tmp")
assert X._RepoDirStructure__config_filename == "/tmp/config/cloner.cfg" assert x._RepoDirStructure__config_filename == "/tmp/config/cloner.cfg"
def test_no_base_dir(tmp_path: PosixPath): def test_no_base_dir(tmp_path: PosixPath):
x = tmp_path.joinpath("nonexistent") x = tmp_path.joinpath("nonexistent")
X = RepoDirStructure(x) xx = RepoDirStructure(x)
with pytest.raises(DirNotFoundError) as excinfo: 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 "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/nonexistent") assert excinfo.exconly().endswith("/nonexistent")
def test_no_config(tmp_path: PosixPath): def test_no_config(tmp_path: PosixPath):
X = RepoDirStructure(tmp_path) x = RepoDirStructure(tmp_path.__str__())
with pytest.raises(DirNotFoundError) as excinfo: 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 "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly()
assert excinfo.exconly().endswith("/config") assert excinfo.exconly().endswith("/config")
@ -41,7 +42,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.cache_dir_exists assert 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")
@ -49,19 +50,15 @@ def test_no_cache(tmp_path: PosixPath):
def test_no_repos(tmp_path: PosixPath): def test_no_repos(tmp_path: PosixPath):
tmp_path.joinpath("config").mkdir() tmp_path.joinpath("config").mkdir()
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.repos_dir_exists assert 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_dirs_exist_okay(tmp_path: PosixPath): def test_dirs_exist_okay(cloner_dir_struct: Path):
tmp_path.joinpath("config").mkdir() X = RepoDirStructure(cloner_dir_struct)
tmp_path.joinpath("cache").mkdir()
tmp_path.joinpath("repos").mkdir()
X = RepoDirStructure(tmp_path)
assert X.base_dir_exists assert X.base_dir_exists
assert X.conf_dir_exists assert X.conf_dir_exists
assert X.cache_dir_exists assert X.cache_dir_exists
@ -89,18 +86,14 @@ def test_repos_dir():
assert X.repos_dir == "/a/c/repos" assert X.repos_dir == "/a/c/repos"
def test_has_config(tmp_path: PosixPath): def test_has_config(cloner_dir_struct: Path):
tmp_path.joinpath("config").mkdir() X = RepoDirStructure(cloner_dir_struct.__str__())
tmp_path.joinpath("cache").mkdir()
tmp_path.joinpath("repos").mkdir()
X = RepoDirStructure(tmp_path)
with pytest.raises(ConfigFileNotFoundError) as excinfo: with pytest.raises(ConfigFileNotFoundError) as excinfo:
X.has_config assert X.has_config
assert "Config file does not exist: " in excinfo.exconly() assert "Config file does not exist: " in excinfo.exconly()
assert "config/cloner.cfg" 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 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__() assert X.config_file == tmp_path.joinpath("config", "cloner.cfg").__str__()
def test_get_config(tmp_path: PosixPath): def test_get_config(cloner_dir_struct: Path):
tmp_path.joinpath("config").mkdir() x = RepoDirStructure(cloner_dir_struct.__str__())
tmp_path.joinpath("cache").mkdir()
tmp_path.joinpath("repos").mkdir()
X = RepoDirStructure(tmp_path)
# no file provided # no file provided
with pytest.raises(ConfigFileNotFoundError) as excinfo: with pytest.raises(ConfigFileNotFoundError) as excinfo:
X.config assert x.config
# create config file # create config file
tmp_path.joinpath("config", "cloner.cfg").touch() cloner_dir_struct.joinpath("config", "cloner.cfg").touch()
X = RepoDirStructure(tmp_path) x = RepoDirStructure(cloner_dir_struct.__str__())
assert X.has_config == True assert x.has_config == True
assert 0 == X.config.cloner_interval assert 0 == x.config.cloner_interval