Skip to content

Commit

Permalink
Fix confusion in boolean parameter order in tests
Browse files Browse the repository at this point in the history
Prevent it from happening again by introducing an enum

Co-authored-by: Philipp Stehle <[email protected]>
  • Loading branch information
2 people authored and ForestEckhardt committed Feb 23, 2023
1 parent e1f6949 commit d41178d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
15 changes: 11 additions & 4 deletions sbom/internal/formats/common/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ func FromSnapshot() ImageOption {
}
}

func AssertEncoderAgainstGoldenImageSnapshot(t *testing.T, format sbom.Format, sbom sbom.SBOM, testImage string, updateSnapshot bool, json bool, redactors ...redactor) {
type Type int

const (
TypePlain Type = iota
TypeJson
)

func AssertEncoderAgainstGoldenImageSnapshot(t *testing.T, format sbom.Format, sbom sbom.SBOM, testImage string, updateSnapshot bool, contentType Type, redactors ...redactor) {
var buffer bytes.Buffer

// grab the latest image contents and persist
Expand All @@ -63,7 +70,7 @@ func AssertEncoderAgainstGoldenImageSnapshot(t *testing.T, format sbom.Format, s
expected = r(expected)
}

if json {
if contentType == TypeJson {
require.JSONEq(t, string(expected), string(actual))
} else if !bytes.Equal(expected, actual) {
// assert that the golden file snapshot matches the actual contents
Expand All @@ -73,7 +80,7 @@ func AssertEncoderAgainstGoldenImageSnapshot(t *testing.T, format sbom.Format, s
}
}

func AssertEncoderAgainstGoldenSnapshot(t *testing.T, format sbom.Format, sbom sbom.SBOM, updateSnapshot bool, json bool, redactors ...redactor) {
func AssertEncoderAgainstGoldenSnapshot(t *testing.T, format sbom.Format, sbom sbom.SBOM, updateSnapshot bool, contentType Type, redactors ...redactor) {
var buffer bytes.Buffer

err := format.Encode(&buffer, sbom)
Expand All @@ -94,7 +101,7 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, format sbom.Format, sbom s
expected = r(expected)
}

if json {
if contentType == TypeJson {
require.JSONEq(t, string(expected), string(actual))
} else if !bytes.Equal(expected, actual) {
dmp := diffmatchpatch.New()
Expand Down
4 changes: 2 additions & 2 deletions sbom/internal/formats/cyclonedx13/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestCycloneDxDirectoryEncoder(t *testing.T) {
Format(),
testutils.DirectoryInput(t),
*updateCycloneDx,
true,
testutils.TypeJson,
cycloneDxRedactor,
)
}
Expand All @@ -27,7 +27,7 @@ func TestCycloneDxImageEncoder(t *testing.T) {
testutils.ImageInput(t, testImage),
testImage,
*updateCycloneDx,
true,
testutils.TypeJson,
cycloneDxRedactor,
)
}
Expand Down
6 changes: 3 additions & 3 deletions sbom/internal/formats/spdx22/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestSPDXJSONDirectoryEncoder(t *testing.T) {
Format(),
testutils.DirectoryInput(t),
*updateSpdxJson,
true,
testutils.TypeJson,
spdxJsonRedactor,
)
}
Expand All @@ -27,7 +27,7 @@ func TestSPDXJSONImageEncoder(t *testing.T) {
testutils.ImageInput(t, testImage, testutils.FromSnapshot()),
testImage,
*updateSpdxJson,
true,
testutils.TypeJson,
spdxJsonRedactor,
)
}
Expand All @@ -41,7 +41,7 @@ func TestSPDXRelationshipOrder(t *testing.T) {
s,
testImage,
*updateSpdxJson,
true,
testutils.TypeJson,
spdxJsonRedactor,
)
}
Expand Down
6 changes: 3 additions & 3 deletions sbom/internal/formats/syft2/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestDirectoryEncoder(t *testing.T) {
testutils.AssertEncoderAgainstGoldenSnapshot(t,
Format(),
testutils.DirectoryInput(t),
true,
*updateJson,
testutils.TypeJson,
)
}

Expand All @@ -34,8 +34,8 @@ func TestImageEncoder(t *testing.T) {
Format(),
testutils.ImageInput(t, testImage, testutils.FromSnapshot()),
testImage,
true,
*updateJson,
testutils.TypeJson,
)
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func TestEncodeFullJSONDocument(t *testing.T) {
testutils.AssertEncoderAgainstGoldenSnapshot(t,
Format(),
s,
true,
*updateJson,
testutils.TypeJson,
)
}
6 changes: 3 additions & 3 deletions sbom/internal/formats/syft301/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestDirectoryEncoder(t *testing.T) {
testutils.AssertEncoderAgainstGoldenSnapshot(t,
Format(),
testutils.DirectoryInput(t),
true,
*updateJson,
testutils.TypeJson,
)
}

Expand All @@ -32,8 +32,8 @@ func TestImageEncoder(t *testing.T) {
Format(),
testutils.ImageInput(t, testImage, testutils.FromSnapshot()),
testImage,
true,
*updateJson,
testutils.TypeJson,
)
}

Expand Down Expand Up @@ -201,7 +201,7 @@ func TestEncodeFullJSONDocument(t *testing.T) {
testutils.AssertEncoderAgainstGoldenSnapshot(t,
Format(),
s,
true,
*updateJson,
testutils.TypeJson,
)
}

0 comments on commit d41178d

Please sign in to comment.