1
0
Fork 0
mirror of synced 2024-12-22 06:31:07 -05:00

Support passing relative paths to --yadm-* and -w

Relative paths are expanded relative the current working dir as
expected.
This commit is contained in:
Erik Flodin 2021-01-07 18:59:41 +01:00
parent a5b1067e02
commit a321c88c7c
No known key found for this signature in database
GPG key ID: 420A7C865EE3F85F
4 changed files with 55 additions and 57 deletions

View file

@ -45,13 +45,18 @@ def test_init(
# command args # command args
args = ['init'] args = ['init']
cwd = None
if alt_work: if alt_work:
args.extend(['-w', paths.work]) if force:
cwd = paths.work.dirname
args.extend(['-w', paths.work.basename])
else:
args.extend(['-w', paths.work])
if force: if force:
args.append('-f') args.append('-f')
# run init # run init
run = runner(yadm_cmd(*args), env={'HOME': home}) run = runner(yadm_cmd(*args), env={'HOME': home}, cwd=cwd)
if repo_present and not force: if repo_present and not force:
assert run.failure assert run.failure

View file

@ -32,26 +32,30 @@ YDATA = '.local/share/yadm'
'override archive', 'override archive',
'override bootstrap', 'override bootstrap',
]) ])
def test_config(runner, paths, override, expect): @pytest.mark.parametrize(
'path', ['.', './override', 'override', '.override', '/override'], ids=[
'cwd', './relative', 'relative', 'hidden relative', 'absolute'
])
def test_config(runner, paths, override, expect, path):
"""Test configure_paths""" """Test configure_paths"""
opath = 'override' if path.startswith('/'):
matches = match_map() expected_path = path
args = [] else:
expected_path = str(paths.root.join(path))
args = [override, path] if override else []
if override == '-Y': if override == '-Y':
matches = match_map('/' + opath) matches = match_map(expected_path)
if override == '--yadm-data': elif override == '--yadm-data':
matches = match_map(None, '/' + opath) matches = match_map(None, expected_path)
else:
matches = match_map()
if override: for ekey in expect.keys():
args = [override, '/' + opath] matches[ekey] = f'{expect[ekey]}="{expected_path}"'
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) run_test(runner, paths, args, matches.values(), cwd=str(paths.root))
def match_map(yadm_dir=None, yadm_data=None): def match_map(yadm_dir=None, yadm_data=None):
@ -71,7 +75,7 @@ def match_map(yadm_dir=None, yadm_data=None):
} }
def run_test(runner, paths, args, expected_matches, expected_code=0): def run_test(runner, paths, args, expected_matches, cwd=None):
"""Run proces global args, and run configure_paths""" """Run proces global args, and run configure_paths"""
argstring = ' '.join(['"'+a+'"' for a in args]) argstring = ' '.join(['"'+a+'"' for a in args])
script = f""" script = f"""
@ -83,9 +87,8 @@ def run_test(runner, paths, args, expected_matches, expected_code=0):
configure_paths configure_paths
declare -p | grep -E '(YADM|GIT)_' declare -p | grep -E '(YADM|GIT)_'
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script, cwd=cwd)
assert run.code == expected_code assert run.success
assert run.success == (run.code == 0) assert run.err == ''
assert (run.err if run.success else run.out) == ''
for match in expected_matches: for match in expected_matches:
assert match in run.out if run.success else run.err assert match in run.out

54
yadm
View file

