90 lines
2.5 KiB
Go
Raw Normal View History

package testutil
2019-04-29 13:35:13 -05:00
import (
"bytes"
"flag"
"io/ioutil"
"os"
2019-04-29 13:35:13 -05:00
"path/filepath"
"strings"
2019-04-29 13:35:13 -05:00
"testing"
"github.com/spf13/cobra"
2019-04-29 13:35:13 -05:00
)
// UpdateGolden writes out the golden files with the latest values, rather than failing the test.
var shouldUpdateGolden = flag.Bool("update", false, "update golden files")
2019-04-29 13:35:13 -05:00
const (
testdataDir = "testdata"
goldenDirSuffix = "GoldenOutput"
goldenFileSuffix = ".golden"
)
2019-04-29 13:35:13 -05:00
// CmdTest is a command to be run on the command line as a test
2019-04-29 13:35:13 -05:00
type CmdTest struct {
// The name of the test. This will be used when generating golden
// files
Name string
// The values that would be inputted to airshipctl as commands, flags,
// and arguments. The initial "airshipctl" is implied
2019-05-23 14:34:28 -05:00
CmdLine string
// The instatiated version of the root airshipctl command to test
Cmd *cobra.Command
}
// RunTest either asserts that a specific command's output matches the expected
// output from its golden file, or generates golden files if the -update flag
// is passed
func RunTest(t *testing.T, test *CmdTest) {
cmd := test.Cmd
actual := &bytes.Buffer{}
cmd.SetOutput(actual)
args := strings.Fields(test.CmdLine)
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if *shouldUpdateGolden {
updateGolden(t, test, actual.Bytes())
} else {
assertEqualGolden(t, test, actual.Bytes())
}
}
func updateGolden(t *testing.T, test *CmdTest, actual []byte) {
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
if err := os.MkdirAll(goldenDir, 0775); err != nil {
t.Fatalf("Failed to create golden directory %s: %s", goldenDir, err)
}
t.Logf("Created %s", goldenDir)
goldenFilePath := filepath.Join(goldenDir, test.Name+goldenFileSuffix)
t.Logf("Updating golden file: %s", goldenFilePath)
if err := ioutil.WriteFile(goldenFilePath, normalize(actual), 0666); err != nil {
t.Fatalf("Failed to update golden file: %s", err)
2019-04-29 13:35:13 -05:00
}
}
func assertEqualGolden(t *testing.T, test *CmdTest, actual []byte) {
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
goldenFilePath := filepath.Join(goldenDir, test.Name+goldenFileSuffix)
2019-04-29 13:35:13 -05:00
golden, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatalf("Failed while reading golden file: %s", err)
2019-04-29 13:35:13 -05:00
}
if !bytes.Equal(actual, golden) {
errFmt := "Output does not match golden file: %s\nEXPECTED:\n%s\nGOT:\n%s"
t.Errorf(errFmt, goldenFilePath, string(golden), string(actual))
2019-04-29 13:35:13 -05:00
}
}
func normalize(in []byte) []byte {
return bytes.Replace(in, []byte("\r\n"), []byte("\n"), -1)
}