| 1 |
# shellcheck disable=SC2148 |
| 2 |
# author: deadc0de6 (https://github.com/deadc0de6) |
| 3 |
# Copyright (c) 2017, deadc0de6 |
| 4 |
# |
| 5 |
# file to be sourced from test scripts |
| 6 |
# |
| 7 |
# for i in *.sh; do ./$i >/dev/null 2>&1; find /tmp/ -maxdepth 1 -type f -iname 'tmp*' >> /tmp/$i.log; find /tmp/ -maxdepth 1 -type d -iname 'tmp.*-dotdrop-tests' >> /tmp/$i.log; find /tmp/ -maxdepth 1 -type d -iname 'dotdrop-*' >> /tmp/$i.log; wc -l /tmp/$i.log; [ "`wc -l /tmp/$i.log | awk '{print $1}'`" -gt "0" ] && break; done |
| 8 |
|
| 9 |
set -eu -o errtrace -o pipefail |
| 10 |
|
| 11 |
#declare -a to_be_cleared |
| 12 |
to_be_cleared=() |
| 13 |
|
| 14 |
# add a file/directory to be cleared |
| 15 |
# on exit |
| 16 |
# |
| 17 |
# $1: file path to clear |
| 18 |
clear_on_exit() |
| 19 |
{ |
| 20 |
local len="${#to_be_cleared[*]}" |
| 21 |
# shellcheck disable=SC2004 |
| 22 |
to_be_cleared[${len}]="$1" |
| 23 |
if [ "${len}" = "0" ]; then |
| 24 |
# set trap |
| 25 |
trap on_exit EXIT |
| 26 |
fi |
| 27 |
} |
| 28 |
|
| 29 |
# clear files |
| 30 |
on_exit() |
| 31 |
{ |
| 32 |
for i in "${to_be_cleared[@]}"; do |
| 33 |
rm -rf "${i}" |
| 34 |
done |
| 35 |
} |
| 36 |
|
| 37 |
# create a directory with sub-dirs and file |
| 38 |
# for tests |
| 39 |
# |
| 40 |
# $1: path of the parent directory to create |
| 41 |
# ret: set the variable token that allows to check |
| 42 |
create_dir() |
| 43 |
{ |
| 44 |
dirs="a/aa a/ab a/ac b/ba c" |
| 45 |
mkdir -p "${1}" |
| 46 |
for d in ${dirs}; do |
| 47 |
# create some dirs |
| 48 |
mkdir -p "${1}"/"${d}" |
| 49 |
# create some files |
| 50 |
# shellcheck disable=SC2001 |
| 51 |
fn=$(echo "${d}" | sed 's#/#-#g') |
| 52 |
f="${1}/${d}/${fn}" |
| 53 |
echo "${d}" > "${f}" |
| 54 |
# shellcheck disable=SC2034 |
| 55 |
token=${f} |
| 56 |
done |
| 57 |
} |
| 58 |
|
| 59 |
# create a clean config file |
| 60 |
# |
| 61 |
# $1: path to save to |
| 62 |
create_conf() |
| 63 |
{ |
| 64 |
cat > "${1}" << _EOF |
| 65 |
config: |
| 66 |
backup: true |
| 67 |
create: true |
| 68 |
dotpath: dotfiles |
| 69 |
dotfiles: |
| 70 |
profiles: |
| 71 |
_EOF |
| 72 |
} |
| 73 |
|
| 74 |
# osx tricks |
| 75 |
# brew install coreutils gnu-sed |
| 76 |
if [[ $OSTYPE == 'darwin'* ]]; then |
| 77 |
mktemp() { |
| 78 |
gmktemp "$@" |
| 79 |
} |
| 80 |
stat() { |
| 81 |
gstat "$@" |
| 82 |
} |
| 83 |
sed() { |
| 84 |
gsed "$@" |
| 85 |
} |
| 86 |
wc() { |
| 87 |
gwc "$@" |
| 88 |
} |
| 89 |
date() { |
| 90 |
gdate "$@" |
| 91 |
} |
| 92 |
chmod() { |
| 93 |
gchmod "$@" |
| 94 |
} |
| 95 |
readlink() { |
| 96 |
greadlink "$@" |
| 97 |
} |
| 98 |
realpath() { |
| 99 |
grealpath "$@" |
| 100 |
} |
| 101 |
|
| 102 |
export -f mktemp |
| 103 |
export -f stat |
| 104 |
export -f sed |
| 105 |
export -f wc |
| 106 |
export -f date |
| 107 |
export -f chmod |
| 108 |
export -f readlink |
| 109 |
export -f realpath |
| 110 |
fi |
| 111 |
|
| 112 |
# workdir tricks |
| 113 |
# when tests are called without using the |
| 114 |
# top level tests.sh script which sets the workdir |
| 115 |
DD_WORKDIR=${DOTDROP_WORKDIR:-} |
| 116 |
if [ -z "${DD_WORKDIR}" ]; then |
| 117 |
_workdir="/tmp/dotdrop-test-workdir" |
| 118 |
export DOTDROP_WORKDIR="${_workdir}" |
| 119 |
clear_on_exit "${_workdir}" |
| 120 |
fi |