github.com/CycloneDX/sbom-utility@v0.16.0/cmd/report_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package cmd 20 21 import ( 22 "bytes" 23 "strings" 24 "testing" 25 26 "github.com/CycloneDX/sbom-utility/utils" 27 ) 28 29 func innerRunReportResultTests(t *testing.T, testInfo *CommonTestInfo, outputBuffer bytes.Buffer, outputError error) (err error) { 30 getLogger().Tracef("TestInfo: %s", testInfo) 31 32 // TEST: Expected error matches actual error 33 if testInfo.ResultExpectedError != nil { 34 // NOTE: err = nil will also fail if error was expected 35 if !ErrorTypesMatch(outputError, testInfo.ResultExpectedError) { 36 err = getLogger().Errorf("expected error: %T, actual error: %T", testInfo.ResultExpectedError, outputError) 37 t.Error(err.Error()) 38 } 39 // TODO: getLogger().Tracef("success") 40 // Always return (with the actual error); as subsequent tests are rendered invalid 41 return outputError 42 } 43 44 // TEST: Unexpected error: return immediately/do not test output/results 45 if outputError != nil { 46 err = getLogger().Errorf("test failed: %s: detail: %s ", testInfo, outputError.Error()) 47 t.Error(err.Error()) 48 return 49 } 50 51 // TEST: Line Count (total) 52 if testInfo.ResultExpectedLineCount != TI_RESULT_DEFAULT_LINE_COUNT { 53 verifyFileLineCountAndIndentation(t, outputBuffer, testInfo) 54 } 55 56 // TEST: Line contains a set of string values 57 // TODO: support any number of row/values in test info. structure 58 if len(testInfo.ResultLineContainsValues) > 0 { 59 matchFoundLine, matchFound := bufferLineContainsValues(outputBuffer, testInfo.ResultLineContainsValuesAtLineNum, testInfo.ResultLineContainsValues...) 60 if !matchFound { 61 err = getLogger().Errorf("output does not contain expected values: `%v` at line: %v\n", strings.Join(testInfo.ResultLineContainsValues, ","), testInfo.ResultLineContainsValuesAtLineNum) 62 t.Error(err.Error()) 63 return 64 } 65 getLogger().Tracef("output contains expected values: `%v` at line: %v\n", testInfo.ResultLineContainsValues, matchFoundLine) 66 } 67 68 // TEST: valid JSON if format JSON 69 // TODO: Allow caller to pass in CDX struct type to validate JSON array contains that type 70 if testInfo.OutputFormat == FORMAT_JSON { 71 // Use Marshal to test for validity 72 if !utils.IsValidJsonRaw(outputBuffer.Bytes()) { 73 err = getLogger().Errorf("output did not contain valid format data; expected: `%s`", FORMAT_JSON) 74 t.Error(err.Error()) 75 t.Logf("%s", outputBuffer.String()) 76 return 77 } 78 getLogger().Tracef("success: validated output format: `%s`", FORMAT_JSON) 79 } 80 81 // TODO: add general validation for CSV and Markdown formats 82 if testInfo.OutputFormat == FORMAT_CSV { 83 getLogger().Tracef("Testing format: %s", FORMAT_CSV) 84 } 85 86 if testInfo.OutputFormat == FORMAT_MARKDOWN { 87 getLogger().Tracef("Testing format: %s", FORMAT_MARKDOWN) 88 } 89 90 return 91 }