github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/template/encoder_test.go (about)

     1  package template
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/anchore/syft/syft/format/internal/testutil"
    11  )
    12  
    13  var updateSnapshot = flag.Bool("update-template", false, "update the *.golden files for json encoders")
    14  
    15  func TestFormatWithOption_Legacy(t *testing.T) {
    16  	f, err := NewFormatEncoder(EncoderConfig{
    17  		TemplatePath: "test-fixtures/legacy/csv.template",
    18  		Legacy:       true,
    19  	})
    20  	require.NoError(t, err)
    21  
    22  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    23  		testutil.EncoderSnapshotTestConfig{
    24  			Subject:                     testutil.DirectoryInput(t, t.TempDir()),
    25  			Format:                      f,
    26  			UpdateSnapshot:              *updateSnapshot,
    27  			PersistRedactionsInSnapshot: true,
    28  			IsJSON:                      false,
    29  		},
    30  	)
    31  }
    32  
    33  func TestFormatWithOptionAndHasField_Legacy(t *testing.T) {
    34  	f, err := NewFormatEncoder(EncoderConfig{
    35  		TemplatePath: "test-fixtures/legacy/csv-hasField.template",
    36  		Legacy:       true,
    37  	})
    38  	require.NoError(t, err)
    39  
    40  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    41  		testutil.EncoderSnapshotTestConfig{
    42  			Subject:                     testutil.DirectoryInputWithAuthorField(t),
    43  			Format:                      f,
    44  			UpdateSnapshot:              *updateSnapshot,
    45  			PersistRedactionsInSnapshot: true,
    46  			IsJSON:                      false,
    47  		},
    48  	)
    49  }
    50  
    51  func TestFormatWithOption(t *testing.T) {
    52  	f, err := NewFormatEncoder(EncoderConfig{
    53  		TemplatePath: "test-fixtures/csv.template",
    54  	})
    55  	require.NoError(t, err)
    56  
    57  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    58  		testutil.EncoderSnapshotTestConfig{
    59  			Subject:                     testutil.DirectoryInput(t, t.TempDir()),
    60  			Format:                      f,
    61  			UpdateSnapshot:              *updateSnapshot,
    62  			PersistRedactionsInSnapshot: true,
    63  			IsJSON:                      false,
    64  		},
    65  	)
    66  }
    67  
    68  func TestFormatWithOptionAndHasField(t *testing.T) {
    69  	f, err := NewFormatEncoder(EncoderConfig{
    70  		TemplatePath: "test-fixtures/csv-hasField.template",
    71  	})
    72  	require.NoError(t, err)
    73  
    74  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    75  		testutil.EncoderSnapshotTestConfig{
    76  			Subject:                     testutil.DirectoryInputWithAuthorField(t),
    77  			Format:                      f,
    78  			UpdateSnapshot:              *updateSnapshot,
    79  			PersistRedactionsInSnapshot: true,
    80  			IsJSON:                      false,
    81  		},
    82  	)
    83  }
    84  
    85  func TestFormatWithoutOptions(t *testing.T) {
    86  	f, err := NewFormatEncoder(DefaultEncoderConfig())
    87  	require.NoError(t, err)
    88  	err = f.Encode(nil, testutil.DirectoryInput(t, t.TempDir()))
    89  	assert.ErrorContains(t, err, "no template file provided")
    90  }