github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/github/encoder_test.go (about)

     1  package github
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/lineaje-labs/syft/syft/format/internal/testutil"
     8  )
     9  
    10  var updateSnapshot = flag.Bool("update-github", false, "update the *.golden files for github encoders")
    11  var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing")
    12  
    13  func TestGithubDirectoryEncoder(t *testing.T) {
    14  	dir := t.TempDir()
    15  	testutil.AssertEncoderAgainstGoldenSnapshot(t,
    16  		testutil.EncoderSnapshotTestConfig{
    17  			Subject:                     testutil.DirectoryInput(t, dir),
    18  			Format:                      NewFormatEncoder(),
    19  			UpdateSnapshot:              *updateSnapshot,
    20  			PersistRedactionsInSnapshot: true,
    21  			IsJSON:                      false,
    22  			Redactor:                    redactor(dir),
    23  		},
    24  	)
    25  }
    26  
    27  func TestGithubImageEncoder(t *testing.T) {
    28  	testImage := "image-simple"
    29  	testutil.AssertEncoderAgainstGoldenImageSnapshot(t,
    30  		testutil.ImageSnapshotTestConfig{
    31  			Image:               testImage,
    32  			UpdateImageSnapshot: *updateImage,
    33  		},
    34  		testutil.EncoderSnapshotTestConfig{
    35  			Subject:                     testutil.ImageInput(t, testImage),
    36  			Format:                      NewFormatEncoder(),
    37  			UpdateSnapshot:              *updateSnapshot,
    38  			PersistRedactionsInSnapshot: true,
    39  			IsJSON:                      false,
    40  			Redactor:                    redactor(),
    41  		},
    42  	)
    43  }
    44  
    45  func redactor(values ...string) testutil.Redactor {
    46  	return testutil.NewRedactions().
    47  		WithValuesRedacted(values...).
    48  		WithPatternRedactors(
    49  			map[string]string{
    50  				// dates
    51  				`"scanned":\s*"[^"]+"`: `"scanned":"redacted"`,
    52  
    53  				// image metadata
    54  				`"syft:filesystem":\s*"[^"]+"`: `"syft:filesystem":"redacted"`,
    55  			},
    56  		)
    57  }