1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-03-22 16:45:08 +00:00

Merge branch 'master' into clear-on-install

This commit is contained in:
deadc0de
2023-10-22 14:46:02 +02:00
committed by GitHub
34 changed files with 1311 additions and 328 deletions

View File

@@ -31,7 +31,10 @@ echo -e "$(tput setaf 6)==> RUNNING $(basename "${BASH_SOURCE[0]}") <==$(tput sg
# $2 path
grep_or_fail()
{
grep "${1}" "${2}" >/dev/null 2>&1 || (echo "pattern not found in ${2}" && exit 1)
if ! grep "${1}" "${2}" >/dev/null 2>&1; then
echo "pattern not found in ${2}"
exit 1
fi
}
# the action temp

248
tests-ng/backup.sh vendored Executable file
View File

@@ -0,0 +1,248 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2023, deadc0de6
#
# test for backups
# returns 1 in case of error
#
## start-cookie
set -euo errtrace pipefail
cur=$(cd "$(dirname "${0}")" && pwd)
ddpath="${cur}/../"
PPATH="{PYTHONPATH:-}"
export PYTHONPATH="${ddpath}:${PPATH}"
altbin="python3 -m dotdrop.dotdrop"
if hash coverage 2>/dev/null; then
mkdir -p coverages/
altbin="coverage run -p --data-file coverages/coverage --source=dotdrop -m dotdrop.dotdrop"
fi
bin="${DT_BIN:-${altbin}}"
# shellcheck source=tests-ng/helpers
source "${cur}"/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename "${BASH_SOURCE[0]}") <==$(tput sgr0)"
## end-cookie
################################################################
# this is the test
################################################################
# $1 pattern
# $2 path
grep_or_fail()
{
if ! grep "${1}" "${2}" >/dev/null 2>&1; then
echo "pattern \"${1}\" not found in ${2}"
exit 1
fi
}
# the dotfile source
tmps=$(mktemp -d --suffix='-dotdrop-tests-dotpath' || mktemp -d)
mkdir -p "${tmps}"/dotfiles
# the dotfile destination
tmpd=$(mktemp -d --suffix='-dotdrop-tests-dst' || mktemp -d)
tmpw=$(mktemp -d --suffix='-dotdrop-workdir' || mktemp -d)
clear_on_exit "${tmps}"
clear_on_exit "${tmpd}"
clear_on_exit "${tmpw}"
clear_dotpath()
{
rm -rf "${tmps:?}"/dotfiles/*
}
create_dotpath()
{
# create the dotfiles in dotpath
echo "modified" > "${tmps}"/dotfiles/file
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/template
mkdir -p "${tmps}"/dotfiles/dir
echo "modified" > "${tmps}"/dotfiles/dir/sub
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/dir/template
mkdir -p "${tmps}"/dotfiles/tree
echo "modified" > "${tmps}"/dotfiles/tree/file
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/tree/template
mkdir -p "${tmps}"/dotfiles/tree/sub
echo "modified" > "${tmps}"/dotfiles/tree/sub/file
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/tree/sub/template
}
clear_fs()
{
rm -rf "${tmpd:?}"/*
}
create_fs()
{
# create the existing dotfiles in filesystem
echo "original" > "${tmpd}"/file
echo "original" > "${tmpd}"/template
mkdir -p "${tmpd}"/dir
echo "original" > "${tmpd}"/dir/sub
echo "original" > "${tmpd}"/dir/template
mkdir -p "${tmpd}"/tree
echo "original" > "${tmpd}"/tree/file
echo "original" > "${tmpd}"/tree/template
mkdir -p "${tmpd}"/tree/sub
echo "original" > "${tmpd}"/tree/sub/file
echo "original" > "${tmpd}"/tree/sub/template
}
# create the config file
cfg="${tmps}/config.yaml"
# $1: linktype
create_config()
{
link_default="${1}"
link_file="${1}"
link_dir="${1}"
if [ "${link_default}" = "link_children" ]; then
link_file="nolink"
fi
cat > "${cfg}" << _EOF
config:
backup: true
create: true
dotpath: dotfiles
link_dotfile_default: ${link_default}
workdir: ${tmpw}
dotfiles:
f_file:
dst: ${tmpd}/file
src: file
link: ${link_file}
f_template:
dst: ${tmpd}/template
src: template
link: ${link_file}
d_dir:
dst: ${tmpd}/dir
src: dir
link: ${link_dir}
d_tree:
dst: ${tmpd}/tree
src: tree
link: ${link_dir}
profiles:
p1:
dotfiles:
- f_file
- f_template
- d_dir
- d_tree
_EOF
#cat ${cfg}
}
# install nolink
pre="link:nolink"
create_config "nolink"
clear_dotpath
clear_fs
create_dotpath
create_fs
cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 --verbose
# checks
[ ! -e "${tmpd}"/file.dotdropbak ] && echo "${pre} file backup not found" && exit 1
[ ! -e "${tmpd}"/template.dotdropbak ] && echo "${pre} template backup not found" && exit 1
[ ! -e "${tmpd}"/dir/sub.dotdropbak ] && echo "${pre} dir sub backup not found" && exit 1
[ ! -e "${tmpd}"/dir/template.dotdropbak ] && echo "${pre} dir template backup not found" && exit 1
[ ! -e "${tmpd}"/tree/file.dotdropbak ] && echo "${pre} tree file backup not found" && exit 1
[ ! -e "${tmpd}"/tree/template.dotdropbak ] && echo "${pre} tree template backup not found" && exit 1
[ ! -e "${tmpd}"/tree/sub/file.dotdropbak ] && echo "${pre} tree sub file backup not found" && exit 1
[ ! -e "${tmpd}"/tree/sub/template.dotdropbak ] && echo "${pre} tree sub template backup not found" && exit 1
grep_or_fail original "${tmpd}"/file.dotdropbak
grep_or_fail original "${tmpd}"/template.dotdropbak
grep_or_fail original "${tmpd}"/dir/sub.dotdropbak
grep_or_fail original "${tmpd}"/dir/template.dotdropbak
grep_or_fail original "${tmpd}"/tree/file.dotdropbak
grep_or_fail original "${tmpd}"/tree/template.dotdropbak
grep_or_fail original "${tmpd}"/tree/sub/file.dotdropbak
grep_or_fail original "${tmpd}"/tree/sub/template.dotdropbak
grep_or_fail p1 "${tmpd}"/template
grep_or_fail modified "${tmpd}"/dir/sub
grep_or_fail p1 "${tmpd}"/dir/template
grep_or_fail modified "${tmpd}"/tree/file
grep_or_fail p1 "${tmpd}"/tree/template
grep_or_fail modified "${tmpd}"/tree/sub/file
grep_or_fail p1 "${tmpd}"/tree/sub/template
# install relative
pre="link:relative"
create_config "relative"
clear_dotpath
clear_fs
create_dotpath
create_fs
cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 --verbose
# checks
[ ! -e "${tmpd}"/file.dotdropbak ] && echo "${pre} file backup not found" && exit 1
[ ! -e "${tmpd}"/template.dotdropbak ] && echo "${pre} template backup not found" && exit 1
grep_or_fail original "${tmpd}"/file.dotdropbak
grep_or_fail original "${tmpd}"/template.dotdropbak
grep_or_fail p1 "${tmpd}"/template
grep_or_fail modified "${tmpd}"/dir/sub
grep_or_fail p1 "${tmpd}"/dir/template
grep_or_fail modified "${tmpd}"/tree/file
grep_or_fail p1 "${tmpd}"/tree/template
grep_or_fail modified "${tmpd}"/tree/sub/file
grep_or_fail p1 "${tmpd}"/tree/sub/template
# install absolute
pre="link:absolute"
create_config "absolute"
clear_dotpath
clear_fs
create_dotpath
create_fs
cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 --verbose
# checks
[ ! -e "${tmpd}"/file.dotdropbak ] && echo "${pre} file backup not found" && exit 1
[ ! -e "${tmpd}"/template.dotdropbak ] && echo "${pre} template backup not found" && exit 1
grep_or_fail original "${tmpd}"/file.dotdropbak
grep_or_fail original "${tmpd}"/template.dotdropbak
grep_or_fail p1 "${tmpd}"/template
grep_or_fail modified "${tmpd}"/dir/sub
grep_or_fail p1 "${tmpd}"/dir/template
grep_or_fail modified "${tmpd}"/tree/file
grep_or_fail p1 "${tmpd}"/tree/template
grep_or_fail modified "${tmpd}"/tree/sub/file
grep_or_fail p1 "${tmpd}"/tree/sub/template
# install link_children
pre="link:link_children"
create_config "link_children"
clear_dotpath
clear_fs
create_dotpath
create_fs
cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 --verbose
# checks
[ ! -e "${tmpd}"/file.dotdropbak ] && echo "${pre} file backup not found" && exit 1
[ ! -e "${tmpd}"/template.dotdropbak ] && echo "${pre} template backup not found" && exit 1
[ ! -e "${tmpd}"/dir/sub.dotdropbak ] && echo "${pre} dir sub backup not found" && exit 1
[ ! -e "${tmpd}"/dir/template.dotdropbak ] && echo "${pre} dir template backup not found" && exit 1
[ ! -e "${tmpd}"/tree/file.dotdropbak ] && echo "${pre} tree file backup not found" && exit 1
[ ! -e "${tmpd}"/tree/template.dotdropbak ] && echo "${pre} tree template backup not found" && exit 1
grep_or_fail original "${tmpd}"/file.dotdropbak
grep_or_fail original "${tmpd}"/template.dotdropbak
grep_or_fail original "${tmpd}"/dir/sub.dotdropbak
grep_or_fail original "${tmpd}"/dir/template.dotdropbak
grep_or_fail original "${tmpd}"/tree/file.dotdropbak
grep_or_fail original "${tmpd}"/tree/template.dotdropbak
grep_or_fail p1 "${tmpd}"/template
grep_or_fail modified "${tmpd}"/dir/sub
grep_or_fail p1 "${tmpd}"/dir/template
grep_or_fail modified "${tmpd}"/tree/file
grep_or_fail p1 "${tmpd}"/tree/template
grep_or_fail modified "${tmpd}"/tree/sub/file
grep_or_fail p1 "${tmpd}"/tree/sub/template
echo "OK"
exit 0

103
tests-ng/chmod-install-dir.sh vendored Executable file
View File

@@ -0,0 +1,103 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2023, deadc0de6
#
# test chmod dir sub file on install
#
## start-cookie
set -euo errtrace pipefail
cur=$(cd "$(dirname "${0}")" && pwd)
ddpath="${cur}/../"
PPATH="{PYTHONPATH:-}"
export PYTHONPATH="${ddpath}:${PPATH}"
altbin="python3 -m dotdrop.dotdrop"
if hash coverage 2>/dev/null; then
mkdir -p coverages/
altbin="coverage run -p --data-file coverages/coverage --source=dotdrop -m dotdrop.dotdrop"
fi
bin="${DT_BIN:-${altbin}}"
# shellcheck source=tests-ng/helpers
source "${cur}"/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename "${BASH_SOURCE[0]}") <==$(tput sgr0)"
## end-cookie
################################################################
# this is the test
################################################################
# $1 path
# $2 rights
has_rights()
{
echo "testing ${1} is ${2}"
[ ! -e "$1" ] && echo "$(basename "$1") does not exist" && exit 1
local mode
mode=$(stat -L -c '%a' "$1")
[ "${mode}" != "$2" ] && echo "bad mode for $(basename "$1") (${mode} VS expected ${2})" && exit 1
true
}
# the dotfile source
tmps=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
mkdir -p "${tmps}"/dotfiles
# the dotfile destination
tmpd=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
#echo "dotfile destination: ${tmpd}"
clear_on_exit "${tmps}"
clear_on_exit "${tmpd}"
# create the config file
cfg="${tmps}/config.yaml"
cat > "${cfg}" << _EOF
config:
backup: true
create: true
dotpath: dotfiles
force_chmod: true
dotfiles:
d_dir:
src: dir
dst: ${tmpd}/dir
profiles:
p1:
dotfiles:
- d_dir
_EOF
#cat ${cfg}
mkdir -p "${tmps}"/dotfiles/dir
echo 'file1' > "${tmps}"/dotfiles/dir/file1
chmod 700 "${tmps}"/dotfiles/dir/file1
echo 'file2' > "${tmps}"/dotfiles/dir/file2
chmod 777 "${tmps}"/dotfiles/dir/file2
echo 'file3' > "${tmps}"/dotfiles/dir/file3
chmod 644 "${tmps}"/dotfiles/dir/file3
ls -l "${tmps}"/dotfiles/dir/
# install
echo "install (1)"
cd "${ddpath}" | ${bin} install -c "${cfg}" -f -p p1 -V
has_rights "${tmpd}/dir/file1" "700"
has_rights "${tmpd}/dir/file2" "777"
has_rights "${tmpd}/dir/file3" "644"
# modify
chmod 666 "${tmpd}/dir/file1"
chmod 666 "${tmpd}/dir/file2"
chmod 666 "${tmpd}/dir/file3"
# install
echo "install (2)"
cd "${ddpath}" | ${bin} install -c "${cfg}" -f -p p1 -V
has_rights "${tmpd}/dir/file1" "700"
has_rights "${tmpd}/dir/file2" "777"
has_rights "${tmpd}/dir/file3" "644"
echo "OK"
exit 0

View File

@@ -61,42 +61,6 @@ clear_on_exit "${tmpd}"
# create the config file
cfg="${tmps}/config.yaml"
echo 'f777' > "${tmps}"/dotfiles/f777
chmod 700 "${tmps}"/dotfiles/f777
echo 'link' > "${tmps}"/dotfiles/link
chmod 777 "${tmps}"/dotfiles/link
mkdir -p "${tmps}"/dotfiles/dir
echo "f1" > "${tmps}"/dotfiles/dir/f1
echo "exists" > "${tmps}"/dotfiles/exists
chmod 644 "${tmps}"/dotfiles/exists
echo "exists" > "${tmpd}"/exists
chmod 644 "${tmpd}"/exists
echo "existslink" > "${tmps}"/dotfiles/existslink
chmod 777 "${tmps}"/dotfiles/existslink
chmod 644 "${tmpd}"/exists
mkdir -p "${tmps}"/dotfiles/direxists
echo "f1" > "${tmps}"/dotfiles/direxists/f1
mkdir -p "${tmpd}"/direxists
echo "f1" > "${tmpd}"/direxists/f1
chmod 644 "${tmpd}"/direxists/f1
chmod 744 "${tmpd}"/direxists
mkdir -p "${tmps}"/dotfiles/linkchildren
echo "f1" > "${tmps}"/dotfiles/linkchildren/f1
mkdir -p "${tmps}"/dotfiles/linkchildren/d1
echo "f2" > "${tmps}"/dotfiles/linkchildren/d1/f2
echo '{{@@ profile @@}}' > "${tmps}"/dotfiles/symlinktemplate
mkdir -p "${tmps}"/dotfiles/symlinktemplatedir
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/symlinktemplatedir/t
echo 'nomode' > "${tmps}"/dotfiles/nomode
cat > "${cfg}" << _EOF
config:
backup: true
@@ -170,6 +134,42 @@ profiles:
_EOF
#cat ${cfg}
# create the dotfiles
echo 'f777' > "${tmps}"/dotfiles/f777
chmod 700 "${tmps}"/dotfiles/f777
echo 'link' > "${tmps}"/dotfiles/link
chmod 777 "${tmps}"/dotfiles/link
mkdir -p "${tmps}"/dotfiles/dir
echo "f1" > "${tmps}"/dotfiles/dir/f1
echo "exists" > "${tmps}"/dotfiles/exists
chmod 644 "${tmps}"/dotfiles/exists
echo "exists" > "${tmpd}"/exists
chmod 644 "${tmpd}"/exists
echo "existslink" > "${tmps}"/dotfiles/existslink
chmod 777 "${tmps}"/dotfiles/existslink
chmod 644 "${tmpd}"/exists
mkdir -p "${tmps}"/dotfiles/direxists
echo "f1" > "${tmps}"/dotfiles/direxists/f1
mkdir -p "${tmpd}"/direxists
echo "f1" > "${tmpd}"/direxists/f1
chmod 644 "${tmpd}"/direxists/f1
chmod 744 "${tmpd}"/direxists
mkdir -p "${tmps}"/dotfiles/linkchildren
echo "f1" > "${tmps}"/dotfiles/linkchildren/f1
mkdir -p "${tmps}"/dotfiles/linkchildren/d1
echo "f2" > "${tmps}"/dotfiles/linkchildren/d1/f2
echo '{{@@ profile @@}}' > "${tmps}"/dotfiles/symlinktemplate
mkdir -p "${tmps}"/dotfiles/symlinktemplatedir
echo "{{@@ profile @@}}" > "${tmps}"/dotfiles/symlinktemplatedir/t
echo 'nomode' > "${tmps}"/dotfiles/nomode
# install
echo "first install round"
cd "${ddpath}" | ${bin} install -c "${cfg}" -f -p p1 -V

View File

@@ -34,19 +34,21 @@ echo "dotfiles source (dotpath): ${tmps}"
# the dotfile destination
tmpd=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
echo "dotfiles destination: ${tmpd}"
tmptmp=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
clear_on_exit "${tmps}"
clear_on_exit "${tmpd}"
clear_on_exit "${tmptmp}"
# create the config file
cfg="${tmps}/config.yaml"
cat > "${cfg}" << _EOF
trans_read:
trans_install:
base64: "cat {0} | base64 -d > {1}"
decompress: "mkdir -p {1} && tar -xf {0} -C {1}"
decrypt: "echo {{@@ profile @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -d {0} > {1}"
trans_write:
trans_update:
base64: "cat {0} | base64 > {1}"
compress: "tar -cf {1} -C {0} ."
encrypt: "echo {{@@ profile @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -o {1} -c {0}"
@@ -90,16 +92,16 @@ cd "${ddpath}" | ${bin} import -f -c "${cfg}" -p p1 -b -V --transw=encrypt --tra
# check content in dotpath
echo "checking content"
file "${tmps}"/dotfiles/"${tmpd}"/abc | grep -i 'text'
cat "${tmpd}"/abc | base64 > "${tmps}"/test-abc
diff "${tmps}"/dotfiles/"${tmpd}"/abc "${tmps}"/test-abc
cat "${tmpd}"/abc | base64 > "${tmptmp}"/test-abc
diff "${tmps}"/dotfiles/"${tmpd}"/abc "${tmptmp}"/test-abc
file "${tmps}"/dotfiles/"${tmpd}"/def | grep -i 'tar'
tar -cf "${tmps}"/test-def -C "${tmpd}"/def .
diff "${tmps}"/dotfiles/"${tmpd}"/def "${tmps}"/test-def
tar -cf "${tmptmp}"/test-def -C "${tmpd}"/def .
diff "${tmps}"/dotfiles/"${tmpd}"/def "${tmptmp}"/test-def
file "${tmps}"/dotfiles/"${tmpd}"/ghi | grep -i 'gpg symmetrically encrypted data\|PGP symmetric key encrypted data'
echo p1 | gpg -q --batch --yes --passphrase-fd 0 --no-tty -d "${tmps}"/dotfiles/"${tmpd}"/ghi > "${tmps}"/test-ghi
diff "${tmps}"/test-ghi "${tmpd}"/ghi
echo p1 | gpg -q --batch --yes --passphrase-fd 0 --no-tty -d "${tmps}"/dotfiles/"${tmpd}"/ghi > "${tmptmp}"/test-ghi
diff "${tmptmp}"/test-ghi "${tmpd}"/ghi
# check is imported in config
echo "checking imported in config"
@@ -108,33 +110,33 @@ cd "${ddpath}" | ${bin} -p p1 -c "${cfg}" files | grep '^f_abc'
cd "${ddpath}" | ${bin} -p p1 -c "${cfg}" files | grep '^d_def'
cd "${ddpath}" | ${bin} -p p1 -c "${cfg}" files | grep '^f_ghi'
# check has trans_write and trans_read in config
echo "checking trans_write is set in config"
# check has trans_update and trans_install in config
echo "checking trans_update is set in config"
echo "--------------"
cat "${cfg}"
echo "--------------"
cat "${cfg}" | grep -A 4 'f_abc:' | grep 'trans_write: base64'
cat "${cfg}" | grep -A 4 'd_def:' | grep 'trans_write: compress'
cat "${cfg}" | grep -A 4 'f_ghi:' | grep 'trans_write: encrypt'
cat "${cfg}" | grep -A 4 'f_abc:' | grep 'trans_update: base64'
cat "${cfg}" | grep -A 4 'd_def:' | grep 'trans_update: compress'
cat "${cfg}" | grep -A 4 'f_ghi:' | grep 'trans_update: encrypt'
cat "${cfg}" | grep -A 4 'f_abc:' | grep 'trans_read: base64'
cat "${cfg}" | grep -A 4 'd_def:' | grep 'trans_read: decompress'
cat "${cfg}" | grep -A 4 'f_ghi:' | grep 'trans_read: decrypt'
cat "${cfg}" | grep -A 4 'f_abc:' | grep 'trans_install: base64'
cat "${cfg}" | grep -A 4 'd_def:' | grep 'trans_install: decompress'
cat "${cfg}" | grep -A 4 'f_ghi:' | grep 'trans_install: decrypt'
# install these
echo "install and check"
rm "${tmpd}"/abc
rm -r "${tmpd}"/def
rm "${tmpd}"/ghi
rm -rf "${tmpd:?}"/*
cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 -b -V
# test exist
echo "check exist"
[ ! -e "${tmpd}"/abc ] && exit 1
[ ! -d "${tmpd}"/def/a ] && exit 1
[ ! -e "${tmpd}"/def/a/file ] && exit 1
[ ! -e "${tmpd}"/ghi ] && exit 1
cat "${cfg}"
tree "${tmpd}"
[ ! -e "${tmpd}"/abc ] && echo "${tmpd}/abc does not exist" && exit 1
[ ! -d "${tmpd}"/def/a ] && echo "${tmpd}/def/a does not exist" && exit 1
[ ! -e "${tmpd}"/def/a/file ] && echo "${tmpd}/def/a/file does not exist" && exit 1
[ ! -e "${tmpd}"/ghi ] && echo "${tmpd}/ghi does not exist" && exit 1
# test content
echo "check content"

View File

@@ -108,8 +108,10 @@ def run_tests(max_jobs=None, stop_on_first_err=True, with_spinner=True):
failed += 1
print()
if stop_on_first_err:
print(log_out)
print(log_err)
if log_out:
print(log_out)
if log_err:
print(log_err)
print(f'test \"{name}\" failed ({ret}): {reason}')
if stop_on_first_err:
ex.shutdown(wait=False)

303
tests-ng/uninstall.sh vendored Executable file
View File

@@ -0,0 +1,303 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2023, deadc0de6
#
# test uninstall (no symlink)
# returns 1 in case of error
#
## start-cookie
set -euo errtrace pipefail
cur=$(cd "$(dirname "${0}")" && pwd)
ddpath="${cur}/../"
PPATH="{PYTHONPATH:-}"
export PYTHONPATH="${ddpath}:${PPATH}"
altbin="python3 -m dotdrop.dotdrop"
if hash coverage 2>/dev/null; then
mkdir -p coverages/
altbin="coverage run -p --data-file coverages/coverage --source=dotdrop -m dotdrop.dotdrop"
fi
bin="${DT_BIN:-${altbin}}"
# shellcheck source=tests-ng/helpers
source "${cur}"/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename "${BASH_SOURCE[0]}") <==$(tput sgr0)"
## end-cookie
################################################################
# this is the test
################################################################
# $1 pattern
# $2 path
grep_or_fail()
{
if ! grep "${1}" "${2}" >/dev/null 2>&1; then
echo "${PRE} pattern \"${1}\" not found in ${2}"
exit 1
fi
}
# $1: basedir
# $2: content
create_hierarchy()
{
echo "${2}" > "${1}"/x
mkdir -p "${1}"/y
echo "${2}" > "${1}"/y/file
mkdir -p "${1}"/y/subdir
echo "${2}" > "${1}"/y/subdir/subfile
echo "profile: ${PRO_TEMPL}" > "${1}"/t
mkdir -p "${1}"/z
echo "profile t1: ${PRO_TEMPL}" > "${1}"/z/t1
echo "profile t2: ${PRO_TEMPL}" > "${1}"/z/t2
echo "${2}" > "${1}"/z/file
echo "trans:${PRO_TEMPL}" > "${1}"/trans
}
# $1: basedir
clean_hierarchy()
{
rm -rf "${1:?}"/*
}
uninstall_with_link()
{
set -e
LINK_TYPE="${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE:-nolink}"
PRE="[link:${LINK_TYPE}] ERROR"
PRO_TEMPL="{{@@ profile @@}}"
DT_ARG="--verbose"
# dotdrop directory
basedir=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
mkdir -p "${basedir}"/dotfiles
echo "[+] dotdrop dir: ${basedir}"
echo "[+] dotpath dir: ${basedir}/dotfiles"
tmpd=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d)
tmpw=$(mktemp -d --suffix='-dotdrop-workdir' || mktemp -d)
clear_on_exit "${basedir}/dotfiles"
clear_on_exit "${tmpd}"
clear_on_exit "${tmpw}"
file_link="${LINK_TYPE}"
dir_link="${LINK_TYPE}"
if [ "${LINK_TYPE}" = "link_children" ]; then
file_link="absolute"
fi
# create the config file
cfg="${basedir}/config.yaml"
cat > "${cfg}" << _EOF
config:
backup: true
create: true
dotpath: dotfiles
link_dotfile_default: ${LINK_TYPE}
workdir: ${tmpw}
dotfiles:
f_x:
src: x
dst: ${tmpd}/x
link: ${file_link}
d_y:
src: y
dst: ${tmpd}/y
link: ${dir_link}
f_t:
src: t
dst: ${tmpd}/t
link: ${file_link}
d_z:
src: z
dst: ${tmpd}/z
link: ${dir_link}
f_trans:
src: trans
dst: ${tmpd}/trans
link: ${file_link}
profiles:
p1:
dotfiles:
- f_x
- d_y
- f_t
- d_z
- f_trans
_EOF
#########################
## no original
#########################
create_hierarchy "${basedir}/dotfiles" "modified"
# install
echo "[+] install (1)"
( \
cd "${ddpath}" && ${bin} install -c "${cfg}" -f -p p1 | grep '^4 dotfile(s) installed.$' \
)
# tests
[ ! -e "${tmpd}"/x ] && echo "${PRE} f_x not installed" && exit 1
[ ! -e "${tmpd}"/y/file ] && echo "${PRE} d_y not installed" && exit 1
[ ! -e "${tmpd}"/y/subdir/subfile ] && echo "${PRE} d_y not installed" && exit 1
[ ! -e "${tmpd}"/t ] && echo "${PRE} f_t not installed" && exit 1
[ ! -e "${tmpd}"/z/t1 ] && echo "${PRE} d_z t1 not installed" && exit 1
[ ! -e "${tmpd}"/z/t2 ] && echo "${PRE} d_z t2 not installed" && exit 1
[ ! -e "${tmpd}"/z/file ] && echo "${PRE} d_z file not installed" && exit 1
[ ! -e "${tmpd}"/trans ] && echo "${PRE} f_trans file not installed" && exit 1
grep_or_fail 'modified' "${tmpd}"/x
grep_or_fail 'modified' "${tmpd}"/y/file
grep_or_fail 'profile: p1' "${tmpd}"/t
grep_or_fail 'profile t1: p1' "${tmpd}"/z/t1
grep_or_fail 'profile t2: p1' "${tmpd}"/z/t2
grep_or_fail 'modified' "${tmpd}"/z/file
grep_or_fail 'trans:p1' "${tmpd}"/trans
# uninstall
echo "[+] uninstall (1)"
( \
cd "${ddpath}" && ${bin} uninstall -c "${cfg}" -f -p p1 "${DT_ARG}" \
)
[ "$?" != "0" ] && exit 1
# tests
[ ! -d "${basedir}"/dotfiles ] && echo "${PRE} dotpath removed" && exit 1
[ -e "${tmpd}"/x ] && echo "${PRE} f_x not uninstalled" && exit 1
[ -d "${tmpd}"/y ] && echo "${PRE} d_y dir not uninstalled" && exit 1
[ -e "${tmpd}"/y/file ] && echo "${PRE} d_y file not uninstalled" && exit 1
[ -e "${tmpd}"/y/subdir/subfile ] && echo "${PRE} d_y subfile not uninstalled" && exit 1
[ -e "${tmpd}"/t ] && echo "${PRE} f_t not uninstalled" && exit 1
[ -e "${tmpd}"/z/t1 ] && echo "${PRE} d_z subfile t1 not uninstalled" && exit 1
[ -e "${tmpd}"/z/t2 ] && echo "${PRE} d_z subfile t2 not uninstalled" && exit 1
[ -e "${tmpd}"/z/file ] && echo "${PRE} d_z subfile file not uninstalled" && exit 1
[ -e "${tmpd}"/trans ] && echo "${PRE} f_trans file not uninstalled" && exit 1
# test workdir is empty
if [ -n "$(ls -A "${tmpw}")" ]; then
echo "${PRE} workdir (1) is not empty"
echo "---"
ls -A "${tmpw}"
echo "---"
exit 1
fi
#########################
## with original
#########################
# clean
clean_hierarchy "${tmpd}"
clean_hierarchy "${basedir}"/dotfiles
# recreate
create_hierarchy "${basedir}"/dotfiles "modified"
create_hierarchy "${tmpd}" "original"
# install
echo "[+] install (2)"
cd "${ddpath}" | ${bin} install -c "${cfg}" -f -p p1 | grep '^4 dotfile(s) installed.$'
# tests
[ ! -e "${tmpd}"/x ] && echo "${PRE} f_x not installed" && exit 1
[ ! -e "${tmpd}"/x.dotdropbak ] && echo "${PRE} f_x backup not created" && exit 1
[ ! -d "${tmpd}"/y ] && echo "${PRE} d_y not installed" && exit 1
[ ! -e "${tmpd}"/y/file ] && echo "${PRE} d_y file not installed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/y/file.dotdropbak ] && echo "${PRE} d_y backup file not created" && exit 1
[ ! -e "${tmpd}"/y/subdir/subfile ] && echo "${PRE} d_y subfile not installed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/y/subdir/subfile.dotdropbak ] && echo "${PRE} d_y subfile backup not created" && exit 1
[ ! -e "${tmpd}"/t ] && echo "${PRE} f_t not installed" && exit 1
[ ! -e "${tmpd}"/t.dotdropbak ] && echo "${PRE} f_t backup not created" && exit 1
[ ! -e "${tmpd}"/z/t1 ] && echo "${PRE} d_z t1 not installed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/t1.dotdropbak ] && echo "${PRE} d_z t1 backup not created" && exit 1
[ ! -e "${tmpd}"/z/t2 ] && echo "${PRE} d_z t2 not installed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/t2.dotdropbak ] && echo "${PRE} d_z t2 backup not created" && exit 1
[ ! -e "${tmpd}"/z/file ] && echo "${PRE} d_z file not installed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/file.dotdropbak ] && echo "${PRE} d_z backup file not created" && exit 1
[ ! -e "${tmpd}"/trans ] && echo "${PRE} f_trans file not installed" && exit 1
[ ! -e "${tmpd}"/trans.dotdropbak ] && echo "${PRE} f_trans backup file not created" && exit 1
grep_or_fail 'modified' "${tmpd}"/x
grep_or_fail 'modified' "${tmpd}"/y/file
grep_or_fail 'profile: p1' "${tmpd}"/t
grep_or_fail 'profile t1: p1' "${tmpd}"/z/t1
grep_or_fail 'profile t2: p1' "${tmpd}"/z/t2
grep_or_fail 'modified' "${tmpd}"/z/file
grep_or_fail 'trans:p1' "${tmpd}"/trans
# uninstall
echo "[+] uninstall (2)"
( \
cd "${ddpath}" && ${bin} uninstall -c "${cfg}" -f -p p1 "${DT_ARG}" \
)
[ "$?" != "0" ] && exit 1
# tests
[ ! -d "${basedir}"/dotfiles ] && echo "${PRE} dotpath removed" && exit 1
[ ! -e "${tmpd}"/x ] && echo "${PRE} f_x backup not restored" && exit 1
[ -e "${tmpd}"/x.dotdropbak ] && echo "${PRE} f_x backup not removed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -d "${tmpd}"/y ] && echo "${PRE} d_y backup not restored" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/y/file ] && echo "${PRE} d_y file backup not restored" && exit 1
[ -e "${tmpd}"/y/file.dotdropbak ] && echo "${PRE} d_y backup not removed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/y/subdir/subfile ] && echo "${PRE} d_y sub backup not restored" && exit 1
[ -e "${tmpd}"/y/subdir/subfile.dotdropbak ] && echo "${PRE} d_y sub backup not removed" && exit 1
[ ! -e "${tmpd}"/t ] && echo "${PRE} f_t not restored" && exit 1
[ -e "${tmpd}"/t.dotdropbak ] && echo "${PRE} f_t backup not removed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/t1 ] && echo "${PRE} d_z t1 not restore" && exit 1
[ -e "${tmpd}"/z/t1.dotdropbak ] && echo "${PRE} d_z t1 backup not removed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/t2 ] && echo "${PRE} d_z t2 not restored" && exit 1
[ -e "${tmpd}"/z/t2.dotdropbak ] && echo "${PRE} d_z t2 backup not removed" && exit 1
[ "${LINK_TYPE}" = "nolink" ] && [ ! -e "${tmpd}"/z/file ] && echo "${PRE} d_z file not restored" && exit 1
[ -e "${tmpd}"/z/file.dotdropbak ] && echo "${PRE} d_z file backup not removed" && exit 1
[ ! -e "${tmpd}"/trans ] && echo "${PRE} f_trans backup not restored" && exit 1
[ -e "${tmpd}"/trans.dotdropbak ] && echo "${PRE} f_trans backup not removed" && exit 1
grep_or_fail 'original' "${tmpd}"/x
[ "${LINK_TYPE}" = "nolink" ] && grep_or_fail 'original' "${tmpd}"/y/file
grep_or_fail "profile: ${PRO_TEMPL}" "${tmpd}/t"
[ "${LINK_TYPE}" = "nolink" ] && grep_or_fail "profile t1: ${PRO_TEMPL}" "${tmpd}/z/t1"
[ "${LINK_TYPE}" = "nolink" ] && grep_or_fail "profile t2: ${PRO_TEMPL}" "${tmpd}/z/t2"
[ "${LINK_TYPE}" = "nolink" ] && grep_or_fail 'original' "${tmpd}"/z/file
grep_or_fail "trans:${PRO_TEMPL}" "${tmpd}"/trans
echo "testing workdir..."
# test workdir is empty
if [ -n "$(ls -A "${tmpw}")" ]; then
echo "${PRE} workdir (2) - ${tmpw} - is not empty"
ls -r "${tmpw}"
exit 1
fi
echo "${PRE} done OK"
}
export DOTDROP_TEST_NG_UNINSTALL_DDPATH="${ddpath}"
export DOTDROP_TEST_NG_UNINSTALL_BIN="${bin}"
export DOTDROP_TEST_NG_CUR="${cur}"
export DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE="nolink"
# shellcheck source=uninstall_
echo "[+] testing uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE}..."
if ! uninstall_with_link; then exit 1; fi
echo "[+] uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE} OK"
export DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE="absolute"
# shellcheck source=uninstall_
echo "[+] testing uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE}..."
if ! uninstall_with_link; then exit 1; fi
echo "[+] uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE} OK"
export DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE="relative"
# shellcheck source=uninstall_
echo "[+] testing uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE}..."
if ! uninstall_with_link; then exit 1; fi
echo "[+] uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE} OK"
export DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE="link_children"
# shellcheck source=uninstall_
echo "[+] testing uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE}..."
if ! uninstall_with_link; then exit 1; fi
echo "[+] uninstall link:${DOTDROP_TEST_NG_UNINSTALL_LINK_TYPE} OK"
echo "OK"
exit 0