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