# Commands covered:  glob
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# @(#) glob.test 1.28 94/12/17 16:19:59

if {[string compare test [info procs test]] == 1} then {source defs}

# First, create some subdirectories to use for testing.

exec rm -rf globTest
exec mkdir globTest globTest/a1 globTest/a2 globTest/a3
exec mkdir globTest/a1/b1 globTest/a1/b2 globTest/a2/b3
exec cat << abc > globTest/x1.c
exec cat << abc > globTest/y1.c
exec cat << abc > globTest/z1.c
exec cat << abc > "globTest/weird name.c"
exec cat << abc > globTest/.1
exec cat << abc > globTest/a1/b1/x2.c
exec cat << abc > globTest/a1/b2/y2.c

test glob-1.1 {simple globbing} {
    lsort [glob globTest/x1.c globTest/y1.c globTest/foo]
} {globTest/x1.c globTest/y1.c}
test glob-1.2 {simple globbing} {
    glob {}
} .

test glob-2.1 {globbing with braces} {
    glob -nocomplain "{a1,a2}"
} {}
test glob-2.2 {globbing with braces} {
    lsort [glob globTest/{a,b,x,y}1.c]
} {globTest/x1.c globTest/y1.c}
test glob-2.3 {globbing with braces} {
    lsort [glob {globTest/{x1,y2,weird name}.c}]
} {{globTest/weird name.c} globTest/x1.c}
test glob-2.4 {globbing with braces} {
    lsort [glob globTest/{x1.c,a1/*}]
} {globTest/a1/b1 globTest/a1/b2 globTest/x1.c}

test glob-3.1 {asterisks, question marks, and brackets} {
    lsort [glob g*/*.c]
} {{globTest/weird name.c} globTest/x1.c globTest/y1.c globTest/z1.c}
test glob-3.2 {asterisks, question marks, and brackets} {
    lsort [glob globTest/?1.c]
} {globTest/x1.c globTest/y1.c globTest/z1.c}
test glob-3.3 {asterisks, question marks, and brackets} {
    lsort [glob */*/*/*.c]
} {globTest/a1/b1/x2.c globTest/a1/b2/y2.c}
test glob-3.4 {asterisks, question marks, and brackets} {
    lsort [glob globTest/*]
} {globTest/a1 globTest/a2 globTest/a3 {globTest/weird name.c} globTest/x1.c globTest/y1.c globTest/z1.c}
test glob-3.5 {asterisks, question marks, and brackets} {
    lsort [glob globTest/.*]
} {globTest/. globTest/.. globTest/.1}
test glob-3.6 {asterisks, question marks, and brackets} {
    lsort [glob globTest/*/*]
} {globTest/a1/b1 globTest/a1/b2 globTest/a2/b3}
test glob-3.7 {asterisks, question marks, and brackets} {
    lsort [glob {globTest/[xyab]1.*}]
} {globTest/x1.c globTest/y1.c}
test glob-3.8 {asterisks, question marks, and brackets} {
    lsort [glob globTest/*/]
} {globTest/a1/ globTest/a2/ globTest/a3/}

# The tests below are not portable, since they depend on a
# particular location for ouster's home directory.

if $doNonPortableTests {
    test glob-4.1 {tildes} {glob ~/.csh*} "/home/ouster/.cshrc"
    test glob-4.2 {tildes} {glob ~ouster/.csh*} "/home/ouster/.cshrc"
} 

test glob-5.1 {error conditions} {
    list [catch {glob} msg] $msg
} {1 {wrong # args: should be "glob ?switches? name ?name ...?"}}
test glob-5.2 {error conditions} {
    list [catch {glob globTest/\{} msg] $msg
} {1 {unmatched open-brace in file name}}
test glob-5.3 {error conditions} {
    list [catch {glob globTest/*/gorp} msg] $msg
} {1 {no files matched glob pattern "globTest/*/gorp"}}
test glob-5.4 {error conditions} {
    list [catch {glob goo/* x*z foo?q} msg] $msg
} {1 {no files matched glob patterns "goo/* x*z foo?q"}}
test glob-5.5 {error conditions} {
    list [catch {lsort [glob globTest/*.c goo/*]} msg] $msg
} {0 {{globTest/weird name.c} globTest/x1.c globTest/y1.c globTest/z1.c}}
test glob-5.6 {error conditions} {
    list [catch {glob ~no-one} msg] $msg
} {1 {user "no-one" doesn't exist}}
test glob-5.7 {error conditions} {
    set home $env(HOME)
    unset env(HOME)
    set x [list [catch {glob ~/*} msg] $msg]
    set env(HOME) $home
    set x
} {1 {couldn't find HOME environment variable to expand "~/*"}}
test glob-5.8 {error conditions} {
    list [catch {glob globTest/{a1,a2}/\{} msg] $msg
} {1 {unmatched open-brace in file name}}
test glob-5.9 {error conditions} {
    list [catch {glob globTest/*/\{} msg] $msg
} {1 {unmatched open-brace in file name}}

# On some systems, like AFS, "000" protection doesn't prevent
# access by owner, so the following test is not portable.

if $doNonPortableTests {
    exec chmod 000 globTest
    test glob-6.1 {setting errorCode variable} {
	string tolower [list [catch {glob globTest/*} msg]  $msg $errorCode]
    } {1 {couldn't read directory "globtest": permission denied} {posix eacces {permission denied}}}
    exec chmod 755 globTest
}

test glob-7.1 {-nocomplain switch} {
    list [catch {glob -nocomplai} msg] $msg
} {1 {bad switch "-nocomplai": must be -nocomplain or --}}
test glob-7.2 {-nocomplain switch} {
    list [catch {glob -nocomplain} msg] $msg
} {1 {wrong # args: should be "glob ?switches? name ?name ...?"}}
test glob-7.3 {-nocomplain switch} {
    list [catch {glob -nocomplain goo/*} msg] $msg
} {0 {}}
test glob-7.4 {-nocomplain switch} {
    list [catch {glob -nocomplain ~xyqrszzz} msg] $msg
} {0 {}}
test glob-7.5 {-nocomplain switch} {
    list [catch {glob ~xyqrszzz} msg] $msg
} {1 {user "xyqrszzz" doesn't exist}}
test glob-7.6 {-- switch} {
    list [catch {glob -- -nocomplain} msg] $msg
} {1 {no files matched glob patterns "-nocomplain"}}
test glob-7.7 {bogus switch} {
    list [catch {glob -gorp} msg] $msg
} {1 {bad switch "-gorp": must be -nocomplain or --}}

exec rm -rf globTest
