github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/glob_test.go (about)

     1  package eval_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "src.elv.sh/pkg/eval"
     7  
     8  	. "src.elv.sh/pkg/eval/evaltest"
     9  	"src.elv.sh/pkg/testutil"
    10  )
    11  
    12  func TestGlob_Simple(t *testing.T) {
    13  	_, cleanup := testutil.InTestDir()
    14  	defer cleanup()
    15  
    16  	testutil.MustMkdirAll("z", "z2")
    17  	testutil.MustCreateEmpty("bar", "foo", "ipsum", "lorem")
    18  
    19  	Test(t,
    20  		That("put *").Puts("bar", "foo", "ipsum", "lorem", "z", "z2"),
    21  		That("put z*").Puts("z", "z2"),
    22  		That("put ?").Puts("z"),
    23  		That("put ????m").Puts("ipsum", "lorem"),
    24  	)
    25  }
    26  
    27  func TestGlob_Recursive(t *testing.T) {
    28  	_, cleanup := testutil.InTestDir()
    29  	defer cleanup()
    30  
    31  	testutil.MustMkdirAll("1/2/3")
    32  	testutil.MustCreateEmpty("a.go", "1/a.go", "1/2/3/a.go")
    33  
    34  	Test(t,
    35  		That("put **").Puts("1/2/3/a.go", "1/2/3", "1/2", "1/a.go", "1", "a.go"),
    36  		That("put **.go").Puts("1/2/3/a.go", "1/a.go", "a.go"),
    37  		That("put 1**.go").Puts("1/2/3/a.go", "1/a.go"),
    38  	)
    39  }
    40  
    41  func TestGlob_NoMatch(t *testing.T) {
    42  	_, cleanup := testutil.InTestDir()
    43  	defer cleanup()
    44  
    45  	Test(t,
    46  		That("put a/b/nonexistent*").Throws(ErrWildcardNoMatch),
    47  		That("put a/b/nonexistent*[nomatch-ok]").DoesNothing(),
    48  	)
    49  }
    50  
    51  func TestGlob_MatchHidden(t *testing.T) {
    52  	_, cleanup := testutil.InTestDir()
    53  	defer cleanup()
    54  
    55  	testutil.MustMkdirAll("d", ".d")
    56  	testutil.MustCreateEmpty("a", ".a", "d/a", "d/.a", ".d/a", ".d/.a")
    57  
    58  	Test(t,
    59  		That("put *").Puts("a", "d"),
    60  		That("put *[match-hidden]").Puts(".a", ".d", "a", "d"),
    61  		That("put *[match-hidden]/*").Puts(".d/a", "d/a"),
    62  		That("put */*[match-hidden]").Puts("d/.a", "d/a"),
    63  		That("put *[match-hidden]/*[match-hidden]").Puts(
    64  			".d/.a", ".d/a", "d/.a", "d/a"),
    65  	)
    66  }
    67  
    68  func TestGlob_SetAndRange(t *testing.T) {
    69  	_, cleanup := testutil.InTestDir()
    70  	defer cleanup()
    71  
    72  	testutil.MustCreateEmpty("a1", "a2", "b1", "c1", "ipsum", "lorem")
    73  
    74  	Test(t,
    75  		That("put ?[set:ab]*").Puts("a1", "a2", "b1"),
    76  		That("put ?[range:a-c]*").Puts("a1", "a2", "b1", "c1"),
    77  		That("put ?[range:a~c]*").Puts("a1", "a2", "b1"),
    78  		That("put *[range:a-z]").Puts("ipsum", "lorem"),
    79  	)
    80  
    81  }
    82  
    83  func TestGlob_But(t *testing.T) {
    84  	_, cleanup := testutil.InTestDir()
    85  	defer cleanup()
    86  
    87  	testutil.MustCreateEmpty("bar", "foo", "ipsum", "lorem")
    88  
    89  	Test(t,
    90  		// Nonexistent files can also be excluded
    91  		That("put *[but:foobar][but:ipsum]").Puts("bar", "foo", "lorem"),
    92  	)
    93  }
    94  
    95  func TestGlob_Type(t *testing.T) {
    96  	_, cleanup := testutil.InTestDir()
    97  	defer cleanup()
    98  
    99  	testutil.MustMkdirAll("d1", "d2", ".d", "b/c")
   100  	testutil.MustCreateEmpty("bar", "foo", "ipsum", "lorem", "d1/f1", "d2/fm")
   101  
   102  	Test(t,
   103  		That("put **[type:dir]").Puts("b/c", "b", "d1", "d2"),
   104  		That("put **[type:regular]").Puts("d1/f1", "d2/fm", "bar", "foo", "ipsum", "lorem"),
   105  		That("put **[type:regular]m").Puts("d2/fm", "ipsum", "lorem"),
   106  		That("put **[type:dir]f*[type:regular]").Throws(ErrMultipleTypeModifiers),
   107  		That("put **[type:unknown]").Throws(ErrUnknownTypeModifier),
   108  	)
   109  }