1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +00:00

add more tests

This commit is contained in:
deadc0de6
2022-12-07 15:46:38 +01:00
committed by deadc0de
parent 83662006d7
commit 593d8d9a61
3 changed files with 459 additions and 6 deletions

View File

@@ -90,6 +90,7 @@ 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
@@ -99,6 +100,7 @@ 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
@@ -187,7 +189,6 @@ profiles:
p2:
dotfiles:
- f_exists
- f_existslink
- d_linkchildren
- f_symlinktemplate
- f_nomode
@@ -196,8 +197,8 @@ _EOF
# install
echo "first install round"
#cd ${ddpath} | ${bin} install -c ${cfg} -f -p p1 -V
cd ${ddpath} | ${bin} install -c ${cfg} -p p1 -V
cd ${ddpath} | ${bin} install -c ${cfg} -f -p p1 -V
echo "first install round"
has_rights "${tmpd}/f777" "777"
has_rights "${tmpd}/link" "777"
@@ -223,17 +224,15 @@ chmod 600 ${tmps}/dotfiles/exists
echo "exists" > ${tmpd}/exists
chmod 600 ${tmpd}/exists
chmod 600 ${tmpd}/existslink
chmod 700 ${tmpd}/linkchildren
chmod 600 ${tmpd}/symlinktemplate
echo "second install round"
cd ${ddpath} | ${bin} install -c ${cfg} -p p2 -f -V
echo "second install round"
has_rights "${tmpd}/exists" "777"
has_rights "${tmpd}/existslink" "777"
has_rights "${tmpd}/linkchildren/f1" "644"
has_rights "${tmpd}/linkchildren/d1" "755"
has_rights "${tmpd}/linkchildren/d1/f2" "644"

300
tests-ng/chmod-preserve-install.sh vendored Executable file
View File

