2018-07-11 08:50:42 -04:00
|
|
|
"""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"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"override, expect",
|
|
|
|
[
|
2018-07-11 08:50:42 -04:00
|
|
|
(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",
|
|
|
|
],
|
|
|
|
)
|
2021-01-07 12:59:41 -05:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"path",
|
|
|
|
[".", "./override", "override", ".override", "/override"],
|
|
|
|
ids=["cwd", "./relative", "relative", "hidden relative", "absolute"],
|
|
|
|
)
|
2021-01-07 12:59:41 -05:00
|
|
|
def test_config(runner, paths, override, expect, path):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Test configure_paths"""
|
2023-07-10 15:43:17 -04:00
|
|
|
if path.startswith("/"):
|
2021-01-07 12:59:41 -05:00
|
|
|
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":
|
2021-01-07 12:59:41 -05:00
|
|
|
matches = match_map(expected_path)
|
2023-07-10 15:43:17 -04:00
|
|
|
elif override == "--yadm-data":
|
2021-01-07 12:59:41 -05:00
|
|
|
matches = match_map(None, expected_path)
|
|
|
|
else:
|
|
|
|
matches = match_map()
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2021-01-07 12:59:41 -05:00
|
|
|
for ekey in expect.keys():
|
|
|
|
matches[ekey] = f'{expect[ekey]}="{expected_path}"'
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2021-01-07 12:59:41 -05:00
|
|
|
run_test(runner, paths, args, matches.values(), cwd=str(paths.root))
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
2020-11-17 15:57:34 -05:00
|
|
|
def match_map(yadm_dir=None, yadm_data=None):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""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])
|
2020-11-17 15:57:34 -05:00
|
|
|
if not yadm_data:
|
2023-07-10 15:43:17 -04:00
|
|
|
yadm_data = "/".join([HOME, YDATA])
|
2018-07-11 08:50:42 -04:00
|
|
|
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}"',
|
|
|
|
}
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
2021-01-07 12:59:41 -05:00
|
|
|
def run_test(runner, paths, args, expected_matches, cwd=None):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Run proces global args, and run configure_paths"""
|
2023-07-10 15:43:17 -04:00
|
|
|
argstring = " ".join(['"' + a + '"' for a in args])
|
2018-07-11 08:50:42 -04:00
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
|
|
|
|
process_global_args {argstring}
|
2020-11-17 15:57:34 -05:00
|
|
|
XDG_CONFIG_HOME=
|
|
|
|
XDG_DATA_HOME=
|
2020-11-15 15:23:49 -05:00
|
|
|
HOME="{HOME}" set_yadm_dirs
|
2018-07-11 08:50:42 -04:00
|
|
|
configure_paths
|
|
|
|
declare -p | grep -E '(YADM|GIT)_'
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script, cwd=cwd)
|
2021-01-07 12:59:41 -05:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2018-07-11 08:50:42 -04:00
|
|
|
for match in expected_matches:
|
2021-01-07 12:59:41 -05:00
|
|
|
assert match in run.out
|