Skip to content

Commit

Permalink
Add additional indentation tests to Trim test group
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Rutkowski <[email protected]>
  • Loading branch information
mrutkows committed Jan 4, 2024
1 parent 1bdf4c1 commit 83591a8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (ti *CommonTestInfo) CreateTemporaryTestOutputFilename(relativeFilename str
trimmedFilename := strings.TrimLeft(relativeFilename, strconv.QuoteRune(os.PathSeparator))
if testFunctionName != "" {
lastIndex := strings.LastIndex(trimmedFilename, string(os.PathSeparator))
// insert variant as last path...
// insert test function name (as a variant since test files are reused) as last path...
if lastIndex > 0 {
path := trimmedFilename[0:lastIndex]
base := trimmedFilename[lastIndex:]
Expand Down Expand Up @@ -335,7 +335,7 @@ func verifyFileLineCountAndIndentation(t *testing.T, buffer bytes.Buffer, cti *C

if cti.ResultExpectedLineCount != TI_RESULT_DEFAULT_LINE_COUNT {
if numLines != cti.ResultExpectedLineCount {
err = fmt.Errorf("invalid test output result: expected: `%v` lines, actual: `%v", cti.ResultExpectedLineCount, numLines)
err = fmt.Errorf("invalid test output result: expected: %v lines, actual: %v", cti.ResultExpectedLineCount, numLines)
t.Error(err)
}
getLogger().Tracef("success: output contained expected line count: %v", cti.ResultExpectedLineCount)
Expand All @@ -345,7 +345,7 @@ func verifyFileLineCountAndIndentation(t *testing.T, buffer bytes.Buffer, cti *C
line := lines[cti.ResultExpectedIndentAtLineNum]
//fmt.Printf("testing indent: %v at %v\n", cti.ResultExpectedIndentLength, cti.ResultExpectedIndentAtLineNum)
if spaceCount := numberOfLeadingSpaces(line); spaceCount != cti.ResultExpectedIndentLength {
t.Errorf("invalid test result: expected indent:`%v`, actual: `%v", cti.ResultExpectedIndentLength, spaceCount)
t.Errorf("invalid test result: expected indent: %v, actual: %v", cti.ResultExpectedIndentLength, spaceCount)
}
}
getLogger().Tracef("success: output contained expected indent length: %v, at line: %v", cti.ResultExpectedIndentLength, cti.ResultLineContainsValuesAtLineNum)
Expand Down
32 changes: 25 additions & 7 deletions cmd/trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ func TestTrimCdx14PreserveUnencodedChars(t *testing.T) {
func TestTrimCdx14ComponentPropertiesSampleXXLBuffered(t *testing.T) {
ti := NewTrimTestInfo(TEST_TRIM_CDX_1_4_SAMPLE_XXL_1, nil)
ti.Keys = append(ti.Keys, "properties")
ti.ResultExpectedByteSize = 8123018
outputBuffer, _ := innerBufferedTestTrim(t, ti)
// TODO: verify "after" trim lengths and content have removed properties
// verify "after" trim lengths and content have removed properties
getLogger().Tracef("Len(outputBuffer): `%v`\n", outputBuffer.Len())
if ti.ResultExpectedByteSize > 0 {
if outputBuffer.Len() != ti.ResultExpectedByteSize {
t.Error(fmt.Errorf("invalid trim result size (bytes): expected: %v, actual: %v", ti.ResultExpectedByteSize, outputBuffer.Len()))
}
}
}

// TODO: enable for when we have a "from" parameter to limit trim scope
Expand All @@ -274,10 +280,6 @@ func TestTrimCdx15MultipleKeys(t *testing.T) {
if err != nil {
t.Error(err)
}
err = VerifyTrimOutputFileResult(t, *ti)
if err != nil {
t.Error(err)
}
}

func TestTrimCdx15Properties(t *testing.T) {
Expand Down Expand Up @@ -338,13 +340,16 @@ func TestTrimCdx15AllIncrementallyFromSmallSample(t *testing.T) {
}
}

func TestTrimCdx15FooFromTools(t *testing.T) {
func TestTrimCdx15FooFromToolsAndTestJsonIndent(t *testing.T) {
ti := NewTrimTestInfo(TEST_TRIM_CDX_1_5_SAMPLE_MEDIUM_1, nil)
ti.Keys = append(ti.Keys, "foo")
ti.FromPaths = []string{"metadata.tools"}
ti.OutputFile = "" // ti.CreateTemporaryFilename(TEST_TRIM_CDX_1_5_SAMPLE_MEDIUM_1)
//ti.OutputFile = ti.CreateTemporaryTestOutputFilename(TEST_TRIM_CDX_1_5_SAMPLE_MEDIUM_1)
ti.OutputIndent = 2 // Matches the space indent of the test input file
ti.ResultExpectedByteSize = 4292
ti.ResultExpectedLineCount = 194
ti.ResultExpectedIndentLength = 2
ti.ResultExpectedIndentAtLineNum = 1

buffer, _, err := innerTestTrim(t, ti)
if err != nil {
Expand All @@ -362,6 +367,19 @@ func TestTrimCdx15FooFromTools(t *testing.T) {
if !contains {
t.Error(fmt.Errorf("invalid trim result: string not found: %s", TEST_STRING_1))
}

verifyFileLineCountAndIndentation(t, buffer, &ti.CommonTestInfo)

// verify indent continues to use multiples of 2
ti.ResultExpectedIndentLength = 4
ti.ResultExpectedIndentAtLineNum = 6
verifyFileLineCountAndIndentation(t, buffer, &ti.CommonTestInfo)
ti.ResultExpectedIndentLength = 6
ti.ResultExpectedIndentAtLineNum = 8
verifyFileLineCountAndIndentation(t, buffer, &ti.CommonTestInfo)
ti.ResultExpectedIndentLength = 4
ti.ResultExpectedIndentAtLineNum = 30
verifyFileLineCountAndIndentation(t, buffer, &ti.CommonTestInfo)
}

func TestTrimCdx14SourceFromVulnerabilities(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/vulnerability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func TestVulnListCdx13JSON(t *testing.T) {
FORMAT_JSON,
nil)
// Note: this value will keep going down as we add more custom marshallers for vuln. structs
testInfo.ResultExpectedLineCount = 186
result, _, _ := innerTestVulnList(t, testInfo, VULN_TEST_DEFAULT_FLAGS)
getLogger().Debugf("result:\n%s", result.String())
testInfo.ResultExpectedLineCount = 191
innerTestVulnList(t, testInfo, VULN_TEST_DEFAULT_FLAGS)
//getLogger().Debugf("result:\n%s", result.String())
}

// -------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions schema/cyclonedx_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func (value *CDXVulnerabilityReference) MarshalJSON() ([]byte, error) {
return json.Marshal(temp)
}

// type CDXVulnerabilitySource struct {
// Url string `json:"url,omitempty"` // v1.4
// Name string `json:"name,omitempty"` // v1.4
// }
func (value *CDXVulnerabilitySource) MarshalJSON() ([]byte, error) {
temp := map[string]interface{}{}
if len(value.Url) > 0 {
Expand Down Expand Up @@ -276,11 +280,18 @@ func (value *CDXCredit) MarshalJSON() ([]byte, error) {
return json.Marshal(temp)
}

// type CDXAffect struct {
// Versions *[]CDXVersionRange `json:"versions,omitempty"` // v1.4: anon. type
// Ref *CDXRefLinkType `json:"ref,omitempty"` // v1.5: added
// }
func (value *CDXAffect) MarshalJSON() ([]byte, error) {
temp := map[string]interface{}{}
if value.Versions != nil && len(*value.Versions) > 0 {
temp["versions"] = value.Versions
}
if value.Ref != nil && *value.Ref != "" {
temp["ref"] = value.Ref
}
if len(temp) == 0 {
return BYTE_ENCODED_ZERO_STRUCT, nil
}
Expand Down

0 comments on commit 83591a8

Please sign in to comment.