github.com/CycloneDX/sbom-utility@v0.16.0/cmd/stats_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 "bufio" 23 "bytes" 24 "io/fs" 25 "log" 26 "os" 27 "testing" 28 29 "github.com/CycloneDX/sbom-utility/schema" 30 "github.com/CycloneDX/sbom-utility/utils" 31 ) 32 33 const ( 34 // Test "resource list" command 35 TEST_STATS_CDX_1_4_SAMPLE_XXL_1 = "test/stats/stats-cdx-1-4-sample-xxl-1.json" 36 ) 37 38 type StatsTestInfo struct { 39 CommonTestInfo 40 } 41 42 func (ti *StatsTestInfo) String() string { 43 buffer, _ := utils.EncodeAnyToDefaultIndentedJSONStr(ti) 44 return buffer.String() 45 } 46 47 func NewStatsTestInfoBasic(inputFile string, listFormat string, resultExpectedError error) *StatsTestInfo { 48 var ti = new(StatsTestInfo) 49 var pCommon = &ti.CommonTestInfo 50 pCommon.InitBasic(inputFile, listFormat, resultExpectedError) 51 return ti 52 } 53 54 // ------------------------------------------- 55 // resource list test helper functions 56 // ------------------------------------------- 57 func innerBufferedTestStatsList(testInfo *StatsTestInfo) (outputBuffer bytes.Buffer, err error) { 58 // Declare an output outputBuffer/outputWriter to use used during tests 59 var outputWriter = bufio.NewWriter(&outputBuffer) 60 // ensure all data is written to buffer before further validation 61 defer outputWriter.Flush() 62 63 var persistentFlags utils.PersistentCommandFlags 64 persistentFlags.OutputFormat = testInfo.OutputFormat 65 var statsFlags utils.StatsCommandFlags 66 67 err = ListStats(outputWriter, persistentFlags, statsFlags) 68 return 69 } 70 71 func innerTestStatsList(testInfo *StatsTestInfo) (outputBuffer bytes.Buffer, basicTestInfo string, err error) { 72 getLogger().Tracef("TestInfo: %s", testInfo) 73 74 // The command looks for the input filename in global flags struct 75 utils.GlobalFlags.PersistentFlags.InputFile = testInfo.InputFile 76 77 // Mock stdin if requested 78 if testInfo.MockStdin == true { 79 utils.GlobalFlags.PersistentFlags.InputFile = INPUT_TYPE_STDIN 80 file, err := os.Open(testInfo.InputFile) // For read access. 81 if err != nil { 82 log.Fatal(err) 83 } 84 85 // convert byte slice to io.Reader 86 savedStdIn := os.Stdin 87 // !!!Important restore stdin 88 defer func() { os.Stdin = savedStdIn }() 89 os.Stdin = file 90 } 91 92 // invoke resource list command with a byte buffer 93 outputBuffer, err = innerBufferedTestStatsList(testInfo) 94 95 return 96 } 97 98 // ---------------------------------------- 99 // Command flag tests 100 // ---------------------------------------- 101 102 // TBD 103 104 // ------------------------------------------- 105 // Test format unsupported (SPDX) 106 // ------------------------------------------- 107 func TestStatsListFormatUnsupportedSPDXMinReq(t *testing.T) { 108 ti := NewStatsTestInfoBasic( 109 TEST_SPDX_2_2_MIN_REQUIRED, 110 FORMAT_DEFAULT, 111 &schema.UnsupportedFormatError{}, 112 ) 113 114 // verify correct error is returned 115 innerTestStatsList(ti) 116 } 117 118 // ------------------------------------------- 119 // Data tests 120 // ------------------------------------------- 121 func TestStatsCdx14SampleXXL1(t *testing.T) { 122 ti := NewStatsTestInfoBasic( 123 TEST_STATS_CDX_1_4_SAMPLE_XXL_1, 124 FORMAT_DEFAULT, 125 &fs.PathError{}, 126 ) 127 128 // verify correct error is returned 129 innerTestStatsList(ti) 130 }