File: test_shell_best_practices.sh

package info (click to toggle)
debci 0.10.3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 904 kB
  • ctags: 310
  • sloc: sh: 1,662; ruby: 1,007; makefile: 35
file content (39 lines) | stat: -rwxr-xr-x 936 bytes parent folder | download
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
if ! which checkbashisms >/dev/null 2>&1; then
  echo "SKIP: checkbashisms not available"
  exit 0
fi

base=$(readlink -f $(dirname $0)/..)

check_shell_usage() {
  script="$1"

  failed_checks=0

  if grep -q '#!/bin/sh' $script && ! grep -q 'set -eu' $script; then
    echo "$script: no 'set -eu'!'"
    failed_checks=$(($failed_checks + 1))
  fi

  if ! checkbashisms $script; then
    failed_checks=$(($failed_checks + 1))
  fi

  return $failed_checks
}
scripts="$(cd $base && grep -l '#!/bin/sh' bin/* backends/*/*) $(cd $base && echo lib/*.sh)"
script_test_names=""

for f in $scripts lib/*.sh; do
  ff=$(echo "$f" | sed -e 's/[^a-zA-Z0-9]\+/_/g')
  script_test_names="${script_test_names} test_${ff}"
  eval "test_$ff() { check_shell_usage '$base/$f' || assertTrue \"$f shell usage problems. See messages above\" '${SHUNIT_FALSE}'; }"
done

suite() {
  for f in $script_test_names; do
    suite_addTest "$f"
  done
}

. shunit2