File: helpers

package info (click to toggle)
dotdrop 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: sh: 13,401; python: 8,186; makefile: 3
file content (120 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# shellcheck disable=SC2148
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2017, deadc0de6
#
# file to be sourced from test scripts
#
# 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

set -eu -o errtrace -o pipefail

#declare -a to_be_cleared
to_be_cleared=()

# add a file/directory to be cleared
# on exit
#
# $1: file path to clear
clear_on_exit()
{
  local len="${#to_be_cleared[*]}"
  # shellcheck disable=SC2004
  to_be_cleared[${len}]="$1"
  if [ "${len}" = "0" ]; then
    # set trap
    trap on_exit EXIT
  fi
}

# clear files
on_exit()
{
  for i in "${to_be_cleared[@]}"; do
    rm -rf "${i}"
  done
}

# create a directory with sub-dirs and file
# for tests
#
# $1: path of the parent directory to create
# ret: set the variable token that allows to check
create_dir()
{
  dirs="a/aa a/ab a/ac b/ba c"
  mkdir -p "${1}"
  for d in ${dirs}; do
    # create some dirs
    mkdir -p "${1}"/"${d}"
    # create some files
    # shellcheck disable=SC2001
    fn=$(echo "${d}" | sed 's#/#-#g')
    f="${1}/${d}/${fn}"
    echo "${d}" > "${f}"
    # shellcheck disable=SC2034
    token=${f}
  done
}

# create a clean config file
#
# $1: path to save to
create_conf()
{
  cat > "${1}" << _EOF
config:
  backup: true
  create: true
  dotpath: dotfiles
dotfiles:
profiles:
_EOF
}

# osx tricks
# brew install coreutils gnu-sed
if [[ $OSTYPE == 'darwin'* ]]; then
  mktemp() {
    gmktemp "$@"
  }
  stat() {
    gstat "$@"
  }
  sed() {
    gsed "$@"
  }
  wc() {
    gwc "$@"
  }
  date() {
    gdate "$@"
  }
  chmod() {
    gchmod "$@"
  }
  readlink() {
    greadlink "$@"
  }
  realpath() {
    grealpath "$@"
  }

  export -f mktemp
  export -f stat
  export -f sed
  export -f wc
  export -f date
  export -f chmod
  export -f readlink
  export -f realpath
fi

# workdir tricks
# when tests are called without using the
# top level tests.sh script which sets the workdir
DD_WORKDIR=${DOTDROP_WORKDIR:-}
if [ -z "${DD_WORKDIR}" ]; then
  _workdir="/tmp/dotdrop-test-workdir"
  export DOTDROP_WORKDIR="${_workdir}"
  clear_on_exit "${_workdir}"
fi