summaryrefslogtreecommitdiff
path: root/tests/glob-test
blob: d435708320dbc37bcb91f2dc31026c9534e5e8c6 (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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
LC_COLLATE=C
#
# test the shell globbing
#
expect()
{
	echo expect "$@"
}

# First, a test that bash-2.01.1 fails
${THIS_SH} ./glob1.sub

MYDIR=$PWD	# save where we are

TESTDIR=/tmp/glob-test
mkdir $TESTDIR
builtin cd $TESTDIR || { echo $0: cannot cd to $TESTDIR >&2 ; exit 1; }
rm -rf *

touch a b c d abc abd abe bb bcd ca cb dd de Beware
mkdir bdir

# see if `regular' globbing works right
expect '<a> <abc> <abd> <abe> <X*>'
recho a* X*

expect '<a> <abc> <abd> <abe>'
recho \a*

# see if null glob expansion works
shopt -s nullglob

expect '<a> <abc> <abd> <abe>'
recho a* X*

shopt -u nullglob

# see if the failglob option works

mkdir tmp
touch tmp/l1 tmp/l2 tmp/l3
builtin echo tmp/l[12] tmp/*4 tmp/*3
shopt -s failglob
builtin echo tmp/l[12] tmp/*4 tmp/*3
rm -r tmp
shopt -u failglob

# see if the code that expands directories only works
expect '<bdir/>'
recho b*/

# Test quoted and unquoted globbing characters
expect '<*>'
recho \*

expect '<a*>'
recho 'a*'

expect '<a*>'
recho a\*

expect '<c> <ca> <cb> <a*> <*q*>'
recho c* a\* *q*

expect '<**>'
recho "*"*

expect '<**>'
recho \**

expect '<\.\./*/>'
recho "\.\./*/"

expect '<s/\..*//>'
recho 's/\..*//'

# Pattern from Larry Wall's Configure that caused bash to blow up
expect '</^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/>'
recho "/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*"'$'"/\1/"

# Make sure character classes work properly

expect '<abc> <abd> <abe> <bb> <cb>'
recho [a-c]b*

expect '<abd> <abe> <bb> <bcd> <bdir> <ca> <cb> <dd> <de>'
recho [a-y]*[^c]

expect '<abd> <abe>'
recho a*[^c]

touch a-b aXb
expect '<a-b> <aXb>'
recho a[X-]b

touch .x .y
expect '<Beware> <d> <dd> <de>'
recho [^a-c]*

# Make sure that filenames with embedded globbing characters are handled
# properly
mkdir a\*b
> a\*b/ooo

expect '<a*b/ooo>'
recho a\*b/*

expect '<a*b/ooo>'
recho a\*?/*

expect '<no match>'
cmd='echo !7'
case "$cmd" in
*\\!*) echo match ;;
*) echo no match ;;
esac

expect '<not there>'
file='r.*'
case $file in
*.\*) echo not there ;;
*) echo there ;;
esac

# examples from the Posix.2 spec (d11.2, p. 243)
expect '<abc>'
recho a[b]c

expect '<abc>'
recho a["b"]c

expect '<abc>'
recho a[\b]c

expect '<abc>'
recho a?c

expect '<match 1>'
case abc in
a"b"c)	echo 'match 1' ;;
*)	echo 'BAD match 1' ;;
esac

expect '<match 2>'
case abc in
a*c)	echo 'match 2' ;;
*)	echo 'BAD match 2' ;;
esac

expect '<ok 1>'
case abc in
"a?c")	echo 'bad 1' ;;
*)	echo 'ok 1' ;;
esac

expect '<ok 2>'
case abc in
a\*c)	echo 'bad 2' ;;
*)	echo 'ok 2' ;;
esac

expect '<ok 3>'
case abc in
a\[b]c)	echo 'bad 3' ;;
*)	echo 'ok 3' ;;
esac

expect '<ok 4>'
case "$nosuchvar" in
"") 	echo 'ok 4' ;;
*)	echo 'bad 4' ;;
esac

# This is very odd, but sh and ksh seem to agree
expect '<ok 5>'
case abc in
a["\b"]c) echo 'ok 5' ;;
*)	echo 'bad 5' ;;
esac

mkdir man
mkdir man/man1
touch man/man1/bash.1
expect '<man/man1/bash.1>'
recho */man*/bash.*
expect '<man/man1/bash.1>'
recho $(echo */man*/bash.*)
expect '<man/man1/bash.1>'
recho "$(echo */man*/bash.*)"

# tests with multiple `*'s
case abc in
a***c)	echo ok 1;;
esac

