github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/test/cli/all_formats_expressible_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/anchore/syft/syft/format"
    10  	"github.com/anchore/syft/syft/format/template"
    11  )
    12  
    13  func TestAllFormatsExpressible(t *testing.T) {
    14  	commonAssertions := []traitAssertion{
    15  		func(tb testing.TB, stdout, _ string, _ int) {
    16  			tb.Helper()
    17  			if len(stdout) < 1000 {
    18  				tb.Errorf("there may not be any report output (len=%d)", len(stdout))
    19  			}
    20  		},
    21  		assertSuccessfulReturnCode,
    22  	}
    23  
    24  	encs := format.NewEncoderCollection(format.Encoders()...)
    25  	formatIDs := encs.IDs()
    26  	require.NotEmpty(t, formatIDs)
    27  	for _, o := range formatIDs {
    28  		t.Run(fmt.Sprintf("format:%s", o), func(t *testing.T) {
    29  			args := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", string(o)}
    30  			if o == template.ID {
    31  				args = append(args, "-t", "test-fixtures/csv.template")
    32  			}
    33  
    34  			cmd, stdout, stderr := runSyft(t, nil, args...)
    35  			for _, traitFn := range commonAssertions {
    36  				traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
    37  			}
    38  			logOutputOnFailure(t, cmd, stdout, stderr)
    39  		})
    40  	}
    41  }
    42  
    43  func Test_formatVersionsExpressible(t *testing.T) {
    44  	tests := []struct {
    45  		format    string
    46  		assertion traitAssertion
    47  	}{
    48  		{
    49  			format:    "spdx@2.1",
    50  			assertion: assertInOutput("SPDXVersion: SPDX-2.1"),
    51  		},
    52  		{
    53  			format:    "spdx@2.2",
    54  			assertion: assertInOutput("SPDXVersion: SPDX-2.2"),
    55  		},
    56  		{
    57  			format:    "spdx@2.3",
    58  			assertion: assertInOutput("SPDXVersion: SPDX-2.3"),
    59  		},
    60  		{
    61  			format:    "spdx-json@2.2",
    62  			assertion: assertInOutput(`"spdxVersion":"SPDX-2.2"`),
    63  		},
    64  		{
    65  			format:    "spdx-json@2.3",
    66  			assertion: assertInOutput(`"spdxVersion":"SPDX-2.3"`),
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		t.Run(test.format, func(t *testing.T) {
    72  			args := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", test.format}
    73  			cmd, stdout, stderr := runSyft(t, nil, args...)
    74  			test.assertion(t, stdout, stderr, cmd.ProcessState.ExitCode())
    75  			logOutputOnFailure(t, cmd, stdout, stderr)
    76  		})
    77  	}
    78  }