aboutsummaryrefslogtreecommitdiff
path: root/src/wrappers/testwrapper.in
blob: b9e10b450826cd8eda94ca89296604ce931cb5b5 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/sh

THIS=$1

ASH="ash -s"
BASH="bash --posix -s"
DASH="dash -s"
KSH="ksh -s"
POSH="posh -s"
ZSH="zsh -s"

ERROR=""

wrapper () {
    $SH -- "$@" <<-'EOF'
### common.sh

outfile=
while getopts o: opt; do
    case $opt in
    o) outfile="$OPTARG" ;;
    esac
done

shift $(($OPTIND - 1))

### postopts.sh

echo "Options passed to wrapper:"
[ -z "$outfile" ] || echo "|$outfile|"

echo "Arguments passed to wrapper:"
for arg; do
    echo "|$arg|"
done

pandoc () {
    echo "Arguments passed to wrappee:"
    for arg; do
        echo "|$arg|"
    done
}
runpandoc
EOF
}

# Portable which(1).
pathfind () {
    oldifs="$IFS"; IFS=':'
    for _p in $PATH; do
        if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
            IFS="$oldifs"
            return 0
        fi
    done
    IFS="$oldifs"
    return 1
}

check_results () {
        if [ "$1" = "$2" ]; then
            echo >&2 ok
            return 0
        else
            echo >&2 failed
            sed "s/^/\t/" >&2 <<EOF
Command line: '$3'
===> Expected:
$2
<=== Got:
$1
EOF
            return 1
        fi
}

for SH in "$BASH" "$DASH" "$KSH" "$ZSH"; do
    CMD=${SH%% *}
    echo >&2 "  Testing with $CMD..."
    if pathfind "$CMD"; then
        if [ "$CMD" = "zsh" ]; then
            # Zsh needs to be called as 'sh' to enable POSIX mode.
            ln -s $(which zsh) ./sh
            SH="./sh ${SH#* }"
            trap 'err=$?; rm -f ./sh; exit $err' 0 1 2 3 13 15
        fi

        set -e

        # Test 1
        printf >&2 "    test case 1... "
        actual=$(wrapper -o "output file" "foo bar" -A "quux baz" -B)
        expected=$(cat <<'EOF'
Options passed to wrapper:
|output file|
Arguments passed to wrapper:
|foo bar|
Arguments passed to wrappee:
|-A|
|quux baz|
|-B|
EOF
)
        check_results "$actual" "$expected" \
            'wrapper -o "output file" "foo bar" -A "quux baz" -B'

        # Test 2
        printf >&2 "    test case 2... "
        actual=$(wrapper -- -A "foo bar")
        expected=$(cat <<'EOF'
Options passed to wrapper:
Arguments passed to wrapper:
Arguments passed to wrappee:
|-A|
|foo bar|
EOF
)
        check_results "$actual" "$expected" 'wrapper -- -A "foo bar"'

        # Test 3 (Test 1 with a redundant '--')
        printf >&2 "    test case 4... "
        actual=$(wrapper -o "output file" "foo bar" -- -A "quux baz" -B)
        expected=$(cat <<'EOF'
Options passed to wrapper:
|output file|
Arguments passed to wrapper:
|foo bar|
Arguments passed to wrappee:
|-A|
|quux baz|
|-B|
EOF
)
        check_results "$actual" "$expected" \
            'wrapper -o "output file" "foo bar" -- -A "quux baz" -B'
    else
        echo >&2 "Warning: cannot verify correctness with $CMD; shell not available"
    fi
done

exit 0