case abc in
a*****?c)	echo ok 2;;
esac

case abc in
?*****??)	echo ok 3;;
esac

case abc in
*****??)	echo ok 4;;
esac

case abc in
*****??c)	echo ok 5;;
esac

case abc in
?*****?c)	echo ok 6;;
esac

case abc in
?***?****c)	echo ok 7;;
esac

case abc in
?***?****?)	echo ok 8;;
esac

case abc in
?***?****)	echo ok 9;;
esac

case abc in
*******c)	echo ok 10;;
esac

case abc in
*******?)	echo ok 11;;
esac

case abcdecdhjk in
a*cd**?**??k)	echo ok 20;;
esac

case abcdecdhjk in
a**?**cd**?**??k)	echo ok 21;;
esac

case abcdecdhjk in
a**?**cd**?**??k***)	echo ok 22;;
esac

case abcdecdhjk in
a**?**cd**?**??***k)	echo ok 23;;
esac

case abcdecdhjk in
a**?**cd**?**??***k**)	echo ok 24;;
esac

case abcdecdhjk in
a****c**?**??*****)	echo ok 25;;
esac

case '-' in
[-abc])	echo ok 26 ;;
esac

case '-' in
[abc-]) echo ok 27 ;;
esac

case '\' in
\\)	echo ok 28 ;;
esac

case '\' in
[\\])	echo ok 29 ;;
esac

case '\' in
'\')	echo ok 30 ;;
esac

case '[' in
[[])	echo ok 31 ;;
esac

# a `[' without a closing `]' is just another character to match, in the
# bash implementation
case '[' in
[)	echo ok 32 ;;
esac

case '[abc' in
[*)	echo 'ok 33';;
esac

# a right bracket shall lose its special meaning and represent itself in
# a bracket expression if it occurs first in the list.  -- POSIX.2 2.8.3.2
case ']' in
[]])	echo ok 34 ;;
esac

case '-' in
[]-])	echo ok 35 ;;
esac

# a backslash should just escape the next character in this context
case p in
[a-\z])	echo ok 36 ;;
esac

# this was a bug in all versions up to bash-2.04-release
case "/tmp" in
[/\\]*) echo ok 37 ;;
esac

# none of these should output anything

case abc in
??**********?****?)	echo bad 1;;
esac

case abc in
??**********?****c)	echo bad 2;;
esac

case abc in
?************c****?****)	echo bad 3;;
esac

case abc in
*c*?**)	echo bad 4;;
esac

case abc in
a*****c*?**)	echo bad 5;;
esac

case abc in
a********???*******)	echo bad 6;;
esac

case 'a' in
[])	echo bad 7 ;;
esac

case '[' in
[abc)	echo bad 8;;
esac

# let's start testing the case-insensitive globbing code
recho b*

shopt -s nocaseglob
recho b*

recho [b]*
shopt -u nocaseglob

# make sure set -f works right
set -f
recho *
set +f

# test out the GLOBIGNORE code
GLOBIGNORE='.*:*c:*e:?'
recho *

GLOBIGNORE='.*:*b:*d:?'
recho *

# see if GLOBIGNORE can substitute for `set -f'
GLOBIGNORE='.*:*'
recho *

unset GLOBIGNORE
expect '<man/man1/bash.1>'
recho */man*/bash.*

# make sure null values for GLOBIGNORE have no effect
GLOBIGNORE=
expect '<man/man1/bash.1>'
recho */man*/bash.*

# this is for the benefit of pure coverage, so it writes the pcv file
# in the right place, and for gprof
builtin cd $MYDIR

rm -rf $TESTDIR

exit 0