github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/file/cataloger/filecontent/cataloger_test.go (about)

     1  package filecontent
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/syft/syft/file"
    10  )
    11  
    12  func TestContentsCataloger(t *testing.T) {
    13  	allFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
    14  
    15  	tests := []struct {
    16  		name     string
    17  		globs    []string
    18  		maxSize  int64
    19  		files    []string
    20  		expected map[file.Coordinates]string
    21  	}{
    22  		{
    23  			name:  "multi-pattern",
    24  			globs: []string{"test-fixtures/last/*.txt", "test-fixtures/*.txt"},
    25  			files: allFiles,
    26  			expected: map[file.Coordinates]string{
    27  				file.NewLocation("test-fixtures/last/path.txt").Coordinates:    "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh",
    28  				file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    29  				file.NewLocation("test-fixtures/a-path.txt").Coordinates:       "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    30  			},
    31  		},
    32  		{
    33  			name:     "no-patterns",
    34  			globs:    []string{},
    35  			files:    []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"},
    36  			expected: map[file.Coordinates]string{},
    37  		},
    38  		{
    39  			name:  "all-txt",
    40  			globs: []string{"**/*.txt"},
    41  			files: allFiles,
    42  			expected: map[file.Coordinates]string{
    43  				file.NewLocation("test-fixtures/last/path.txt").Coordinates:    "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh",
    44  				file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    45  				file.NewLocation("test-fixtures/a-path.txt").Coordinates:       "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    46  			},
    47  		},
    48  		{
    49  			name:  "subpath",
    50  			globs: []string{"test-fixtures/*.txt"},
    51  			files: allFiles,
    52  			expected: map[file.Coordinates]string{
    53  				file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    54  				file.NewLocation("test-fixtures/a-path.txt").Coordinates:       "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    55  			},
    56  		},
    57  		{
    58  			name:    "size-filter",
    59  			maxSize: 42,
    60  			globs:   []string{"**/*.txt"},
    61  			files:   allFiles,
    62  			expected: map[file.Coordinates]string{
    63  				file.NewLocation("test-fixtures/last/path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh",
    64  				file.NewLocation("test-fixtures/a-path.txt").Coordinates:    "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh",
    65  			},
    66  		},
    67  	}
    68  
    69  	for _, test := range tests {
    70  		t.Run(test.name, func(t *testing.T) {
    71  			c := NewCataloger(Config{
    72  				Globs:              test.globs,
    73  				SkipFilesAboveSize: test.maxSize,
    74  			})
    75  
    76  			resolver := file.NewMockResolverForPaths(test.files...)
    77  			actual, err := c.Catalog(context.Background(), resolver)
    78  			assert.NoError(t, err)
    79  			assert.Equal(t, test.expected, actual, "mismatched contents")
    80  
    81  		})
    82  	}
    83  }