github.com/verrazzano/verrazzano@v1.7.0/tools/charts-manager/vcm/tests/pkg/helpers/helpers.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package helpers
     5  
     6  import (
     7  	"os"
     8  
     9  	testhelpers "github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    10  	"k8s.io/cli-runtime/pkg/genericclioptions"
    11  )
    12  
    13  const (
    14  	DummyError   = "dummy error"
    15  	FlagNotFound = "%s flag not supported by command"
    16  )
    17  
    18  // ContextSetup creates a default RootCmdContext.
    19  func ContextSetup() (*testhelpers.FakeRootCmdContext, func(), error) {
    20  	stdoutFile, stderrFile, err := createStdTempFiles()
    21  	if err != nil {
    22  		return nil, nil, err
    23  	}
    24  	return testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: stdoutFile, ErrOut: stderrFile}), func() {
    25  		os.Remove(stdoutFile.Name())
    26  		os.Remove(stderrFile.Name())
    27  	}, nil
    28  }
    29  
    30  // createStdTempFiles creates temporary files for stdout and stderr.
    31  func createStdTempFiles() (*os.File, *os.File, error) {
    32  	stdoutFile, err := os.CreateTemp("", "tmpstdout")
    33  	if err != nil {
    34  		return nil, nil, err
    35  	}
    36  
    37  	stderrFile, err := os.CreateTemp("", "tmpstderr")
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  
    42  	return stdoutFile, stderrFile, nil
    43  }