Add initial unit tests
This commit is contained in:
parent
001eed5836
commit
46f3162ac1
5
cmd/testdata/TestVersion.golden
vendored
Normal file
5
cmd/testdata/TestVersion.golden
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
airshipadm: v0.1.0
|
||||
golang : go1.12
|
||||
kubernetes: v1.12.6
|
||||
helm : TODO
|
||||
argo : TODO
|
5
cmd/testdata/golden/TestVersion.golden
vendored
Normal file
5
cmd/testdata/golden/TestVersion.golden
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
airshipadm: v0.1.0
|
||||
golang : go1.12
|
||||
kubernetes: v1.12.6
|
||||
helm : TODO
|
||||
argo : TODO
|
15
cmd/version_test.go
Normal file
15
cmd/version_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package cmd_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ian-howell/airshipadm/internal/test"
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
tests := []test.CmdTest{{
|
||||
Name: "version",
|
||||
Command: "version",
|
||||
}}
|
||||
test.RunCmdTests(t, tests)
|
||||
}
|
62
internal/test/utilities.go
Normal file
62
internal/test/utilities.go
Normal file
@ -0,0 +1,62 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ian-howell/airshipadm/cmd"
|
||||
)
|
||||
|
||||
// UpdateGolden writes out the golden files with the latest values, rather than failing the test.
|
||||
var updateGolden = flag.Bool("update", false, "update golden files")
|
||||
|
||||
const goldenFileDir = "testdata/golden"
|
||||
|
||||
type CmdTest struct {
|
||||
Name string
|
||||
Command string
|
||||
}
|
||||
|
||||
func RunCmdTests(t *testing.T, tests []CmdTest) {
|
||||
t.Helper()
|
||||
|
||||
for _, test := range tests {
|
||||
executeCmd(t, test.Command)
|
||||
}
|
||||
}
|
||||
|
||||
func executeCmd(t *testing.T, command string) {
|
||||
var actual bytes.Buffer
|
||||
rootCmd := cmd.NewRootCmd(&actual)
|
||||
|
||||
// TODO(howell): switch to shellwords (or similar)
|
||||
args := strings.Fields(command)
|
||||
rootCmd.SetArgs(args)
|
||||
|
||||
rootCmd.Execute()
|
||||
|
||||
goldenFilePath := filepath.Join(goldenFileDir, filepath.FromSlash(t.Name())+".golden")
|
||||
if *updateGolden {
|
||||
t.Logf("updating golden file: %s", goldenFilePath)
|
||||
if err := ioutil.WriteFile(goldenFilePath, actual.Bytes(), 0644); err != nil {
|
||||
t.Fatalf("failed to update golden file: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
golden, err := ioutil.ReadFile(goldenFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed while reading golden file: %s", err)
|
||||
}
|
||||
if !bytes.Equal(actual.Bytes(), golden) {
|
||||
errFmt := "output does not match golden file: %s\nEXPECTED:\n%s\nGOT:\n%s"
|
||||
t.Errorf(errFmt, goldenFilePath, string(golden), actual.String)
|
||||
}
|
||||
}
|
||||
|
||||
func normalize(in []byte) []byte {
|
||||
return bytes.Replace(in, []byte("\r\n"), []byte("\n"), -1)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user