@@ -0,0 +1,300 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2022, deadc0de6
#
# test chmod preserve on install
#
# exit on first error
set -e
# all this crap to get current path
rl="readlink -f"
if ! ${rl} "${0}" >/dev/null 2>&1; then
rl="realpath"
if ! hash ${rl}; then
echo "\"${rl}\" not found !" && exit 1
fi
fi
cur=$(dirname "$(${rl} "${0}")")
#hash dotdrop >/dev/null 2>&1
#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1
#echo "called with ${1}"
# dotdrop path can be pass as argument
ddpath="${cur}/../"
[ "${1}" != "" ] && ddpath="${1}"
[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1
export PYTHONPATH="${ddpath}:${PYTHONPATH}"
bin="python3 -m dotdrop.dotdrop"
hash coverage 2>/dev/null && bin="coverage run -a --source=dotdrop -m dotdrop.dotdrop" || true
echo "dotdrop path: ${ddpath}"
echo "pythonpath: ${PYTHONPATH}"
# get the helpers
source ${cur}/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
################################################################
# 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=`stat -L -c '%a' "$1"`
[ "${mode}" != "$2" ] && echo "bad mode for `basename $1` (${mode} VS expected ${2})" && exit 1
true
}
# test $1 path has same right than $2
is_same_as()
{
echo "testing ${1} has same rights than ${2}"
[ ! -e "$1" ] && echo "`basename $1` does not exist" && exit 1
[ ! -e "$2" ] && echo "`basename $2` does not exist" && exit 1
local mode1=`stat -L -c '%a' "$1"`
echo "$1: ${mode1}"
local mode2=`stat -L -c '%a' "$2"`
echo "$2: ${mode2}"
[ "${mode1}" != "${mode2}" ] && echo "`basename $1` (${mode1}) does not have same mode as `basename $2` (${mode2})" && exit 1
true
}
get_default_file_mode()
{
u=`umask`
u=`echo ${u} | sed 's/^0*//'`
v=$((666 - u))
echo "${v}"
}
get_default_dir_mode()
{
u=`umask`
u=`echo ${u} | sed 's/^0*//'`
v=$((777 - u))
echo "${v}"
}
# 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"
##
# non existing files
##
# file
echo 'f777' > ${tmps}/dotfiles/f777
chmod 700 ${tmps}/dotfiles/f777
# link
echo 'link' > ${tmps}/dotfiles/link
chmod 700 ${tmps}/dotfiles/link
# directory
mkdir -p ${tmps}/dotfiles/dir
echo "f1" > ${tmps}/dotfiles/dir/f1
chmod 700 ${tmps}/dotfiles/dir
chmod 700 ${tmps}/dotfiles/dir/f1
# template
echo '{{@@ profile @@}}' > ${tmps}/dotfiles/template
chmod 700 ${tmps}/dotfiles/template
# link template
echo '{{@@ profile @@}}' > ${tmps}/dotfiles/link-template
chmod 700 ${tmps}/dotfiles/link-template
##
# existing files
##
# file
echo "exists-original" > ${tmps}/dotfiles/exists
chmod 644 ${tmps}/dotfiles/exists
echo "exists" > ${tmpd}/exists
chmod 700 ${tmpd}/exists
# link
echo "existslink" > ${tmps}/dotfiles/existslink
chmod 700 ${tmps}/dotfiles/existslink
ln -s ${tmps}/dotfiles/existslink ${tmpd}/existslink
# directory
mkdir -p ${tmps}/dotfiles/direxists
echo "f1-original" > ${tmps}/dotfiles/direxists/f1
mkdir -p ${tmpd}/direxists
echo "f1" > ${tmpd}/direxists/f1
chmod 700 ${tmpd}/direxists/f1
chmod 700 ${tmpd}/direxists
# link children
mkdir -p ${tmps}/dotfiles/linkchildren
echo "f1-original" > ${tmps}/dotfiles/linkchildren/f1
chmod 700 ${tmps}/dotfiles/linkchildren/f1
mkdir -p ${tmps}/dotfiles/linkchildren/d1
chmod 700 ${tmps}/dotfiles/linkchildren/d1
echo "f2-original" > ${tmps}/dotfiles/linkchildren/d1/f2
chmod 700 ${tmps}/dotfiles/linkchildren/d1/f2
mkdir -p ${tmpd}/linkchildren
chmod 700 ${tmpd}/linkchildren
echo "f1" > ${tmpd}/linkchildren/f1
mkdir -p ${tmpd}/linkchildren/d1
echo "f2" > ${tmpd}/linkchildren/d1/f2
# no mode
echo 'nomode-original' > ${tmps}/dotfiles/nomode
echo 'nomode' > ${tmpd}/nomode
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
force_chmod: true
dotfiles:
f_f777:
src: f777
dst: ${tmpd}/f777
chmod: preserve
f_link:
src: link
dst: ${tmpd}/link
chmod: preserve
link: absolute
d_dir:
src: dir
dst: ${tmpd}/dir
chmod: preserve
f_template:
src: template
dst: ${tmpd}/template
chmod: preserve
f_link_template:
src: link-template
dst: ${tmpd}/link-template
chmod: preserve
f_exists:
src: exists
dst: ${tmpd}/exists
chmod: preserve
f_existslink:
src: existslink
dst: ${tmpd}/existslink
chmod: preserve
link: absolute
d_direxists:
src: direxists
dst: ${tmpd}/direxists
chmod: preserve
d_linkchildren:
src: linkchildren
dst: ${tmpd}/linkchildren
chmod: preserve
link: link_children
f_nomode:
src: nomode
dst: ${tmpd}/nomode
chmod: preserve
profiles:
p1:
dotfiles:
- f_f777
- f_link
- d_dir
- f_template
- f_link_template
- f_exists
- f_existslink
- d_direxists
- d_linkchildren
- f_nomode
_EOF
#cat ${cfg}
exists_before=`stat -L -c '%a' "${tmpd}/exists"`
direxists_before=`stat -L -c '%a' "${tmpd}/direxists"`
direxists_f1_before=`stat -L -c '%a' "${tmpd}/direxists/f1"`
# install
echo "first round"
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
echo "first round"
# non-existing but will create with "default" rights on preserve
# 644 for file
# 755 for directory
# link will get the rights of the file it points to
has_rights "${tmpd}/f777" "`get_default_file_mode`"
has_rights "${tmpd}/link" "700"
has_rights "${tmpd}/dir" "`get_default_dir_mode`"
has_rights "${tmpd}/template" "`get_default_file_mode`"
# first install to workdir (def rights) and then symlink
has_rights "${tmpd}/link-template" "644"
[ -L "${tmpd}/link-template" ] && echo "link-template is not a symlink" && exit 1
# existing
has_rights "${tmpd}/exists" "700"
has_rights "${tmpd}/exists" "${exists_before}"
has_rights "${tmpd}/existslink" "700" # points back to dotpath
is_same_as "${tmpd}/existslink" "${tmps}/dotfiles/existslink"
has_rights "${tmpd}/direxists" "700"
has_rights "${tmpd}/direxists" "${direxists_before}"
has_rights "${tmpd}/direxists/f1" "700"
has_rights "${tmpd}/direxists/f1" "${direxists_f1_before}"
has_rights "${tmpd}/linkchildren" "700" # default for new directory
has_rights "${tmpd}/linkchildren/f1" "700" # points back to dotpath
has_rights "${tmpd}/linkchildren/d1" "700" # points back to dotpath
has_rights "${tmpd}/linkchildren/d1/f2" "700"
# modify
echo 'f777-2' >> ${tmps}/dotfiles/f777
chmod 701 ${tmps}/dotfiles/f777
echo 'link-2' >> ${tmps}/dotfiles/link
chmod 701 ${tmps}/dotfiles/link
echo "f1-2" >> ${tmps}/dotfiles/dir/f1
chmod 701 ${tmps}/dotfiles/dir
chmod 701 ${tmps}/dotfiles/dir/f1
f777_before=`stat -L -c '%a' "${tmpd}/f777"`
link_before=`stat -L -c '%a' "${tmpd}/link"`
dir_before=`stat -L -c '%a' "${tmpd}/dir"`
echo "second round"
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
echo "second round"
# existing
has_rights "${tmpd}/f777" "${f777_before}"
has_rights "${tmpd}/link" "${link_before}"
has_rights "${tmpd}/dir" "${dir_before}"
echo "OK"
exit 0

