github.com/anchore/syft@v1.38.2/syft/format/encoders_test.go (about)

     1  package format
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/scylladb/go-set/strset"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/anchore/syft/internal"
    11  	"github.com/anchore/syft/syft/format/cyclonedxjson"
    12  	"github.com/anchore/syft/syft/format/cyclonedxxml"
    13  	"github.com/anchore/syft/syft/format/internal/cyclonedxutil"
    14  	"github.com/anchore/syft/syft/format/internal/spdxutil"
    15  	"github.com/anchore/syft/syft/format/spdxjson"
    16  	"github.com/anchore/syft/syft/format/spdxtagvalue"
    17  	"github.com/anchore/syft/syft/format/syftjson"
    18  	"github.com/anchore/syft/syft/format/template"
    19  	"github.com/anchore/syft/syft/sbom"
    20  )
    21  
    22  func Test_Encoders(t *testing.T) {
    23  	// this is an explicit test that the default encoders and encoders are the same and does not error
    24  	encs, err := DefaultEncodersConfig().Encoders()
    25  	require.NoError(t, err)
    26  	assert.NotEmpty(t, encs)
    27  
    28  	expected := expectedDefaultEncoders()
    29  	assertHasEncoders(t, expected, encs)
    30  	assertHasEncoders(t, expected, Encoders())
    31  }
    32  
    33  func expectedDefaultEncoders() *strset.Set {
    34  	expected := strset.New()
    35  	// note: template is not expected in the default encoders
    36  	expected.Add("syft-json@" + internal.JSONSchemaVersion) // TODO: support multiple versions
    37  	expected.Add("syft-table@")                             // no version
    38  	expected.Add("syft-text@")                              // no version
    39  	expected.Add("github-json@")                            // no version
    40  	expected.Add("purls@")                                  // no version
    41  	for _, v := range spdxjson.SupportedVersions() {
    42  		expected.Add("spdx-json@" + v)
    43  	}
    44  	for _, v := range spdxtagvalue.SupportedVersions() {
    45  		expected.Add("spdx-tag-value@" + v)
    46  	}
    47  	for _, v := range cyclonedxjson.SupportedVersions() {
    48  		expected.Add("cyclonedx-json@" + v)
    49  	}
    50  	for _, v := range cyclonedxxml.SupportedVersions() {
    51  		expected.Add("cyclonedx-xml@" + v)
    52  	}
    53  	return expected
    54  }
    55  
    56  func assertHasEncoders(t *testing.T, expected *strset.Set, encs []sbom.FormatEncoder) {
    57  	for _, enc := range encs {
    58  		assert.True(t, expected.Has(string(enc.ID())+"@"+enc.Version()), "missing: "+string(enc.ID())+"@"+enc.Version())
    59  	}
    60  
    61  	if t.Failed() {
    62  		t.Log("got encoders:")
    63  		for _, enc := range encs {
    64  			t.Log(" - " + string(enc.ID()) + "@" + enc.Version())
    65  		}
    66  	}
    67  }
    68  
    69  func TestEncodersConfig_Encoders(t *testing.T) {
    70  
    71  	tests := []struct {
    72  		name    string
    73  		cfg     EncodersConfig
    74  		want    *strset.Set
    75  		wantErr require.ErrorAssertionFunc
    76  	}{
    77  		{
    78  			name: "default",
    79  			cfg:  DefaultEncodersConfig(),
    80  			want: expectedDefaultEncoders(),
    81  		},
    82  		{
    83  			name: "with template",
    84  			cfg: func() EncodersConfig {
    85  				cfg := DefaultEncodersConfig()
    86  				cfg.Template.TemplatePath = "foo"
    87  				return cfg
    88  			}(),
    89  			want: func() *strset.Set {
    90  				expected := expectedDefaultEncoders()
    91  				expected.Add("template@")
    92  				return expected
    93  			}(),
    94  		},
    95  		{
    96  			name: "explicit versions template",
    97  			cfg: EncodersConfig{
    98  				Template:      template.DefaultEncoderConfig(),
    99  				SyftJSON:      syftjson.DefaultEncoderConfig(),
   100  				SPDXJSON:      spdxjson.DefaultEncoderConfig(),
   101  				SPDXTagValue:  spdxtagvalue.DefaultEncoderConfig(),
   102  				CyclonedxJSON: cyclonedxjson.DefaultEncoderConfig(),
   103  				CyclonedxXML:  cyclonedxxml.DefaultEncoderConfig(),
   104  			},
   105  			want: func() *strset.Set {
   106  				expected := strset.New()
   107  				// note: template is not expected in the default encoders
   108  				expected.Add("syft-json@" + internal.JSONSchemaVersion) // TODO: support multiple versions
   109  				expected.Add("syft-table@")                             // no version
   110  				expected.Add("syft-text@")                              // no version
   111  				expected.Add("github-json@")                            // no version
   112  				expected.Add("purls@")                                  // no version
   113  				expected.Add("spdx-json@" + spdxutil.DefaultVersion)
   114  				expected.Add("spdx-tag-value@" + spdxutil.DefaultVersion)
   115  				expected.Add("cyclonedx-json@" + cyclonedxutil.DefaultVersion)
   116  				expected.Add("cyclonedx-xml@" + cyclonedxutil.DefaultVersion)
   117  
   118  				return expected
   119  			}(),
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			if tt.wantErr == nil {
   125  				tt.wantErr = require.NoError
   126  			}
   127  
   128  			got, err := tt.cfg.Encoders()
   129  			tt.wantErr(t, err)
   130  			if err != nil {
   131  				return
   132  			}
   133  
   134  			assertHasEncoders(t, tt.want, got)
   135  		})
   136  	}
   137  }