Black Lives Matter. Support the Equal Justice Initiative.

Source file src/io/fs/glob_test.go

Documentation: io/fs

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fs_test
     6  
     7  import (
     8  	. "io/fs"
     9  	"os"
    10  	"path"
    11  	"testing"
    12  )
    13  
    14  var globTests = []struct {
    15  	fs              FS
    16  	pattern, result string
    17  }{
    18  	{os.DirFS("."), "glob.go", "glob.go"},
    19  	{os.DirFS("."), "gl?b.go", "glob.go"},
    20  	{os.DirFS("."), `gl\ob.go`, "glob.go"},
    21  	{os.DirFS("."), "*", "glob.go"},
    22  	{os.DirFS(".."), "*/glob.go", "fs/glob.go"},
    23  }
    24  
    25  func TestGlob(t *testing.T) {
    26  	for _, tt := range globTests {
    27  		matches, err := Glob(tt.fs, tt.pattern)
    28  		if err != nil {
    29  			t.Errorf("Glob error for %q: %s", tt.pattern, err)
    30  			continue
    31  		}
    32  		if !contains(matches, tt.result) {
    33  			t.Errorf("Glob(%#q) = %#v want %v", tt.pattern, matches, tt.result)
    34  		}
    35  	}
    36  	for _, pattern := range []string{"no_match", "../*/no_match", `\*`} {
    37  		matches, err := Glob(os.DirFS("."), pattern)
    38  		if err != nil {
    39  			t.Errorf("Glob error for %q: %s", pattern, err)
    40  			continue
    41  		}
    42  		if len(matches) != 0 {
    43  			t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
    44  		}
    45  	}
    46  }
    47  
    48  func TestGlobError(t *testing.T) {
    49  	bad := []string{`[]`, `nonexist/[]`}
    50  	for _, pattern := range bad {
    51  		_, err := Glob(os.DirFS("."), pattern)
    52  		if err != path.ErrBadPattern {
    53  			t.Errorf("Glob(fs, %#q) returned err=%v, want path.ErrBadPattern", pattern, err)
    54  		}
    55  	}
    56  }
    57  
    58  // contains reports whether vector contains the string s.
    59  func contains(vector []string, s string) bool {
    60  	for _, elem := range vector {
    61  		if elem == s {
    62  			return true
    63  		}
    64  	}
    65  	return false
    66  }
    67  
    68  type globOnly struct{ GlobFS }
    69  
    70  func (globOnly) Open(name string) (File, error) { return nil, ErrNotExist }
    71  
    72  func TestGlobMethod(t *testing.T) {
    73  	check := func(desc string, names []string, err error) {
    74  		t.Helper()
    75  		if err != nil || len(names) != 1 || names[0] != "hello.txt" {
    76  			t.Errorf("Glob(%s) = %v, %v, want %v, nil", desc, names, err, []string{"hello.txt"})
    77  		}
    78  	}
    79  
    80  	// Test that ReadDir uses the method when present.
    81  	names, err := Glob(globOnly{testFsys}, "*.txt")
    82  	check("readDirOnly", names, err)
    83  
    84  	// Test that ReadDir uses Open when the method is not present.
    85  	names, err = Glob(openOnly{testFsys}, "*.txt")
    86  	check("openOnly", names, err)
    87  }
    88  

View as plain text