2020-11-23 23:40:32 +08:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
|
|
// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
|
|
|
|
|
// https://github.com/influxdata/telegraf/issues/6248
|
|
|
|
|
|
2016-04-24 01:42:28 +08:00
|
|
|
package globpath
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-23 23:40:32 +08:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2016-04-24 01:42:28 +08:00
|
|
|
"runtime"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2020-11-23 23:40:32 +08:00
|
|
|
var (
|
|
|
|
|
testdataDir = getTestdataDir()
|
|
|
|
|
)
|
|
|
|
|
|
2016-04-24 01:42:28 +08:00
|
|
|
func TestCompileAndMatch(t *testing.T) {
|
2016-04-25 04:37:44 +08:00
|
|
|
// test super asterisk
|
2020-11-23 23:40:32 +08:00
|
|
|
g1, err := Compile(filepath.Join(testdataDir, "**"))
|
2016-04-24 01:42:28 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-25 04:37:44 +08:00
|
|
|
// test single asterisk
|
2020-11-23 23:40:32 +08:00
|
|
|
g2, err := Compile(filepath.Join(testdataDir, "*.log"))
|
2016-04-24 01:42:28 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-25 04:37:44 +08:00
|
|
|
// test no meta characters (file exists)
|
2020-11-23 23:40:32 +08:00
|
|
|
g3, err := Compile(filepath.Join(testdataDir, "log1.log"))
|
2016-04-24 01:42:28 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-25 04:37:44 +08:00
|
|
|
// test file that doesn't exist
|
2020-11-23 23:40:32 +08:00
|
|
|
g4, err := Compile(filepath.Join(testdataDir, "i_dont_exist.log"))
|
2016-04-25 04:37:44 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
// test super asterisk that doesn't exist
|
2020-11-23 23:40:32 +08:00
|
|
|
g5, err := Compile(filepath.Join(testdataDir, "dir_doesnt_exist", "**"))
|
2016-04-25 04:37:44 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-24 01:42:28 +08:00
|
|
|
|
|
|
|
|
matches := g1.Match()
|
2018-10-13 05:48:11 +08:00
|
|
|
require.Len(t, matches, 6)
|
2016-04-24 01:42:28 +08:00
|
|
|
matches = g2.Match()
|
2018-10-13 05:48:11 +08:00
|
|
|
require.Len(t, matches, 2)
|
2016-04-24 01:42:28 +08:00
|
|
|
matches = g3.Match()
|
2018-10-13 05:48:11 +08:00
|
|
|
require.Len(t, matches, 1)
|
2016-04-25 04:37:44 +08:00
|
|
|
matches = g4.Match()
|
2018-12-19 06:23:25 +08:00
|
|
|
require.Len(t, matches, 1)
|
2016-04-25 04:37:44 +08:00
|
|
|
matches = g5.Match()
|
2018-10-13 05:48:11 +08:00
|
|
|
require.Len(t, matches, 0)
|
2016-04-24 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-13 05:48:11 +08:00
|
|
|
func TestRootGlob(t *testing.T) {
|
2016-04-24 01:42:28 +08:00
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
output string
|
|
|
|
|
}{
|
2020-11-23 23:40:32 +08:00
|
|
|
{filepath.Join(testdataDir, "**"), filepath.Join(testdataDir, "*")},
|
|
|
|
|
{filepath.Join(testdataDir, "nested?", "**"), filepath.Join(testdataDir, "nested?", "*")},
|
|
|
|
|
{filepath.Join(testdataDir, "ne**", "nest*"), filepath.Join(testdataDir, "ne*")},
|
|
|
|
|
{filepath.Join(testdataDir, "nested?", "*"), ""},
|
2016-04-24 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-10-13 05:48:11 +08:00
|
|
|
actual, _ := Compile(test.input)
|
|
|
|
|
require.Equal(t, actual.rootGlob, test.output)
|
2016-04-24 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 06:43:23 +08:00
|
|
|
func TestFindNestedTextFile(t *testing.T) {
|
|
|
|
|
// test super asterisk
|
2020-11-23 23:40:32 +08:00
|
|
|
g1, err := Compile(filepath.Join(testdataDir, "**.txt"))
|
2017-02-02 06:43:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
matches := g1.Match()
|
2018-10-13 05:48:11 +08:00
|
|
|
require.Len(t, matches, 1)
|
2017-02-02 06:43:23 +08:00
|
|
|
}
|
|
|
|
|
|
2017-07-22 05:28:14 +08:00
|
|
|
func TestMatch_ErrPermission(t *testing.T) {
|
2020-11-23 23:40:32 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
t.Skip("Skipping Unix only test")
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-22 05:28:14 +08:00
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
2018-12-19 06:23:25 +08:00
|
|
|
expected []string
|
2017-07-22 05:28:14 +08:00
|
|
|
}{
|
2018-12-19 06:23:25 +08:00
|
|
|
{"/root/foo", []string{"/root/foo"}},
|
|
|
|
|
{"/root/f*", []string(nil)},
|
2017-07-22 05:28:14 +08: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-09 05:46:00 +08: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)
|
|
|
|
|
}
|
2020-11-23 23:40:32 +08:00
|
|
|
|
|
|
|
|
func getTestdataDir() string {
|
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// if we cannot even establish the test directory, further progress is meaningless
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filepath.Join(dir, "testdata")
|
|
|
|
|
}
|