i-bash/tests/glob-test
2009-09-12 16:46:49 +00:00

284 lines
3.7 KiB
Text

#
# test the shell globbing
#
expect()
{
echo expect "$@"
}
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
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 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 '<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>'
case abc in
a"b"c) echo match
;;
*) echo BAD
;;
esac
expect '<match>'
case abc in
a*c) echo match
;;
*) echo BAD
;;
esac
expect '<ok>'
case abc in
"a?c") echo bad
;;
*) echo ok
;;
esac
expect '<ok>'
case abc in
a\*c) echo bad
;;
*) echo ok
;;
esac
expect '<ok>'
case abc in
a\[b]c) echo bad
;;
*) echo ok
;;
esac
expect '<ok>'
case "$nosuchvar" in
"") echo ok ;;
*) echo bad ;;
esac
# This is very odd, but sh and ksh seem to agree
expect '<ok>'
case abc in
a["\b"]c) echo ok
;;
*) echo bad
;;
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
# none of these should output anything
case abc in
??**********?****?) echo bad ;;
esac
case abc in
??**********?****c) echo bad ;;
esac
case abc in
?************c****?****) echo bad;;
esac
case abc in
*c*?**) echo bad;;
esac
case abc in
a*****c*?**) echo bad;;
esac
case abc in
a********???*******) echo bad;;
esac
builtin cd /
rm -rf $TESTDIR
exit 0