go.arsenm.dev/pcre@v0.0.0-20220530205550-74594f6c8b0e/glob_test.go (about)

     1  package pcre_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"go.arsenm.dev/pcre"
     8  )
     9  
    10  func TestCompileGlob(t *testing.T) {
    11  	r, err := pcre.CompileGlob("/**/bin")
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  
    16  	if !r.MatchString("/bin") {
    17  		t.Error("expected /bin to match")
    18  	}
    19  
    20  	if !r.MatchString("/usr/bin") {
    21  		t.Error("expected /usr/bin to match")
    22  	}
    23  
    24  	if !r.MatchString("/usr/local/bin") {
    25  		t.Error("expected /usr/local/bin to match")
    26  	}
    27  
    28  	if r.MatchString("/usr") {
    29  		t.Error("expected /usr not to match")
    30  	}
    31  
    32  	if r.MatchString("/usr/local") {
    33  		t.Error("expected /usr/local not to match")
    34  	}
    35  
    36  	if r.MatchString("/home") {
    37  		t.Error("expected /home not to match")
    38  	}
    39  }
    40  
    41  func TestGlob(t *testing.T) {
    42  	err := os.MkdirAll("pcretest/dir1", 0755)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	err = os.MkdirAll("pcretest/dir2", 0755)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	err = os.MkdirAll("pcretest/test1/dir4", 0755)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	err = touch("pcretest/file1")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	
    62  	err = touch("pcretest/file2")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	err = touch("pcretest/test1/dir4/text.txt")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	matches, err := pcre.Glob("pcretest")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if len(matches) != 1 || matches[0] != "pcretest" {
    77  		t.Errorf("expected [pcretest], got %v", matches)
    78  	}
    79  
    80  	matches, err = pcre.Glob("pcretest/dir*")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if len(matches) != 2 ||
    85  		matches[0] != "pcretest/dir1" ||
    86  		matches[1] != "pcretest/dir2" {
    87  		t.Errorf("expected [pcretest/dir1 pcretest/dir2], got %v", matches)
    88  	}
    89  
    90  	matches, err = pcre.Glob("pcretest/file*")
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if len(matches) != 2 ||
    95  		matches[0] != "pcretest/file1" ||
    96  		matches[1] != "pcretest/file2" {
    97  		t.Errorf("expected [pcretest/file1 pcretest/file2], got %v", matches)
    98  	}
    99  
   100  	matches, err = pcre.Glob("pcretest/**/*.txt")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if len(matches) != 1 ||
   105  		matches[0] != "pcretest/test1/dir4/text.txt" {
   106  		t.Errorf("expected [pcretest/test1/dir4/text.txt], got %v", matches)
   107  	}
   108  
   109  	err = os.RemoveAll("pcretest")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  }
   114  
   115  func touch(path string) error {
   116  	fl, err := os.OpenFile(path, os.O_CREATE, 0644)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return fl.Close()
   121  }