1
0
Fork 0
mirror of synced 2024-11-05 01:08:59 -05:00
yadm/test/test_unit_configure_paths.py

99 lines
2.9 KiB
Python
Raw Normal View History

"""Unit tests: configure_paths"""
import pytest
2023-07-10 15:43:17 -04:00
ARCHIVE = "archive"
BOOTSTRAP = "bootstrap"
CONFIG = "config"
ENCRYPT = "encrypt"
HOME = "/testhome"
REPO = "repo.git"
YDIR = ".config/yadm"
YDATA = ".local/share/yadm"
@pytest.mark.parametrize(
2023-07-10 15:43:17 -04:00
"override, expect",
[
(None, {}),
2023-07-10 15:43:17 -04:00
("-Y", {"yadm": "YADM_DIR"}),
("--yadm-data", {"data": "YADM_DATA"}),
("--yadm-repo", {"repo": "YADM_REPO", "git": "GIT_DIR"}),
("--yadm-config", {"config": "YADM_CONFIG"}),
("--yadm-encrypt", {"encrypt": "YADM_ENCRYPT"}),
("--yadm-archive", {"archive": "YADM_ARCHIVE"}),
("--yadm-bootstrap", {"bootstrap": "YADM_BOOTSTRAP"}),
],
ids=[
"default",
"override yadm dir",
"override yadm data",
"override repo",
"override config",
"override encrypt",
"override archive",
"override bootstrap",
],
)
@pytest.mark.parametrize(
2023-07-10 15:43:17 -04:00
"path",
[".", "./override", "override", ".override", "/override"],
ids=["cwd", "./relative", "relative", "hidden relative", "absolute"],
)
def test_config(runner, paths, override, expect, path):
"""Test configure_paths"""
2023-07-10 15:43:17 -04:00
if path.startswith("/"):
expected_path = path
else:
expected_path = str(paths.root.join(path))
args = [override, path] if override else []
2023-07-10 15:43:17 -04:00
if override == "-Y":
matches = match_map(expected_path)
2023-07-10 15:43:17 -04:00
elif override == "--yadm-data":
matches = match_map(None, expected_path)
else:
matches = match_map()
for ekey in expect.keys():
matches[ekey] = f'{expect[ekey]}="{expected_path}"'
run_test(runner, paths, args, matches.values(), cwd=str(paths.root))
def match_map(yadm_dir=None, yadm_data=None):
"""Create a dictionary of matches, relative to yadm_dir"""
if not yadm_dir:
2023-07-10 15:43:17 -04:00
yadm_dir = "/".join([HOME, YDIR])
if not yadm_data:
2023-07-10 15:43:17 -04:00
yadm_data = "/".join([HOME, YDATA])
return {
2023-07-10 15:43:17 -04:00
"yadm": f'YADM_DIR="{yadm_dir}"',
"repo": f'YADM_REPO="{yadm_data}/{REPO}"',
"config": f'YADM_CONFIG="{yadm_dir}/{CONFIG}"',
"encrypt": f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"',
"archive": f'YADM_ARCHIVE="{yadm_data}/{ARCHIVE}"',
"bootstrap": f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"',
"git": f'GIT_DIR="{yadm_data}/{REPO}"',
}
def run_test(runner, paths, args, expected_matches, cwd=None):
"""Run proces global args, and run configure_paths"""
2023-07-10 15:43:17 -04:00
argstring = " ".join(['"' + a + '"' for a in args])
script = f"""
YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
process_global_args {argstring}
XDG_CONFIG_HOME=
XDG_DATA_HOME=
HOME="{HOME}" set_yadm_dirs
configure_paths
declare -p | grep -E '(YADM|GIT)_'
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script, cwd=cwd)
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
for match in expected_matches:
assert match in run.out