github.com/influxdata/telegraf@v1.30.3/internal/globpath/globpath_test.go (about)

     1  //go:build !windows
     2  
     3  // TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
     4  // https://github.com/influxdata/telegraf/issues/6248
     5  
     6  package globpath
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var (
    18  	testdataDir = getTestdataDir()
    19  )
    20  
    21  func TestCompileAndMatch(t *testing.T) {
    22  	type test struct {
    23  		path    string
    24  		matches int
    25  	}
    26  
    27  	tests := []test{
    28  		//test super asterisk
    29  		{path: filepath.Join(testdataDir, "**"), matches: 7},
    30  		// test single asterisk
    31  		{path: filepath.Join(testdataDir, "*.log"), matches: 3},
    32  		// test no meta characters (file exists)
    33  		{path: filepath.Join(testdataDir, "log1.log"), matches: 1},
    34  		// test file that doesn't exist
    35  		{path: filepath.Join(testdataDir, "i_dont_exist.log"), matches: 0},
    36  		// test super asterisk that doesn't exist
    37  		{path: filepath.Join(testdataDir, "dir_doesnt_exist", "**"), matches: 0},
    38  		// test exclamation mark creates non-matching list with a range
    39  		{path: filepath.Join(testdataDir, "log[!1-2]*"), matches: 1},
    40  		// test caret creates non-matching list
    41  		{path: filepath.Join(testdataDir, "log[^1-2]*"), matches: 1},
    42  		// test exclamation mark creates non-matching list without a range
    43  		{path: filepath.Join(testdataDir, "log[!2]*"), matches: 2},
    44  		// test exclamation mark creates non-matching list without a range
    45  		//nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator
    46  		{path: filepath.Join(testdataDir, "log\\[!*"), matches: 1},
    47  		// test exclamation mark creates non-matching list without a range
    48  		//nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator
    49  		{path: filepath.Join(testdataDir, "log\\[^*"), matches: 0},
    50  	}
    51  
    52  	for _, tc := range tests {
    53  		g, err := Compile(tc.path)
    54  		require.NoError(t, err)
    55  		matches := g.Match()
    56  		require.Len(t, matches, tc.matches)
    57  	}
    58  }
    59  
    60  func TestRootGlob(t *testing.T) {
    61  	tests := []struct {
    62  		input  string
    63  		output string
    64  	}{
    65  		{filepath.Join(testdataDir, "**"), filepath.Join(testdataDir, "*")},
    66  		{filepath.Join(testdataDir, "nested?", "**"), filepath.Join(testdataDir, "nested?", "*")},
    67  		{filepath.Join(testdataDir, "ne**", "nest*"), filepath.Join(testdataDir, "ne*")},
    68  		{filepath.Join(testdataDir, "nested?", "*"), ""},
    69  	}
    70  
    71  	for _, test := range tests {
    72  		actual, _ := Compile(test.input)
    73  		require.Equal(t, actual.rootGlob, test.output)
    74  	}
    75  }
    76  
    77  func TestFindNestedTextFile(t *testing.T) {
    78  	// test super asterisk
    79  	g1, err := Compile(filepath.Join(testdataDir, "**.txt"))
    80  	require.NoError(t, err)
    81  
    82  	matches := g1.Match()
    83  	require.Len(t, matches, 1)
    84  }
    85  
    86  func TestMatch_ErrPermission(t *testing.T) {
    87  	if runtime.GOOS == "windows" {
    88  		t.Skip("Skipping Unix only test")
    89  	}
    90  
    91  	tests := []struct {
    92  		input    string
    93  		expected []string
    94  	}{
    95  		{"/root/foo", []string(nil)},
    96  		{"/root/f*", []string(nil)},
    97  	}
    98  
    99  	for _, test := range tests {
   100  		glob, err := Compile(test.input)
   101  		require.NoError(t, err)
   102  		actual := glob.Match()
   103  		require.Equal(t, test.expected, actual)
   104  	}
   105  }
   106  
   107  func TestWindowsSeparator(t *testing.T) {
   108  	if runtime.GOOS != "windows" {
   109  		t.Skip("Skipping Windows only test")
   110  	}
   111  
   112  	glob, err := Compile("testdata/nested1")
   113  	require.NoError(t, err)
   114  	ok := glob.MatchString("testdata\\nested1")
   115  	require.True(t, ok)
   116  }
   117  
   118  func getTestdataDir() string {
   119  	dir, err := os.Getwd()
   120  	if err != nil {
   121  		// if we cannot even establish the test directory, further progress is meaningless
   122  		panic(err)
   123  	}
   124  
   125  	return filepath.Join(dir, "testdata")
   126  }