154
tests-ng/chmod-preserve-update.sh vendored Executable file
View File

@@ -0,0 +1,154 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2022, deadc0de6
#
# test chmod preserve on update
#
# exit on first error
set -e
# all this crap to get current path
rl="readlink -f"
if ! ${rl} "${0}" >/dev/null 2>&1; then
rl="realpath"
if ! hash ${rl}; then
echo "\"${rl}\" not found !" && exit 1
fi
fi
cur=$(dirname "$(${rl} "${0}")")
#hash dotdrop >/dev/null 2>&1
#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1
#echo "called with ${1}"
# dotdrop path can be pass as argument
ddpath="${cur}/../"
[ "${1}" != "" ] && ddpath="${1}"
[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1
export PYTHONPATH="${ddpath}:${PYTHONPATH}"
bin="python3 -m dotdrop.dotdrop"
hash coverage 2>/dev/null && bin="coverage run -a --source=dotdrop -m dotdrop.dotdrop" || true
echo "dotdrop path: ${ddpath}"
echo "pythonpath: ${PYTHONPATH}"
# get the helpers
source ${cur}/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
################################################################
# this is the test
################################################################
# 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}"
##
# existing files
##
# file
echo "exists-original" > ${tmps}/dotfiles/exists
chmod 644 ${tmps}/dotfiles/exists
echo "exists" > ${tmpd}/exists
chmod 700 ${tmpd}/exists
# link
echo "existslink" > ${tmps}/dotfiles/existslink
chmod 700 ${tmps}/dotfiles/existslink
ln -s ${tmps}/dotfiles/existslink ${tmpd}/existslink
# directory
mkdir -p ${tmps}/dotfiles/direxists
echo "f1-original" > ${tmps}/dotfiles/direxists/f1
mkdir -p ${tmpd}/direxists
echo "f1" > ${tmpd}/direxists/f1
chmod 700 ${tmpd}/direxists/f1
chmod 700 ${tmpd}/direxists
# link children
mkdir -p ${tmps}/dotfiles/linkchildren
echo "f1-original" > ${tmps}/dotfiles/linkchildren/f1
chmod 700 ${tmps}/dotfiles/linkchildren/f1
mkdir -p ${tmps}/dotfiles/linkchildren/d1
chmod 700 ${tmps}/dotfiles/linkchildren/d1
echo "f2-original" > ${tmps}/dotfiles/linkchildren/d1/f2
chmod 700 ${tmps}/dotfiles/linkchildren/d1/f2
mkdir -p ${tmpd}/linkchildren
chmod 700 ${tmpd}/linkchildren
echo "f1" > ${tmpd}/linkchildren/f1
mkdir -p ${tmpd}/linkchildren/d1
echo "f2" > ${tmpd}/linkchildren/d1/f2
# no mode
echo 'nomode-original' > ${tmps}/dotfiles/nomode
echo 'nomode' > ${tmpd}/nomode
# create the config file
cfg="${tmps}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
force_chmod: true
dotfiles:
f_exists:
src: exists
dst: ${tmpd}/exists
chmod: preserve
f_existslink:
src: existslink
dst: ${tmpd}/existslink
chmod: preserve
link: absolute
d_direxists:
src: direxists
dst: ${tmpd}/direxists
chmod: preserve
d_linkchildren:
src: linkchildren
dst: ${tmpd}/linkchildren
chmod: preserve
link: link_children
f_nomode:
src: nomode
dst: ${tmpd}/nomode
chmod: preserve
profiles:
p1:
dotfiles:
- f_exists
- f_existslink
- d_direxists
- d_linkchildren
- f_nomode
_EOF
#cat ${cfg}
echo "update"
cd ${ddpath} | ${bin} update -f -c ${cfg} -p p1 -V ${tmpd}/exists
cd ${ddpath} | ${bin} update -f -c ${cfg} -p p1 -V ${tmpd}/existslink
cd ${ddpath} | ${bin} update -f -c ${cfg} -p p1 -V ${tmpd}/direxists
cd ${ddpath} | ${bin} update -f -c ${cfg} -p p1 -V ${tmpd}/linkchildren
cd ${ddpath} | ${bin} update -f -c ${cfg} -p p1 -V ${tmpd}/nomode
count=$(cat ${cfg} | grep chmod | grep -v 'chmod: preserve\|force_chmod' | wc -l)
echo "${count}"
[ "${count}" != "0" ] && echo "chmod altered" && exit 1
echo "OK"
exit 0