OILS / test / parse-err-compare.sh View on Github | oilshell.org

138 lines, 54 significant
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
9set -o nounset
10set -o pipefail
11
12log() {
13 echo "$@" >& 2
14}
15
16show-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
43test-for-loop() {
44 ### test for loop errors
45
46 show-case 'for missing semi-colon' '
47for i in 1 2 do
48 echo $i
49done
50'
51
52 show-case 'for missing do' '
53for i in 1 2
54 echo $i
55done
56'
57
58 show-case 'for semi in wrong place' '
59for i in 1 2 do;
60 echo $i
61done
62'
63
64 show-case 'trying to use JS style' '
65for (i in x) {
66 echo $i
67}
68'
69}
70
71test-while-loop() {
72 ### Same thing for while loops
73
74 show-case 'while missing semi' '
75while test -f file do
76 echo $i
77done
78'
79
80 show-case 'while missing do' '
81while test -f file
82 echo $i
83done
84'
85
86 show-case 'while semi in wrong place' '
87while test -f file do;
88 echo $i
89done
90'
91}
92
93test-if() {
94 ### Same thing for if statements
95
96 show-case 'if missing semi' '
97if test -f file then
98 echo $i
99fi
100'
101
102 show-case 'if missing then' '
103if test -f file
104 echo $i
105fi
106'
107
108 show-case 'if semi in wrong place' '
109if test -f file then;
110 echo $i
111fi
112'
113}
114
115test-case() {
116 show-case 'missing ;;' '
117case $x in
118 x) echo missing
119 y) echo missing
120esac
121'
122}
123
124all() {
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"$@"