github.com/CycloneDX/sbom-utility@v0.16.0/cmd/schema_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  	"testing"
    25  
    26  	"github.com/CycloneDX/sbom-utility/common"
    27  	"github.com/CycloneDX/sbom-utility/utils"
    28  )
    29  
    30  // -------------------------------------------
    31  // resource list test helper functions
    32  // -------------------------------------------
    33  
    34  func innerBufferedTestSchemaList(pTestInfo *CommonTestInfo, whereFilters []common.WhereFilter) (outputBuffer bytes.Buffer, err error) {
    35  	// Declare an output outputBuffer/outputWriter to use used during tests
    36  	var outputWriter = bufio.NewWriter(&outputBuffer)
    37  	// ensure all data is written to buffer before further validation
    38  	defer outputWriter.Flush()
    39  
    40  	// TODO: test for different output formats
    41  	utils.GlobalFlags.PersistentFlags.OutputFormat = pTestInfo.OutputFormat
    42  	err = ListSchemas(outputWriter, utils.GlobalFlags.PersistentFlags, whereFilters)
    43  	return
    44  }
    45  
    46  func innerTestSchemaList(t *testing.T, pTestInfo *CommonTestInfo) (outputBuffer bytes.Buffer, basicTestInfo string, err error) {
    47  	getLogger().Tracef("TestInfo: %s", pTestInfo)
    48  
    49  	// Parse out --where filters and exit out if error detected
    50  	whereFilters, err := prepareWhereFilters(t, pTestInfo)
    51  	if err != nil {
    52  		return
    53  	}
    54  
    55  	// invoke resource list command with a byte buffer
    56  	outputBuffer, err = innerBufferedTestSchemaList(pTestInfo, whereFilters)
    57  
    58  	// Run all common tests against "result" values in the CommonTestInfo struct
    59  	err = innerRunReportResultTests(t, pTestInfo, outputBuffer, err)
    60  
    61  	return
    62  }
    63  
    64  // ----------------------------------------
    65  // Command tests
    66  // ----------------------------------------
    67  
    68  func TestSchemaListText(t *testing.T) {
    69  	ti := NewCommonTestInfo()
    70  	ti.OutputFormat = FORMAT_TEXT
    71  	// verify correct error is returned
    72  	innerTestSchemaList(t, ti)
    73  }