1 # Test the glob command
3 source [file dirname [info script]]/testing.tcl
8 # Fake the bare minimum that glob.tcl needs:
9 # [readdir], [file isdir] and [file exists]
10 local proc file {cmd args} {
11 if {$cmd in {isdir exists}} {
12 lassign [fslookup [lindex $args 0]] type contents
13 if {$cmd eq "isdir" && $type eq "dir"} {
15 } elseif {$type ne "none"} {
20 tailcall upcall file $cmd {*}$args
23 local proc readdir {{-nocomplain {}} dir} {
24 lassign [fslookup $dir] type contents
26 if {${-nocomplain} eq ""} {
29 return -code error "No such file or directory"
34 local proc fslookup {path} {
37 foreach p [split $path /] {
41 if {![dict exists $dict $p] || $type ne "dir"} {
44 lassign [dict get $dict $p] type dict
49 # Creates the representation of a filesystem in a dictionary - for testing
50 local proc makefakefs {fs} {
52 foreach {type name contents} $fs {
53 switch -glob -- $type {
55 set fakefs($name) [list file $contents]
58 set fakefs($name) [list dir [makefakefs $contents]]
61 error "Unknown fs type: $type"
68 # Create a fake filesystem for testing the glob command
69 set ::FAKEFS [makefakefs {
70 file abc {This is the contents of abc}
72 file ghi {This file is inside def}
78 file "open\[bracket" {}
79 file "close\]bracket" {}
83 test glob-1.1 {Simple} {
87 test glob-1.2 {Simple} {
91 test glob-1.3 {Simple} -returnCodes error -body {
93 } -result {no files matched glob pattern "x*"}
95 test glob-1.4 {Simple} -returnCodes error -body {
97 } -result {wrong # args: should be "glob ?options? pattern ?pattern ...?"}
99 test glob-1.5 {Simple} -returnCodes ok -body {
100 lsort [glob -nocomplain x*]
103 test glob-2.1 {Braces} -returnCodes ok -body {
104 lsort [glob "{a,d}*"]
107 test glob-2.2 {Files containing braces and brackets} -returnCodes ok -body {
109 } -result {tmp/close\]bracket tmp/close\}brace {tmp/open[bracket} tmp/open\{brace}
111 test glob-2.3 {Glob match files open bracket} -returnCodes ok -body {
112 lsort [glob {tmp/*\[*}]
113 } -result [list tmp/open\[bracket]
115 test glob-2.4 {Glob match files close bracket} -returnCodes ok -body {
116 lsort [glob {tmp/*\]*}]
117 } -result [list tmp/close\]bracket]
119 test glob-2.5 {Glob match files containing braced brackets} -returnCodes ok -body {
120 lsort [glob {tmp/*{\[,]}*}]
121 } -result [list tmp/close\]bracket tmp/open\[bracket]
123 test glob-3.1 {Directory option} -returnCodes ok -body {
124 lsort [glob -dir tmp -tails *]
125 } -result [list close\]bracket close\}brace open\[bracket open\{brace]
127 test glob-3.2 {Directory option} -returnCodes ok -body {
128 lsort [glob -dir tmp -tails *close*]
129 } -result [list close\]bracket close\}brace]