github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/source/directorysource/directory_source_win_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // why the build tags? there is behavior from filepath.ToSlash() that must be tested, but can't be tested on non-windows
     5  // since the stdlib keeps this functionality behind a build tag (specifically filepath.Separator):
     6  //   - https://github.com/golang/go/blob/3aea422e2cb8b1ec2e0c2774be97fe96c7299838/src/path/filepath/path.go#L224-L227
     7  //   - https://github.com/golang/go/blob/3aea422e2cb8b1ec2e0c2774be97fe96c7299838/src/path/filepath/path.go#L63
     8  //   - https://github.com/golang/go/blob/master/src/os/path_windows.go#L8
     9  //
    10  // It would be nice to extract this to simplify testing, however, we also need filepath.Abs(), which in windows
    11  // requires a specific syscall:
    12  //   - https://github.com/golang/go/blob/3aea422e2cb8b1ec2e0c2774be97fe96c7299838/src/path/filepath/path_windows.go#L216
    13  // ... which means we can't extract this functionality without build tags.
    14  
    15  package directorysource
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func Test_DirectorySource_crossPlatformExclusions(t *testing.T) {
    24  	testCases := []struct {
    25  		desc    string
    26  		root    string
    27  		path    string
    28  		exclude string
    29  		match   bool
    30  	}{
    31  		{
    32  			desc:    "windows doublestar",
    33  			root:    "C:\\User\\stuff",
    34  			path:    "C:\\User\\stuff\\thing.txt",
    35  			exclude: "**/*.txt",
    36  			match:   true,
    37  		},
    38  		{
    39  			desc:    "windows relative",
    40  			root:    "C:\\User\\stuff",
    41  			path:    "C:\\User\\stuff\\thing.txt",
    42  			exclude: "./*.txt",
    43  			match:   true,
    44  		},
    45  		{
    46  			desc:    "windows one level",
    47  			root:    "C:\\User\\stuff",
    48  			path:    "C:\\User\\stuff\\thing.txt",
    49  			exclude: "*/*.txt",
    50  			match:   false,
    51  		},
    52  	}
    53  
    54  	for _, test := range testCases {
    55  		t.Run(test.desc, func(t *testing.T) {
    56  			fns, err := GetDirectoryExclusionFunctions(test.root, []string{test.exclude})
    57  			require.NoError(t, err)
    58  
    59  			for _, f := range fns {
    60  				result := f(test.path, nil, nil)
    61  				require.Equal(t, test.match, result)
    62  			}
    63  		})
    64  	}
    65  }