-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·287 lines (231 loc) · 6.61 KB
/
test.sh
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
set -u
stop_on_fail=false
keep_tmpfile=false
do_coverage=false
while getopts skc opt; do
case "$opt" in
s) stop_on_fail=true;;
k) keep_tmpfile=true;;
c) do_coverage=true;;
*) exit 1;;
esac
done
fsfile="$(mktemp)"
if $keep_tmpfile; then
echo "Test fsfile is $fsfile"
else
trap "rm -f $fsfile" EXIT
fi
if $do_coverage; then
jemf() { coverage run --append --branch ./jemf "$@"; }
else
jemf() { ./jemf "$@"; }
fi
export JEMF_FSFILE="$fsfile"
export __JEMF_TEST__=1
export __JEMF_TEST_PASSWORD__="hunter2"
nfail=0
npass=0
ntests=0
finish()
{
echo
echo "Ran $ntests tests; $npass passed, $nfail failed."
if $do_coverage; then
echo
echo "Coverage Report"
echo "==============="
coverage report
fi
[ "$nfail" = 0 ]
exit $?
}
runtest()
{
local status label result n invert=false
OPTIND=1
while getopts n opt; do
case "$opt" in
n) invert=true;;
*) return 1;;
esac
done
shift "$((OPTIND-1))"
label="$1"
shift 1
printf '%-60s ' "$label:"
((++ntests))
if $invert; then
! TEST_OUTPUT="$("$@" 2>&1)"
else
TEST_OUTPUT="$("$@")"
fi
status="$?"
if [ "$status" = 0 ]; then
result="pass"
((++npass))
else
result="fail"
((++nfail))
if $stop_on_fail; then
finish
fi
fi
printf '%s\n' "$result"
}
if $do_coverage; then
coverage erase
fi
runtest "mkfs" jemf mkfs -f
runtest "ls fresh FS" jemf ls
runtest "empty FS ls output empty" [ -z "$TEST_OUTPUT" ]
runtest "mkdir" jemf mkdir d
runtest "ls with single top-level entry" jemf ls
runtest "single-item ls output" [ "$TEST_OUTPUT" = "d/" ]
runtest "find on subdirectory" jemf find d
runtest "find on root" jemf find /
runtest -n "mkdir when directory exists" jemf mkdir d
runtest -n "moving a directory into itself" jemf mv d d
runtest "create file" jemf create -g L10 d/f1
runtest -n "create existing file" jemf create -g L10 d/f1
runtest -n "cat directory" jemf cat d
runtest "cat file" jemf cat d/f1
df1="$TEST_OUTPUT"
runtest "autogenerated file length" [ "${#df1}" = 10 ]
runtest "cat file in read-only mode" jemf -r cat d/f1
runtest -n "delete file in read-only mode" jemf -r rm d/f1
runtest -n "create file via read-only shell" jemf -r shell -e <<-EOF
create -g L12 nofile
EOF
datastr="foobarble bazzledorf"
runtest "create file with explicit data" jemf shell -e <<-EOF
create -D "$datastr" fb
cat fb
rm fb
EOF
runtest "explicit data respected on create" [ "$TEST_OUTPUT" = "$datastr" ]
runtest "edit file with explicit data" jemf shell -e <<-EOF
create -g L20 fb
edit -D "$datastr" fb
cat fb
rm fb
EOF
runtest "explicit data respected on edit" [ "$TEST_OUTPUT" = "$datastr" ]
runtest -n "create file with both generated and explicit data" jemf create -g L10 -D foo d/x
runtest -n "edit file with both generated and explicit data" jemf edit -g L10 -D foo d/f1
runtest -n "edit directory" jemf edit -g L10 d
runtest "data generation with punctuation character constraints" jemf shell -e <<-EOF
create -g L6:C0:m0:N0 -P . alldots
cat alldots
rm alldots
EOF
runtest "punctuation constraints respected" [ "$TEST_OUTPUT" = "......" ]
runtest "path lookup with '.'" jemf cat d/./f1
runtest "path lookup with '..'" jemf cat d/../d/f1
runtest "create symlink to file" jemf ln d/f1 l1
runtest "read file through symlink" jemf cat l1
l1="$TEST_OUTPUT"
runtest "symlink read data" [ "$l1" = "$df1" ]
runtest "rm symlink to file" jemf rm l1
runtest "create symlink to directory" jemf ln d dl
runtest "read file through directory symlink" jemf cat dl/f1
dlf1="$TEST_OUTPUT"
runtest "directory link read data" [ "$dlf1" = "$df1" ]
runtest "rm symlink to directory" jemf rm dl
runtest "create and read through chained symlink" jemf shell -e <<-EOF
ln d/f1 l1
ln l1 l2
ln l2 l3
cat l3
rm l1 l2 l3
EOF
l3data="$TEST_OUTPUT"
runtest "chained symlink read data" [ "$l3data" = "$df1" ]
runtest "create broken symlink" jemf ln missing bl
runtest -n "read from broken symlink" jemf cat bl
runtest -n "mkdir over broken symlink" jemf mkdir bl
runtest "rm broken symlink" jemf rm bl
runtest "create over broken symlink" jemf shell -e <<-EOF
ln target link
create -g L10 link
EOF
runtest "read from symlink unbroken by create" jemf cat link
linkdata="$TEST_OUTPUT"
runtest "read from target of unbroken link" jemf cat target
tgtdata="$TEST_OUTPUT"
runtest "compare data from link and target" [ "$linkdata" = "$tgtdata" ]
runtest -n "symlink over existing file" jemf ln foobar target
runtest -n "symlink over existing file in directory target" jemf ln link .
runtest -n "symlink over existing symlink" jemf ln foobar link
runtest "rm link and target" jemf rm link target
runtest "pwd at root" jemf shell -e <<-EOF
pwd
EOF
rootpwd="$TEST_OUTPUT"
runtest "pwd output at root" [ "$rootpwd" = "/" ]
runtest "pwd in subdir" jemf shell -e <<-EOF
cd d
pwd
EOF
subdirpwd="$TEST_OUTPUT"
runtest "pwd output in subdir" [ "$subdirpwd" = "/d" ]
runtest -n "mkdir when file exists" jemf mkdir d/f1
runtest "cd & mv via shell" jemf shell -e <<-EOF
cd d
mv f1 f2
EOF
runtest "cat renamed file" jemf cat d/f2
df2="$TEST_OUTPUT"
runtest "file content after rename" [ "$df2" = "$df1" ]
runtest "rename directory" jemf mv d e
runtest "cat file after directory rename" jemf cat e/f2
ef2="$TEST_OUTPUT"
runtest "file content after directory rename" [ "$ef2" = "$df2" ]
runtest "edit existing file" jemf edit -g L10 e/f2
ef2_old="$ef2"
runtest "cat edited file" jemf cat e/f2
ef2="$TEST_OUTPUT"
runtest "content of edited file" [ "$ef2" != "$ef2_old" ]
runtest "rm file" jemf rm e/f2
runtest "rm directory" jemf rm e
runtest "ls on emptied FS" jemf ls
runtest "ls output empty after emptying FS" [ -z "$TEST_OUTPUT" ]
runtest "cross-directory rename" jemf shell -e <<-EOF
mkdir d1
mkdir d2
create -g L12 d1/f
mv d1/f d2
EOF
runtest -n "cat old after cross-directory rename" jemf cat d1/f
runtest "cat new after cross-directory rename" jemf cat d2/f
runtest "mv to directory symlink" jemf shell -e <<-EOF
ln d1 dirlink
create -g L10 newfile
mv newfile dirlink
cat dirlink/newfile
EOF
fdata="$TEST_OUTPUT"
runtest "symlink into directory" jemf shell -e <<-EOF
ln /d1/newfile d2
cat d2/newfile
EOF
ldata="$TEST_OUTPUT"
runtest "verify symlink data" [ "$ldata" = "$fdata" ]
runtest -n "rename over existing file without -f" jemf shell -e <<-EOF
create -g L10 /otherfile
mv /otherfile /d1/newfile
EOF
runtest "rename over existing file with -f" jemf mv -f otherfile d1/newfile
runtest -n "rm non-empty directory" jemf rm d2
runtest "rm -r on non-empty directory" jemf rm -r d2
runtest -n "rm root directory" jemf rm /
runtest "cross-directory directory rename" jemf shell -e <<-EOF
mkdir a
mkdir b
mv b a
cd a/b
pwd
EOF
runtest "parent update on cross-directory directory rename" [ "$TEST_OUTPUT" = "/a/b" ]
finish