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
import (
"bytes"
"os"
"testing"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/pkg/environment"
"github.com/ian-howell/airshipctl/internal/test"
)
func TestRoot(t *testing.T) {
tests := []test.CmdTest{{
Name: "default",
Command: "",
}}
test.RunCmdTests(t, tests)
tests := []test.CmdTest{
{
Name: "default",
CmdLine: "",
},
}
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:
help Help about any command
version Show the version number of airshipctl
workflow Access to argo workflows
Flags:
--debug enable verbose output
-h, --help help for airshipctl
--kubeconfig string path to kubeconfig
--namespace string kubernetes namespace to use for the context of this command (default "default")
--debug enable verbose output
-h, --help help for airshipctl
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
import (
"bytes"
"testing"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/internal/test"
)
func TestVersion(t *testing.T) {
tests := []test.CmdTest{{
Name: "version",
Command: "version",
}}
test.RunCmdTests(t, tests)
tests := []test.CmdTest{
{
Name: "version",
CmdLine: "version",
},
}
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"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/cobra"
)
// UpdateGolden writes out the golden files with the latest values, rather than failing the test.
@ -24,6 +27,19 @@ type CmdTest struct {
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) {
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
if err := os.MkdirAll(goldenDir, 0775); err != nil {