|
| 1 | +#!/usr/bin/env bash |
| 2 | +# author: deadc0de6 (https://github.com/deadc0de6) |
| 3 | +# Copyright (c) 2023, deadc0de6 |
| 4 | +# |
| 5 | +# test import_variables |
| 6 | +# returns 1 in case of error |
| 7 | +# see issue 380 |
| 8 | +# |
| 9 | + |
| 10 | +# exit on first error |
| 11 | +set -e |
| 12 | + |
| 13 | +# all this crap to get current path |
| 14 | +rl="readlink -f" |
| 15 | +if ! ${rl} "${0}" >/dev/null 2>&1; then |
| 16 | + rl="realpath" |
| 17 | + |
| 18 | + if ! hash ${rl}; then |
| 19 | + echo "\"${rl}\" not found !" && exit 1 |
| 20 | + fi |
| 21 | +fi |
| 22 | +cur=$(dirname "$(${rl} "${0}")") |
| 23 | + |
| 24 | +#hash dotdrop >/dev/null 2>&1 |
| 25 | +#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1 |
| 26 | + |
| 27 | +#echo "called with ${1}" |
| 28 | + |
| 29 | +# dotdrop path can be pass as argument |
| 30 | +ddpath="${cur}/../" |
| 31 | +[ "${1}" != "" ] && ddpath="${1}" |
| 32 | +[ ! -d "${ddpath}" ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1 |
| 33 | + |
| 34 | +export PYTHONPATH="${ddpath}:${PYTHONPATH}" |
| 35 | +bin="python3 -m dotdrop.dotdrop" |
| 36 | +if hash coverage 2>/dev/null; then |
| 37 | + bin="coverage run -p --source=dotdrop -m dotdrop.dotdrop" |
| 38 | +fi |
| 39 | + |
| 40 | +echo "dotdrop path: ${ddpath}" |
| 41 | +echo "pythonpath: ${PYTHONPATH}" |
| 42 | + |
| 43 | +# get the helpers |
| 44 | +# shellcheck source=tests-ng/helpers |
| 45 | +source "${cur}"/helpers |
| 46 | + |
| 47 | +echo -e "$(tput setaf 6)==> RUNNING $(basename "${BASH_SOURCE[0]}") <==$(tput sgr0)" |
| 48 | + |
| 49 | +################################################################ |
| 50 | +# this is the test |
| 51 | +################################################################ |
| 52 | + |
| 53 | +# the dotfile source |
| 54 | +tmps=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d) |
| 55 | +mkdir -p "${tmps}"/dotfiles |
| 56 | +# the dotfile destination |
| 57 | +tmpd=$(mktemp -d --suffix='-dotdrop-tests' || mktemp -d) |
| 58 | + |
| 59 | +clear_on_exit "${tmps}" |
| 60 | +clear_on_exit "${tmpd}" |
| 61 | + |
| 62 | +# create the config file |
| 63 | +cfg="${tmps}/config.yaml" |
| 64 | +cfgvar1="${tmps}/var1.yaml" |
| 65 | +cfgvar2="${tmps}/var2.yaml" |
| 66 | + |
| 67 | +cat << _EOF > "${tmps}/dotfiles/abc" |
| 68 | +var1: {{@@ var1 @@}} |
| 69 | +var2: {{@@ var2 @@}} |
| 70 | +var3: {{@@ var3 @@}} |
| 71 | +var4: {{@@ var4 @@}} |
| 72 | +var5: {{@@ var5 @@}} |
| 73 | +var6: {{@@ var6 @@}} |
| 74 | +_EOF |
| 75 | + |
| 76 | +cat > "${cfg}" << _EOF |
| 77 | +config: |
| 78 | + backup: true |
| 79 | + create: true |
| 80 | + dotpath: dotfiles |
| 81 | + import_variables: |
| 82 | + - ${cfgvar1} |
| 83 | + - ${cfgvar2} |
| 84 | +variables: |
| 85 | + var1: "this is var1 from main config" |
| 86 | + var2: "this is var2 from main config" |
| 87 | + var3: "this is var3 from main config" |
| 88 | +dotfiles: |
| 89 | + f_abc: |
| 90 | + dst: ${tmpd}/abc |
| 91 | + src: 'abc' |
| 92 | +profiles: |
| 93 | + p1: |
| 94 | + dotfiles: |
| 95 | + - f_abc |
| 96 | +_EOF |
| 97 | +echo "main config: ${cfg}" |
| 98 | +cat "${cfg}" |
| 99 | + |
| 100 | +cat << _EOF > "${cfgvar1}" |
| 101 | +variables: |
| 102 | + var2: "this is var2 from sub1" |
| 103 | + var3: "this is var3 from sub1" |
| 104 | + var4: "this is var4 from sub1" |
| 105 | + var5: "this is var5 from sub1" |
| 106 | +_EOF |
| 107 | +echo "cfgvar1: ${cfgvar1}" |
| 108 | +cat "${cfgvar1}" |
| 109 | + |
| 110 | +cat << _EOF > "${cfgvar2}" |
| 111 | +variables: |
| 112 | + var3: "this is var3 from sub2" |
| 113 | + var4: "this is var4 from sub2" |
| 114 | + var6: "this is var6 from sub2" |
| 115 | +_EOF |
| 116 | +echo "cfgvar2: ${cfgvar2}" |
| 117 | +cat "${cfgvar2}" |
| 118 | + |
| 119 | +# install |
| 120 | +cd "${ddpath}" | ${bin} install -f -c "${cfg}" -p p1 --verbose |
| 121 | + |
| 122 | +# test file existence |
| 123 | +[ -f "${tmpd}/abc" ] || { |
| 124 | + echo 'Dotfile not installed' |
| 125 | + exit 1 |
| 126 | +} |
| 127 | + |
| 128 | +# test file content |
| 129 | +cat "${tmpd}"/abc |
| 130 | +echo "----------------------" |
| 131 | +grep '^var1: this is var1 from main config$' "${tmpd}"/abc >/dev/null |
| 132 | +echo "var1 ok" |
| 133 | +grep '^var2: this is var2 from sub1$' "${tmpd}"/abc >/dev/null |
| 134 | +echo "var2 ok" |
| 135 | +grep '^var3: this is var3 from sub2$' "${tmpd}"/abc >/dev/null |
| 136 | +echo "var3 ok" |
| 137 | +grep '^var4: this is var4 from sub2$' "${tmpd}"/abc >/dev/null |
| 138 | +echo "var4 ok" |
| 139 | +grep '^var5: this is var5 from sub1$' "${tmpd}"/abc >/dev/null |
| 140 | +echo "var5 ok" |
| 141 | +grep '^var6: this is var6 from sub2$' "${tmpd}"/abc >/dev/null |
| 142 | +echo "var6 ok" |
| 143 | + |
| 144 | +echo "OK" |
| 145 | +exit 0 |
0 commit comments