github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/options/file_test.go (about)

     1  package options
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/file"
     9  )
    10  
    11  func Test_fileConfig_PostLoad(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		cfg     fileConfig
    15  		assert  func(t *testing.T, cfg fileConfig)
    16  		wantErr assert.ErrorAssertionFunc
    17  	}{
    18  		{
    19  			name: "deduplicate digests",
    20  			cfg: fileConfig{
    21  				Metadata: fileMetadata{
    22  					Selection: file.NoFilesSelection,
    23  					Digests:   []string{"sha1", "sha1"},
    24  				},
    25  			},
    26  			assert: func(t *testing.T, cfg fileConfig) {
    27  				assert.Equal(t, []string{"sha1"}, cfg.Metadata.Digests)
    28  			},
    29  		},
    30  		{
    31  			name: "error on invalid selection",
    32  			cfg: fileConfig{
    33  				Metadata: fileMetadata{
    34  					Selection: file.Selection("invalid"),
    35  				},
    36  			},
    37  			wantErr: assert.Error,
    38  		},
    39  		{
    40  			name:    "error on empty selection",
    41  			cfg:     fileConfig{},
    42  			wantErr: assert.Error,
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			if tt.wantErr == nil {
    48  				tt.wantErr = assert.NoError
    49  			}
    50  			tt.wantErr(t, tt.cfg.PostLoad())
    51  			if tt.assert != nil {
    52  				tt.assert(t, tt.cfg)
    53  			}
    54  		})
    55  	}
    56  }