github.com/IBM/fsgo@v0.0.0-20220920202152-e16fd2119d49/match_test.go (about)

     1  // Copyright 2022 IBM Inc. All rights reserved
     2  // Copyright © 2014 Steve Francia <spf@spf13.com>
     3  //
     4  // SPDX-License-Identifier: Apache2.0
     5  package fsgo
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  // contains returns true if vector contains the string s.
    15  func contains(vector []string, s string) bool {
    16  	for _, elem := range vector {
    17  		if elem == s {
    18  			return true
    19  		}
    20  	}
    21  	return false
    22  }
    23  
    24  func setupGlobDirRoot(t *testing.T, fs Fs) string {
    25  	path := testDir(fs)
    26  	setupGlobFiles(t, fs, path)
    27  	return path
    28  }
    29  
    30  func setupGlobDirReusePath(t *testing.T, fs Fs, path string) string {
    31  	testRegistry[fs] = append(testRegistry[fs], path)
    32  	return setupGlobFiles(t, fs, path)
    33  }
    34  
    35  func setupGlobFiles(t *testing.T, fs Fs, path string) string {
    36  	testSubDir := filepath.Join(path, "globs", "bobs")
    37  	err := fs.MkdirAll(testSubDir, 0700)
    38  	if err != nil && !os.IsExist(err) {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	f, err := fs.Create(filepath.Join(testSubDir, "/matcher"))
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	f.WriteString("Testfile 1 content")
    47  	f.Close()
    48  
    49  	f, err = fs.Create(filepath.Join(testSubDir, "/../submatcher"))
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	f.WriteString("Testfile 2 content")
    54  	f.Close()
    55  
    56  	f, err = fs.Create(filepath.Join(testSubDir, "/../../match"))
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	f.WriteString("Testfile 3 content")
    61  	f.Close()
    62  
    63  	return testSubDir
    64  }
    65  
    66  func TestGlob(t *testing.T) {
    67  	defer removeAllTestFiles(t)
    68  	var testDir string
    69  	for i, fs := range Fss {
    70  		if i == 0 {
    71  			testDir = setupGlobDirRoot(t, fs)
    72  		} else {
    73  			setupGlobDirReusePath(t, fs, testDir)
    74  		}
    75  	}
    76  
    77  	var globTests = []struct {
    78  		pattern, result string
    79  	}{
    80  		{testDir + "/globs/bobs/matcher", testDir + "/globs/bobs/matcher"},
    81  		{testDir + "/globs/*/mat?her", testDir + "/globs/bobs/matcher"},
    82  		{testDir + "/globs/bobs/../*", testDir + "/globs/submatcher"},
    83  		{testDir + "/match", testDir + "/match"},
    84  	}
    85  
    86  	for _, fs := range Fss {
    87  
    88  		for _, tt := range globTests {
    89  			pattern := tt.pattern
    90  			result := tt.result
    91  			if runtime.GOOS == "windows" {
    92  				pattern = filepath.Clean(pattern)
    93  				result = filepath.Clean(result)
    94  			}
    95  			matches, err := Glob(fs, pattern)
    96  			if err != nil {
    97  				t.Errorf("Glob error for %q: %s", pattern, err)
    98  				continue
    99  			}
   100  			if !contains(matches, result) {
   101  				t.Errorf("Glob(%#q) = %#v want %v", pattern, matches, result)
   102  			}
   103  		}
   104  		for _, pattern := range []string{"no_match", "../*/no_match"} {
   105  			matches, err := Glob(fs, pattern)
   106  			if err != nil {
   107  				t.Errorf("Glob error for %q: %s", pattern, err)
   108  				continue
   109  			}
   110  			if len(matches) != 0 {
   111  				t.Errorf("Glob(%#q) = %#v want []", pattern, matches)
   112  			}
   113  		}
   114  
   115  	}
   116  }
   117  
   118  func TestGlobSymlink(t *testing.T) {
   119  	defer removeAllTestFiles(t)
   120  
   121  	fs := &OsFs{}
   122  	testDir := setupGlobDirRoot(t, fs)
   123  
   124  	err := os.Symlink("target", filepath.Join(testDir, "symlink"))
   125  	if err != nil {
   126  		t.Skipf("skipping on %s", runtime.GOOS)
   127  	}
   128  
   129  	var globSymlinkTests = []struct {
   130  		path, dest string
   131  		brokenLink bool
   132  	}{
   133  		{"test1", "link1", false},
   134  		{"test2", "link2", true},
   135  	}
   136  
   137  	for _, tt := range globSymlinkTests {
   138  		path := filepath.Join(testDir, tt.path)
   139  		dest := filepath.Join(testDir, tt.dest)
   140  		f, err := fs.Create(path)
   141  		if err != nil {
   142  			t.Fatal(err)
   143  		}
   144  		if err := f.Close(); err != nil {
   145  			t.Fatal(err)
   146  		}
   147  		err = os.Symlink(path, dest)
   148  		if err != nil {
   149  			t.Fatal(err)
   150  		}
   151  		if tt.brokenLink {
   152  			// Break the symlink.
   153  			fs.Remove(path)
   154  		}
   155  		matches, err := Glob(fs, dest)
   156  		if err != nil {
   157  			t.Errorf("GlobSymlink error for %q: %s", dest, err)
   158  		}
   159  		if !contains(matches, dest) {
   160  			t.Errorf("Glob(%#q) = %#v want %v", dest, matches, dest)
   161  		}
   162  	}
   163  }
   164  
   165  func TestGlobError(t *testing.T) {
   166  	for _, fs := range Fss {
   167  		_, err := Glob(fs, "[7]")
   168  		if err != nil {
   169  			t.Error("expected error for bad pattern; got none")
   170  		}
   171  	}
   172  }