blob: f6215f545dcc2be00fe4608140f64dd2a2fadc5a (
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#!/bin/bash
#
# Tests for wrapper.sh.
set -euo pipefail
# The location of the script under test.
readonly WRAPPER="$(realpath "$(dirname "$0")/wrapper.sh")"
# The name of the tests to run. Each test correspond to a function in this file
# whose name is the name of the test prefixed by 'test'.
readonly TEST_NAMES=(
SuccessfulCase
FailedCase
FailedSignalCase
)
# Fails with an error message.
function fatal() {
echo 1>&2 "FATAL: $@"
exit 113
}
function withTestFiles() {
(
echo '1'
echo '2'
echo '3'
echo '4'
echo '5'
echo '6'
echo '7'
) >testfile
(
echo 'module: 1'
echo 'module: 2'
echo 'module: 3'
echo 'module: 4'
echo 'module: 5'
echo 'module: 6'
echo 'module: 7'
) >testfileWithModule
(
echo 'module: 3'
echo 'module: 4'
echo 'module: 5'
echo 'module: 6'
echo 'module: 7'
) >testfileWithModuleTruncated
}
function expectNoWrapOutput() {
if [ "$(cat output-wrap)" != '' ]; then
echo 'Wrap should not generate any output'
diff testfile output || true
return 1
fi
}
function expectSavedOutput() {
if ! diff testfile output; then
echo 'Should have saved the correct output'
diff testfile output || true
return 1
fi
}
function expectFullOutputWithModule() {
if ! diff testfileWithModule output-eval; then
echo 'Should have printed the full output'
diff testfileWithModule output || true
return 1
fi
}
function expectTruncatedOutputWithModule() {
if ! diff testfileWithModuleTruncated output-eval; then
echo 'Should have printed the truncated output'
diff testfileWithModuleTruncated output || true
return 1
fi
}
function whenWrap() {
"$WRAPPER" module "$PWD/output" "$PWD/retval" 'wrap' "$@" \
2>/dev/null \
>output-wrap
}
function whenEval() {
"$WRAPPER" module "$PWD/output" "$PWD/retval" 'eval' "$@" \
>output-eval 2>&1
}
function testSuccessfulCase() {
withTestFiles
(
echo '#!/bin/bash'
echo
echo 'cat testfile'
) >script.sh
chmod 755 script.sh
whenWrap "$PWD/script.sh"
expectNoWrapOutput
if ! whenEval; then
echo 'Should have run successfully'
return 1
fi
expectSavedOutput
expectTruncatedOutputWithModule
}
function testFailedCase() {
withTestFiles
(
echo '#!/bin/bash'
echo
echo 'cat testfile'
echo 'exit 1'
) >script.sh
chmod 755 script.sh
whenWrap "$PWD/script.sh"
expectNoWrapOutput
if whenEval; then
echo 'Should have failed to run'
return 1
fi
expectSavedOutput
expectFullOutputWithModule
}
function testFailedSignalCase() {
withTestFiles
(
echo '#!/bin/bash'
echo
echo 'cat testfile'
echo 'kill -TERM $$'
echo 'echo Should not be printed'
) >script.sh
chmod 755 script.sh
whenWrap "$PWD/script.sh"
expectNoWrapOutput
if whenEval; then
echo 'Should have failed to run'
return 1
fi
expectSavedOutput
expectFullOutputWithModule
}
function main() {
local result=0
local tmp="$(mktemp -d)"
for test_name in "${TEST_NAMES[@]}"; do
mkdir -p "$tmp/$test_name"
cd "$tmp/$test_name"
echo -n "Running $test_name..."
test"$test_name" >log || {
echo "FAILED";
sed -e "s/^/$test_name: /" <log
rm log
result=1;
continue;
}
echo "PASSED"
rm log
done
return "$result"
}
main "$@"
|