github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/file/cataloger/filecontent/cataloger_test.go (about)

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