@ -130,10 +130,7 @@ function main() {
[ "$YADM_COMMAND" = "config" ] && YADM_ARGS+=("$1") [ "$YADM_COMMAND" = "config" ] && YADM_ARGS+=("$1")
;; ;;
-w) # used by init() and clone() -w) # used by init() and clone()
if [[ ! "$2" =~ ^/ ]] ; then YADM_WORK="$(qualify_path "$2" "work tree")"
error_out "You must specify a fully qualified work tree"
fi
YADM_WORK="$2"
shift shift
;; ;;
*) # any unhandled arguments *) # any unhandled arguments
@ -1495,52 +1492,31 @@ function process_global_args() {
key="$1" key="$1"
case $key in case $key in
-Y|--yadm-dir) # override the standard YADM_DIR -Y|--yadm-dir) # override the standard YADM_DIR
if [[ ! "$2" =~ ^/ ]] ; then YADM_DIR="$(qualify_path "$2" "yadm")"
error_out "You must specify a fully qualified yadm directory"
fi
YADM_DIR="$2"
shift shift
;; ;;
--yadm-data) # override the standard YADM_DATA --yadm-data) # override the standard YADM_DATA
if [[ ! "$2" =~ ^/ ]] ; then YADM_DATA="$(qualify_path "$2" "data")"
error_out "You must specify a fully qualified yadm data directory"
fi
YADM_DATA="$2"
shift shift
;; ;;
--yadm-repo) # override the standard YADM_REPO --yadm-repo) # override the standard YADM_REPO
if [[ ! "$2" =~ ^/ ]] ; then YADM_OVERRIDE_REPO="$(qualify_path "$2" "repo")"
error_out "You must specify a fully qualified repo path"
fi
YADM_OVERRIDE_REPO="$2"
shift shift
;; ;;
--yadm-config) # override the standard YADM_CONFIG --yadm-config) # override the standard YADM_CONFIG
if [[ ! "$2" =~ ^/ ]] ; then YADM_OVERRIDE_CONFIG="$(qualify_path "$2" "config")"
error_out "You must specify a fully qualified config path"
fi
YADM_OVERRIDE_CONFIG="$2"
shift shift
;; ;;
--yadm-encrypt) # override the standard YADM_ENCRYPT --yadm-encrypt) # override the standard YADM_ENCRYPT
if [[ ! "$2" =~ ^/ ]] ; then YADM_OVERRIDE_ENCRYPT="$(qualify_path "$2" "encrypt")"
error_out "You must specify a fully qualified encrypt path"
fi
YADM_OVERRIDE_ENCRYPT="$2"
shift shift
;; ;;
--yadm-archive) # override the standard YADM_ARCHIVE --yadm-archive) # override the standard YADM_ARCHIVE
if [[ ! "$2" =~ ^/ ]] ; then YADM_OVERRIDE_ARCHIVE="$(qualify_path "$2" "archive")"
error_out "You must specify a fully qualified archive path"
fi
YADM_OVERRIDE_ARCHIVE="$2"
shift shift
;; ;;
--yadm-bootstrap) # override the standard YADM_BOOTSTRAP --yadm-bootstrap) # override the standard YADM_BOOTSTRAP
if [[ ! "$2" =~ ^/ ]] ; then YADM_OVERRIDE_BOOTSTRAP="$(qualify_path "$2" "bootstrap")"
error_out "You must specify a fully qualified bootstrap path"
fi
YADM_OVERRIDE_BOOTSTRAP="$2"
shift shift
;; ;;
*) # main arguments are kept intact *) # main arguments are kept intact
@ -1552,6 +1528,20 @@ function process_global_args() {
} }
function qualify_path() {
local path="$1"
if [[ -z "$path" ]]; then
error_out "You can't specify an empty $2 path"
fi
if [[ "$path" = "." ]]; then
path="$PWD"
elif [[ "$path" != /* ]]; then
path="$PWD/${path#./}"
fi
echo "$path"
}
function set_yadm_dirs() { function set_yadm_dirs() {
# only resolve YADM_DATA if it hasn't been provided already # only resolve YADM_DATA if it hasn't been provided already

2
yadm.1
View file

@ -331,7 +331,7 @@ alias yadm='yadm --yadm-repo /alternate/path/to/repo'
.RE .RE
The following is the full list of universal options. The following is the full list of universal options.
Each option should be followed by a fully qualified path. Each option should be followed by a path.
.TP .TP
.B -Y,--yadm-dir .B -Y,--yadm-dir
Override the yadm directory. Override the yadm directory.