...

Source file src/github.com/klauspost/compress/s2/cmd/internal/filepathx/filepathx_test.go

Documentation: github.com/klauspost/compress/s2/cmd/internal/filepathx

     1  package filepathx
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) {
    10  	// test passthru to vanilla path/filepath
    11  	path := "./a/b/c.d/e.f"
    12  	err := os.MkdirAll(path, 0755)
    13  	if err != nil {
    14  		t.Fatalf("os.MkdirAll: %s", err)
    15  	}
    16  	matches, err := Glob("./*/*/*.d")
    17  	if err != nil {
    18  		t.Fatalf("Glob: %s", err)
    19  	}
    20  	if len(matches) != 1 {
    21  		t.Fatalf("got %d matches, expected 1", len(matches))
    22  	}
    23  	expected := strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator))
    24  	if matches[0] != expected {
    25  		t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
    26  	}
    27  }
    28  
    29  func TestGlob_OneDoubleStar_oneMatch(t *testing.T) {
    30  	// test a single double-star
    31  	path := "./a/b/c.d/e.f"
    32  	err := os.MkdirAll(path, 0755)
    33  	if err != nil {
    34  		t.Fatalf("os.MkdirAll: %s", err)
    35  	}
    36  	matches, err := Glob("./**/*.f")
    37  	if err != nil {
    38  		t.Fatalf("Glob: %s", err)
    39  	}
    40  	if len(matches) != 1 {
    41  		t.Fatalf("got %d matches, expected 1", len(matches))
    42  	}
    43  	expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator))
    44  	if matches[0] != expected {
    45  		t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
    46  	}
    47  }
    48  
    49  func TestGlob_OneDoubleStar_twoMatches(t *testing.T) {
    50  	// test a single double-star
    51  	path := "./a/b/c.d/e.f"
    52  	err := os.MkdirAll(path, 0755)
    53  	if err != nil {
    54  		t.Fatalf("os.MkdirAll: %s", err)
    55  	}
    56  	matches, err := Glob("./a/**/*.*")
    57  	if err != nil {
    58  		t.Fatalf("Glob: %s", err)
    59  	}
    60  	if len(matches) != 2 {
    61  		t.Fatalf("got %d matches, expected 2", len(matches))
    62  	}
    63  	expected := []string{
    64  		strings.Join([]string{"a", "b", "c.d"}, string(os.PathSeparator)),
    65  		strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator)),
    66  	}
    67  
    68  	for i, match := range matches {
    69  		if match != expected[i] {
    70  			t.Fatalf("matched [%s], expected [%s]", match, expected[i])
    71  		}
    72  	}
    73  }
    74  
    75  func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) {
    76  	// test two double-stars
    77  	path := "./a/b/c.d/e.f"
    78  	err := os.MkdirAll(path, 0755)
    79  	if err != nil {
    80  		t.Fatalf("os.MkdirAll: %s", err)
    81  	}
    82  	matches, err := Glob("./**/b/**/*.f")
    83  	if err != nil {
    84  		t.Fatalf("Glob: %s", err)
    85  	}
    86  	if len(matches) != 1 {
    87  		t.Fatalf("got %d matches, expected 1", len(matches))
    88  	}
    89  	expected := strings.Join([]string{"a", "b", "c.d", "e.f"}, string(os.PathSeparator))
    90  
    91  	if matches[0] != expected {
    92  		t.Fatalf("matched [%s], expected [%s]", matches[0], expected)
    93  	}
    94  }
    95  
    96  func TestExpand_DirectCall_emptySlice(t *testing.T) {
    97  	var empty []string
    98  	matches, err := Globs(empty).Expand()
    99  	if err != nil {
   100  		t.Fatalf("Glob: %s", err)
   101  	}
   102  	if len(matches) != 0 {
   103  		t.Fatalf("got %d matches, expected 0", len(matches))
   104  	}
   105  }
   106  

View as plain text