tests: check init.php for syntax errors
[racktables] / tests / express.sh
1 #!/bin/sh
2
3 THISDIR=`dirname $0`
4 BASEDIR=`readlink -f "$THISDIR/.."`
5 : ${PHPUNIT_BIN:=phpunit}
6
7 case `"$PHPUNIT_BIN" --version` in
8 'PHPUnit 4.'*|'PHPUnit 5.'*)
9 BOOTSTRAP_FILE=bootstrap_v4v5.php
10 ;;
11 'PHPUnit 6.'*|'PHPUnit 7.'*)
12 BOOTSTRAP_FILE=bootstrap_v6v7.php
13 ;;
14 *)
15 echo 'ERROR: failed to find a known version of PHPUnit'
16 "$PHPUNIT_BIN" --version
17 exit 5
18 esac
19
20 echo "Running express tests using the base directory '$BASEDIR'"
21 echo "and PHPUnit bootstrap file '$BOOTSTRAP_FILE'."
22
23 testPHPSyntaxOnly()
24 {
25 local FORMAT="${1:?}"
26 local INPUT="${2:?}"
27
28 if php --syntax-check "$INPUT" >/dev/null 2>&1; then
29 printf "$FORMAT" "$INPUT" 'OK (syntax only)'
30 return 0
31 else
32 printf "$FORMAT" "$INPUT" "ERROR: PHP syntax check failed"
33 return 1
34 fi
35 }
36
37 testPHPExitCodeAndOutput()
38 {
39 local FORMAT="${1:?}"
40 local INPUT="${2:?}"
41 local TEMPFILE="${3:?}"
42 local fname rc curdir
43
44 fname=`basename "$INPUT"`
45 curdir=`pwd`
46 cd `dirname "$INPUT"`
47 php "$fname" > "$TEMPFILE"
48 rc=$?
49 cd "$curdir"
50 if [ $rc -eq 0 -a ! -s "$TEMPFILE" ]; then
51 printf "$FORMAT" "$INPUT" 'OK'
52 return 0
53 else
54 [ $rc -ne 0 ] && printf "$FORMAT" "$INPUT" "ERROR: PHP interpreter returned code $rc"
55 [ -s "$TEMPFILE" ] && printf "$FORMAT" "$f" 'ERROR: produces output when parsed'
56 return 1
57 fi
58 }
59
60 # Every file in wwwroot/inc/ must be a valid PHP input file and must not
61 # produce any output when parsed by PHP (because, for instance, a plain text
62 # file is a valid PHP input file).
63 echo
64 cd "$BASEDIR"
65 files=0
66 errors=0
67 TEMPFILE=`mktemp /tmp/racktables_unittest.XXXXXX`
68 FORMAT='%-50s : %s\n'
69 for f in wwwroot/inc/*.php plugins/*/plugin.php; do
70 if [ "$f" = "wwwroot/inc/init.php" ]; then
71 testPHPSyntaxOnly "$FORMAT" "$f" || errors=`expr $errors + 1`
72 else
73 testPHPExitCodeAndOutput "$FORMAT" "$f" "$TEMPFILE" || errors=`expr $errors + 1`
74 fi
75 files=`expr $files + 1`
76 done
77 for f in tests/*.php; do
78 testPHPSyntaxOnly "$FORMAT" "$f" || errors=`expr $errors + 1`
79 files=`expr $files + 1`
80 done
81 echo '---------------------------------------------------'
82 echo "Files parsed: $files, failed: $errors"
83 rm -f "$TEMPFILE"
84 [ $errors -eq 0 ] || exit 1
85
86 # The command-line scripts among other things prove that init.php actually works.
87 echo
88 cd "$BASEDIR/wwwroot"
89 echo 'Testing syncdomain.php'; ../scripts/syncdomain.php --help || exit 1
90 echo 'Testing cleanup_ldap_cache.php'; ../scripts/cleanup_ldap_cache.php || exit 1
91 echo 'Testing reload_dictionary.php'; ../scripts/reload_dictionary.php || exit 1
92
93 # At this point it makes sense to test specific functions.
94 echo
95 cd "$BASEDIR/tests"
96 "$PHPUNIT_BIN" --group small --bootstrap $BOOTSTRAP_FILE || exit 1