# 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-1993 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
# $Header: /user6/ouster/tcl/tests/RCS/glob.test,v 1.23 93/08/28 15:57:40 ouster Exp $ (Berkeley)

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 immediately below can only be run at Berkeley, where
# the file-system structure is well-known.

if $atBerkeley {
    test glob-4.1 {tildes} {glob ~/.csh*} "/users/ouster/.cshrc"
    test glob-4.2 {tildes} {glob ~ouster/.csh*} "/users/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}}

exec chmod 000 globTest
if {$user != "root"} {
    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 {-- switch} {
    list [catch {glob -- -nocomplain} msg] $msg
} {1 {no files matched glob patterns "-nocomplain"}}
test glob-7.5 {bogus switch} {
    list [catch {glob -gorp} msg] $msg
} {1 {bad switch "-gorp": must be -nocomplain or --}}

exec rm -rf globTest
