2016-04-23 17:42:28 +00:00
|
|
|
package globpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompileAndMatch(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
2016-04-24 20:37:44 +00:00
|
|
|
// test super asterisk
|
2016-04-23 17:42:28 +00:00
|
|
|
g1, err := Compile(dir + "/**")
|
|
|
|
require.NoError(t, err)
|
2016-04-24 20:37:44 +00:00
|
|
|
// test single asterisk
|
2016-04-23 17:42:28 +00:00
|
|
|
g2, err := Compile(dir + "/*.log")
|
|
|
|
require.NoError(t, err)
|
2016-04-24 20:37:44 +00:00
|
|
|
// test no meta characters (file exists)
|
2016-04-23 17:42:28 +00:00
|
|
|
g3, err := Compile(dir + "/log1.log")
|
|
|
|
require.NoError(t, err)
|
2016-04-24 20:37:44 +00:00
|
|
|
// test file that doesn't exist
|
|
|
|
g4, err := Compile(dir + "/i_dont_exist.log")
|
|
|
|
require.NoError(t, err)
|
|
|
|
// test super asterisk that doesn't exist
|
|
|
|
g5, err := Compile(dir + "/dir_doesnt_exist/**")
|
|
|
|
require.NoError(t, err)
|
2016-04-23 17:42:28 +00:00
|
|
|
|
|
|
|
matches := g1.Match()
|
2018-10-12 21:48:11 +00:00
|
|
|
require.Len(t, matches, 6)
|
2016-04-23 17:42:28 +00:00
|
|
|
matches = g2.Match()
|
2018-10-12 21:48:11 +00:00
|
|
|
require.Len(t, matches, 2)
|
2016-04-23 17:42:28 +00:00
|
|
|
matches = g3.Match()
|
2018-10-12 21:48:11 +00:00
|
|
|
require.Len(t, matches, 1)
|
2016-04-24 20:37:44 +00:00
|
|
|
matches = g4.Match()
|
2018-12-18 22:23:25 +00:00
|
|
|
require.Len(t, matches, 1)
|
2016-04-24 20:37:44 +00:00
|
|
|
matches = g5.Match()
|
2018-10-12 21:48:11 +00:00
|
|
|
require.Len(t, matches, 0)
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 21:48:11 +00:00
|
|
|
func TestRootGlob(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
2016-04-23 17:42:28 +00:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
2018-10-12 21:48:11 +00:00
|
|
|
{dir + "/**", dir + "/*"},
|
|
|
|
{dir + "/nested?/**", dir + "/nested?/*"},
|
|
|
|
{dir + "/ne**/nest*", dir + "/ne*"},
|
|
|
|
{dir + "/nested?/*", ""},
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-10-12 21:48:11 +00:00
|
|
|
actual, _ := Compile(test.input)
|
|
|
|
require.Equal(t, actual.rootGlob, test.output)
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:43:23 +00:00
|
|
|
func TestFindNestedTextFile(t *testing.T) {
|
|
|
|
dir := getTestdataDir()
|
|
|
|
// test super asterisk
|
|
|
|
g1, err := Compile(dir + "/**.txt")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
matches := g1.Match()
|
2018-10-12 21:48:11 +00:00
|
|
|
require.Len(t, matches, 1)
|
2017-02-01 22:43:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 17:42:28 +00:00
|
|
|
func getTestdataDir() string {
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
return strings.Replace(filename, "globpath_test.go", "testdata", 1)
|
|
|
|
}
|
2017-07-21 21:28:14 +00:00
|
|
|
|
|
|
|
func TestMatch_ErrPermission(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
2018-12-18 22:23:25 +00:00
|
|
|
expected []string
|
2017-07-21 21:28:14 +00:00
|
|
|
}{
|
2018-12-18 22:23:25 +00:00
|
|
|
{"/root/foo", []string{"/root/foo"}},
|
|
|
|
{"/root/f*", []string(nil)},
|
2017-07-21 21:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
glob, err := Compile(test.input)
|
|
|
|
require.NoError(t, err)
|
|
|
|
actual := glob.Match()
|
|
|
|
require.Equal(t, test.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 21:46:00 +00:00
|
|
|
|
|
|
|
func TestWindowsSeparator(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("Skipping Windows only test")
|
|
|
|
}
|
|
|
|
|
|
|
|
glob, err := Compile("testdata/nested1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
ok := glob.MatchString("testdata\\nested1")
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|