Fix old unit tests

These are going to need to be revisited.
This commit is contained in:
Ian Howell 2019-05-24 08:47:49 -05:00
parent d2aabc5678
commit 6aa9b3809b
5 changed files with 59 additions and 16 deletions

View File

@ -1,15 +1,33 @@
package cmd_test package cmd_test
import ( import (
"bytes"
"os"
"testing" "testing"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/pkg/environment"
"github.com/ian-howell/airshipctl/internal/test" "github.com/ian-howell/airshipctl/internal/test"
) )
func TestRoot(t *testing.T) { func TestRoot(t *testing.T) {
tests := []test.CmdTest{{ tests := []test.CmdTest{
Name: "default", {
Command: "", Name: "default",
}} CmdLine: "",
test.RunCmdTests(t, tests) },
}
for _, tt := range tests {
actual := &bytes.Buffer{}
rootCmd, err := cmd.NewRootCmd(actual)
if err != nil {
t.Fatalf("Could not create root command: %s", err.Error())
}
settings := &environment.AirshipCTLSettings{}
settings.InitFlags(rootCmd)
rootCmd.PersistentFlags().Parse(os.Args[1:])
settings.Init()
test.RunTest(t, tt, rootCmd, actual)
}
} }

View File

@ -6,12 +6,9 @@ Usage:
Available Commands: Available Commands:
help Help about any command help Help about any command
version Show the version number of airshipctl version Show the version number of airshipctl
workflow Access to argo workflows
Flags: Flags:
--debug enable verbose output --debug enable verbose output
-h, --help help for airshipctl -h, --help help for airshipctl
--kubeconfig string path to kubeconfig
--namespace string kubernetes namespace to use for the context of this command (default "default")
Use "airshipctl [command] --help" for more information about a command. Use "airshipctl [command] --help" for more information about a command.

View File

@ -1 +1 @@
airshipctl: v0.1.0 airshipctl: v0.1.0

View File

@ -1,15 +1,27 @@
package cmd_test package cmd_test
import ( import (
"bytes"
"testing" "testing"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/internal/test" "github.com/ian-howell/airshipctl/internal/test"
) )
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
tests := []test.CmdTest{{ tests := []test.CmdTest{
Name: "version", {
Command: "version", Name: "version",
}} CmdLine: "version",
test.RunCmdTests(t, tests) },
}
for _, tt := range tests {
actual := &bytes.Buffer{}
rootCmd, err := cmd.NewRootCmd(actual)
if err != nil {
t.Fatalf("Could not create root command: %s", err.Error())
}
rootCmd.AddCommand(cmd.NewVersionCommand(actual))
test.RunTest(t, tt, rootCmd, actual)
}
} }

View File

@ -6,7 +6,10 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/spf13/cobra"
) )
// UpdateGolden writes out the golden files with the latest values, rather than failing the test. // UpdateGolden writes out the golden files with the latest values, rather than failing the test.
@ -24,6 +27,19 @@ type CmdTest struct {
CmdLine string CmdLine string
} }
func RunTest(t *testing.T, test CmdTest, cmd *cobra.Command, actual *bytes.Buffer) {
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) { func updateGolden(t *testing.T, test CmdTest, actual []byte) {
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix) goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
if err := os.MkdirAll(goldenDir, 0775); err != nil { if err := os.MkdirAll(goldenDir, 0775); err != nil {