github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/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/lineaje-labs/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(t *testing.T) {
    16  	f, err := NewFormatEncoder(EncoderConfig{
    17  		TemplatePath: "test-fixtures/csv.template",
    18  	})
    19  	require.NoError(t, err)
    20  
    21  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    22  		testutil.EncoderSnapshotTestConfig{
    23  			Subject:                     testutil.DirectoryInput(t, t.TempDir()),
    24  			Format:                      f,
    25  			UpdateSnapshot:              *updateSnapshot,
    26  			PersistRedactionsInSnapshot: true,
    27  			IsJSON:                      false,
    28  		},
    29  	)
    30  }
    31  
    32  func TestFormatWithOptionAndHasField(t *testing.T) {
    33  	f, err := NewFormatEncoder(EncoderConfig{
    34  		TemplatePath: "test-fixtures/csv-hasField.template",
    35  	})
    36  	require.NoError(t, err)
    37  
    38  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    39  		testutil.EncoderSnapshotTestConfig{
    40  			Subject:                     testutil.DirectoryInputWithAuthorField(t),
    41  			Format:                      f,
    42  			UpdateSnapshot:              *updateSnapshot,
    43  			PersistRedactionsInSnapshot: true,
    44  			IsJSON:                      false,
    45  		},
    46  	)
    47  
    48  }
    49  
    50  func TestFormatWithoutOptions(t *testing.T) {
    51  	f, err := NewFormatEncoder(DefaultEncoderConfig())
    52  	require.NoError(t, err)
    53  	err = f.Encode(nil, testutil.DirectoryInput(t, t.TempDir()))
    54  	assert.ErrorContains(t, err, "no template file provided")
    55  }