@@ -80,6 +80,54 @@ func copyFileCmd(script *testscript.TestScript, neg bool, args []string) {
8080 script .Check (err )
8181}
8282
83+ func globCmd (script * testscript.TestScript , neg bool , args []string ) {
84+ count := - 1
85+ if neg {
86+ count = 0
87+ }
88+ if len (args ) != 0 {
89+ after , ok := strings .CutPrefix (args [0 ], "-count=" )
90+ if ok {
91+ var err error
92+ count , err = strconv .Atoi (after )
93+ if err != nil {
94+ script .Fatalf ("invalid -count=: %v" , err )
95+ }
96+ if count < 1 {
97+ script .Fatalf ("invalid -count=: must be at least 1" )
98+ }
99+ args = args [1 :]
100+ }
101+ }
102+ if len (args ) == 0 {
103+ script .Fatalf ("usage: glob [-count=N] pattern" )
104+ }
105+
106+ var matches []string
107+ for _ , a := range args {
108+ glob := script .MkAbs (a )
109+ m , err := filepath .Glob (glob )
110+ if err != nil {
111+ script .Fatalf ("invalid glob pattern: %v" , err )
112+ }
113+ for _ , match := range m {
114+ script .Logf ("glob %q matched: %s" , glob , match )
115+ }
116+ matches = append (matches , m ... )
117+ }
118+
119+ // -1 means that no -count= was given, so we want at least 1 match.
120+ if count == - 1 {
121+ if len (matches ) == 0 && ! neg {
122+ script .Fatalf ("no matches for globs %q, want at least 1" , strings .Join (args , " " ))
123+ }
124+ return
125+ }
126+ if len (matches ) != count {
127+ script .Fatalf ("got %d matches for globs %q, want %d" , len (matches ), strings .Join (args , " " ), count )
128+ }
129+ }
130+
83131func getTestscriptParams (dir string ) testscript.Params {
84132 return testscript.Params {
85133 Dir : dir ,
@@ -91,6 +139,7 @@ func getTestscriptParams(dir string) testscript.Params {
91139 "devboxjson.packages.contains" : assertDevboxJSONPackagesContains ,
92140 "devboxlock.packages.contains" : assertDevboxLockPackagesContains ,
93141 "env.path.len" : assertPathLength ,
142+ "glob" : globCmd ,
94143 "json.superset" : assertJSONSuperset ,
95144 "path.order" : assertPathOrder ,
96145 "source.path" : sourcePath ,
0 commit comments