github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/cataloging/filecataloging/config_test.go (about) 1 package filecataloging 2 3 import ( 4 "crypto" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/anchore/syft/syft/file" 12 ) 13 14 func TestConfig_MarshalJSON(t *testing.T) { 15 tests := []struct { 16 name string 17 cfg Config 18 want []byte 19 wantErr require.ErrorAssertionFunc 20 }{ 21 { 22 name: "converts hashers to strings", 23 cfg: Config{ 24 Selection: file.FilesOwnedByPackageSelection, 25 Hashers: []crypto.Hash{crypto.SHA256}, 26 }, 27 want: []byte(`{"selection":"owned-by-package","hashers":["sha-256"],"content":{"globs":null,"skip-files-above-size":0}}`), 28 }, 29 } 30 for _, tt := range tests { 31 t.Run(tt.name, func(t *testing.T) { 32 if tt.wantErr == nil { 33 tt.wantErr = require.NoError 34 } 35 got, err := tt.cfg.MarshalJSON() 36 tt.wantErr(t, err) 37 if err != nil { 38 return 39 } 40 if d := cmp.Diff(got, tt.want); d != "" { 41 t.Errorf("MarshalJSON() mismatch (-want +got):\n%s", d) 42 } 43 }) 44 } 45 } 46 47 func TestConfig_UnmarshalJSON(t *testing.T) { 48 49 tests := []struct { 50 name string 51 data []byte 52 want Config 53 wantErr bool 54 }{ 55 { 56 name: "converts strings to hashers", 57 data: []byte(`{"selection":"owned-by-package","hashers":["sha-256"]}`), 58 want: Config{ 59 Selection: file.FilesOwnedByPackageSelection, 60 Hashers: []crypto.Hash{crypto.SHA256}, 61 }, 62 }, 63 } 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 cfg := Config{} 67 if err := cfg.UnmarshalJSON(tt.data); (err != nil) != tt.wantErr { 68 t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 69 } 70 71 assert.Equal(t, tt.want, cfg) 72 }) 73 } 74 }