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

     1  package spdxjson
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/formats/internal/testutils"
     8  )
     9  
    10  var updateSnapshot = flag.Bool("update-spdx-json", false, "update the *.golden files for spdx-json encoders")
    11  var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing")
    12  
    13  func TestSPDXJSONDirectoryEncoder(t *testing.T) {
    14  	dir := t.TempDir()
    15  	testutils.AssertEncoderAgainstGoldenSnapshot(t,
    16  		testutils.EncoderSnapshotTestConfig{
    17  			Subject:                     testutils.DirectoryInput(t, dir),
    18  			Format:                      Format(),
    19  			UpdateSnapshot:              *updateSnapshot,
    20  			PersistRedactionsInSnapshot: true,
    21  			IsJSON:                      true,
    22  			Redactor:                    redactor(dir),
    23  		},
    24  	)
    25  }
    26  
    27  func TestSPDXJSONImageEncoder(t *testing.T) {
    28  	testImage := "image-simple"
    29  	testutils.AssertEncoderAgainstGoldenImageSnapshot(t,
    30  		testutils.ImageSnapshotTestConfig{
    31  			Image:               testImage,
    32  			UpdateImageSnapshot: *updateImage,
    33  		},
    34  		testutils.EncoderSnapshotTestConfig{
    35  			Subject:                     testutils.ImageInput(t, testImage, testutils.FromSnapshot()),
    36  			Format:                      Format(),
    37  			UpdateSnapshot:              *updateSnapshot,
    38  			PersistRedactionsInSnapshot: true,
    39  			IsJSON:                      true,
    40  			Redactor:                    redactor(),
    41  		},
    42  	)
    43  }
    44  
    45  func TestSPDXRelationshipOrder(t *testing.T) {
    46  	testImage := "image-simple"
    47  
    48  	s := testutils.ImageInput(t, testImage, testutils.FromSnapshot())
    49  	testutils.AddSampleFileRelationships(&s)
    50  
    51  	testutils.AssertEncoderAgainstGoldenImageSnapshot(t,
    52  		testutils.ImageSnapshotTestConfig{
    53  			Image:               testImage,
    54  			UpdateImageSnapshot: *updateImage,
    55  		},
    56  		testutils.EncoderSnapshotTestConfig{
    57  			Subject:                     s,
    58  			Format:                      Format(),
    59  			UpdateSnapshot:              *updateSnapshot,
    60  			PersistRedactionsInSnapshot: true,
    61  			IsJSON:                      true,
    62  			Redactor:                    redactor(),
    63  		},
    64  	)
    65  }
    66  
    67  func redactor(values ...string) testutils.Redactor {
    68  	return testutils.NewRedactions().
    69  		WithValuesRedacted(values...).
    70  		WithPatternRedactors(
    71  			map[string]string{
    72  				// each SBOM reports the time it was generated, which is not useful during snapshot testing
    73  				`"created":\s+"[^"]*"`: `"created":"redacted"`,
    74  
    75  				// each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing
    76  				`"documentNamespace":\s+"[^"]*"`: `"documentNamespace":"redacted"`,
    77  
    78  				// the license list will be updated periodically, the value here should not be directly tested in snapshot tests
    79  				`"licenseListVersion":\s+"[^"]*"`: `"licenseListVersion":"redacted"`,
    80  			},
    81  		)
    82  }