github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/template/encoder_test.go (about)

     1  package template
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/syft/syft/formats/internal/testutils"
    10  )
    11  
    12  var updateSnapshot = flag.Bool("update-template", false, "update the *.golden files for json encoders")
    13  
    14  func TestFormatWithOption(t *testing.T) {
    15  	f := OutputFormat{}
    16  	f.SetTemplatePath("test-fixtures/csv.template")
    17  
    18  	testutils.AssertEncoderAgainstGoldenSnapshot(t,
    19  		testutils.EncoderSnapshotTestConfig{
    20  			Subject:                     testutils.DirectoryInput(t, t.TempDir()),
    21  			Format:                      f,
    22  			UpdateSnapshot:              *updateSnapshot,
    23  			PersistRedactionsInSnapshot: true,
    24  			IsJSON:                      false,
    25  		},
    26  	)
    27  }
    28  
    29  func TestFormatWithOptionAndHasField(t *testing.T) {
    30  	f := OutputFormat{}
    31  	f.SetTemplatePath("test-fixtures/csv-hasField.template")
    32  
    33  	testutils.AssertEncoderAgainstGoldenSnapshot(t,
    34  		testutils.EncoderSnapshotTestConfig{
    35  			Subject:                     testutils.DirectoryInputWithAuthorField(t),
    36  			Format:                      f,
    37  			UpdateSnapshot:              *updateSnapshot,
    38  			PersistRedactionsInSnapshot: true,
    39  			IsJSON:                      false,
    40  		},
    41  	)
    42  
    43  }
    44  
    45  func TestFormatWithoutOptions(t *testing.T) {
    46  	f := Format()
    47  	err := f.Encode(nil, testutils.DirectoryInput(t, t.TempDir()))
    48  	assert.ErrorContains(t, err, "no template file: please provide a template path")
    49  }