1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # test/parse-error-compare.sh <function name>
|
5 | #
|
6 | # Example:
|
7 | # test/parse-error-compare.sh all
|
8 |
|
9 | set -o nounset
|
10 | set -o pipefail
|
11 |
|
12 | log() {
|
13 | echo "$@" >& 2
|
14 | }
|
15 |
|
16 | show-case() {
|
17 | local desc=$1
|
18 | local code=$2
|
19 |
|
20 | echo '======================'
|
21 | echo "# $desc"
|
22 | echo "$code"
|
23 | echo '----------------------'
|
24 | echo
|
25 |
|
26 | local status
|
27 |
|
28 | for sh in bash dash zsh mksh bin/osh; do
|
29 | $sh -n -c "$code"
|
30 | status=$?
|
31 |
|
32 | #echo status=$status
|
33 |
|
34 | if test $status -eq 0; then
|
35 | log "Expected non-zero status"
|
36 | exit 1
|
37 | fi
|
38 |
|
39 | echo
|
40 | done
|
41 | }
|
42 |
|
43 | test-for-loop() {
|
44 | ### test for loop errors
|
45 |
|
46 | show-case 'for missing semi-colon' '
|
47 | for i in 1 2 do
|
48 | echo $i
|
49 | done
|
50 | '
|
51 |
|
52 | show-case 'for missing do' '
|
53 | for i in 1 2
|
54 | echo $i
|
55 | done
|
56 | '
|
57 |
|
58 | show-case 'for semi in wrong place' '
|
59 | for i in 1 2 do;
|
60 | echo $i
|
61 | done
|
62 | '
|
63 |
|
64 | show-case 'trying to use JS style' '
|
65 | for (i in x) {
|
66 | echo $i
|
67 | }
|
68 | '
|
69 | }
|
70 |
|
71 | test-while-loop() {
|
72 | ### Same thing for while loops
|
73 |
|
74 | show-case 'while missing semi' '
|
75 | while test -f file do
|
76 | echo $i
|
77 | done
|
78 | '
|
79 |
|
80 | show-case 'while missing do' '
|
81 | while test -f file
|
82 | echo $i
|
83 | done
|
84 | '
|
85 |
|
86 | show-case 'while semi in wrong place' '
|
87 | while test -f file do;
|
88 | echo $i
|
89 | done
|
90 | '
|
91 | }
|
92 |
|
93 | test-if() {
|
94 | ### Same thing for if statements
|
95 |
|
96 | show-case 'if missing semi' '
|
97 | if test -f file then
|
98 | echo $i
|
99 | fi
|
100 | '
|
101 |
|
102 | show-case 'if missing then' '
|
103 | if test -f file
|
104 | echo $i
|
105 | fi
|
106 | '
|
107 |
|
108 | show-case 'if semi in wrong place' '
|
109 | if test -f file then;
|
110 | echo $i
|
111 | fi
|
112 | '
|
113 | }
|
114 |
|
115 | test-case() {
|
116 | show-case 'missing ;;' '
|
117 | case $x in
|
118 | x) echo missing
|
119 | y) echo missing
|
120 | esac
|
121 | '
|
122 | }
|
123 |
|
124 | all() {
|
125 | compgen -A function | egrep '^test-' | while read func; do
|
126 | echo "***"
|
127 | echo "*** $func"
|
128 | echo "***"
|
129 | echo
|
130 |
|
131 | $func
|
132 | echo
|
133 | echo
|
134 |
|
135 | done
|
136 | }
|
137 |
|
138 | "$@"
|