2018-07-11 12:50:42 +00:00
|
|
|
"""Unit tests: configure_paths"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-09-25 15:03:27 +00:00
|
|
|
ARCHIVE = 'archive'
|
2018-07-11 12:50:42 +00:00
|
|
|
BOOTSTRAP = 'bootstrap'
|
|
|
|
CONFIG = 'config'
|
|
|
|
ENCRYPT = 'encrypt'
|
|
|
|
HOME = '/testhome'
|
|
|
|
REPO = 'repo.git'
|
2019-08-06 13:19:45 +00:00
|
|
|
YDIR = '.config/yadm'
|
2020-11-17 20:57:34 +00:00
|
|
|
YDATA = '.local/share/yadm'
|
2018-07-11 12:50:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'override, expect', [
|
|
|
|
(None, {}),
|
2020-11-17 20:57:34 +00:00
|
|
|
('-Y', {'yadm': 'YADM_DIR'}),
|
|
|
|
('--yadm-data', {'data': 'YADM_DATA'}),
|
2018-07-11 12:50:42 +00:00
|
|
|
('--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',
|
2020-11-17 20:57:34 +00:00
|
|
|
'override yadm data',
|
2018-07-11 12:50:42 +00:00
|
|
|
'override repo',
|
|
|
|
'override config',
|
|
|
|
'override encrypt',
|
|
|
|
'override archive',
|
|
|
|
'override bootstrap',
|
|
|
|
])
|
|
|
|
def test_config(runner, paths, override, expect):
|
|
|
|
"""Test configure_paths"""
|
|
|
|
opath = 'override'
|
|
|
|
matches = match_map()
|
|
|
|
args = []
|
|
|
|
if override == '-Y':
|
|
|
|
matches = match_map('/' + opath)
|
2020-11-17 20:57:34 +00:00
|
|
|
if override == '--yadm-data':
|
|
|
|
matches = match_map(None, '/' + opath)
|
2018-07-11 12:50:42 +00:00
|
|
|
|
|
|
|
if override:
|
|
|
|
args = [override, '/' + opath]
|
|
|
|
for ekey in expect.keys():
|
|
|
|
matches[ekey] = f'{expect[ekey]}="/{opath}"'
|
|
|
|
run_test(
|
|
|
|
runner, paths,
|
|
|
|
[override, opath],
|
|
|
|
['must specify a fully qualified'], 1)
|
|
|
|
|
|
|
|
run_test(runner, paths, args, matches.values(), 0)
|
|
|
|
|
|
|
|
|
2020-11-17 20:57:34 +00:00
|
|
|
def match_map(yadm_dir=None, yadm_data=None):
|
2018-07-11 12:50:42 +00:00
|
|
|
"""Create a dictionary of matches, relative to yadm_dir"""
|
|
|
|
if not yadm_dir:
|
|
|
|
yadm_dir = '/'.join([HOME, YDIR])
|
2020-11-17 20:57:34 +00:00
|
|
|
if not yadm_data:
|
|
|
|
yadm_data = '/'.join([HOME, YDATA])
|
2018-07-11 12:50:42 +00:00
|
|
|
return {
|
|
|
|
'yadm': f'YADM_DIR="{yadm_dir}"',
|
2020-11-17 20:57:34 +00:00
|
|
|
'repo': f'YADM_REPO="{yadm_data}/{REPO}"',
|
2018-07-11 12:50:42 +00:00
|
|
|
'config': f'YADM_CONFIG="{yadm_dir}/{CONFIG}"',
|
|
|
|
'encrypt': f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"',
|
2020-11-17 20:57:34 +00:00
|
|
|
'archive': f'YADM_ARCHIVE="{yadm_data}/{ARCHIVE}"',
|
2018-07-11 12:50:42 +00:00
|
|
|
'bootstrap': f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"',
|
2020-11-17 20:57:34 +00:00
|
|
|
'git': f'GIT_DIR="{yadm_data}/{REPO}"',
|
2018-07-11 12:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def run_test(runner, paths, args, expected_matches, expected_code=0):
|
|
|
|
"""Run proces global args, and run configure_paths"""
|
|
|
|
argstring = ' '.join(['"'+a+'"' for a in args])
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
|
|
|
|
process_global_args {argstring}
|
2020-11-17 20:57:34 +00:00
|
|
|
XDG_CONFIG_HOME=
|
|
|
|
XDG_DATA_HOME=
|
2020-11-15 20:23:49 +00:00
|
|
|
HOME="{HOME}" set_yadm_dirs
|
2018-07-11 12:50:42 +00:00
|
|
|
configure_paths
|
|
|
|
declare -p | grep -E '(YADM|GIT)_'
|
|
|
|
"""
|
|
|
|
run = runner(command=['bash'], inp=script)
|
|
|
|
assert run.code == expected_code
|
|
|
|
assert run.err == ''
|
|
|
|
for match in expected_matches:
|
|
|
|
assert match in run.out
|