From 6aa9b3809bc002a5f87ad8d50548b42aaa7dd312 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Fri, 24 May 2019 08:47:49 -0500 Subject: [PATCH] Fix old unit tests These are going to need to be revisited. --- cmd/root_test.go | 28 +++++++++++++++---- .../TestRootGoldenOutput/default.golden | 7 ++--- .../TestVersionGoldenOutput/version.golden | 2 +- cmd/version_test.go | 22 +++++++++++---- internal/test/utilities.go | 16 +++++++++++ 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 1ba92ecf2..dd1051baf 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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) + } } diff --git a/cmd/testdata/TestRootGoldenOutput/default.golden b/cmd/testdata/TestRootGoldenOutput/default.golden index cf8cc6462..80c18f7f3 100644 --- a/cmd/testdata/TestRootGoldenOutput/default.golden +++ b/cmd/testdata/TestRootGoldenOutput/default.golden @@ -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. diff --git a/cmd/testdata/TestVersionGoldenOutput/version.golden b/cmd/testdata/TestVersionGoldenOutput/version.golden index 6ea004fc4..5316bfd0e 100644 --- a/cmd/testdata/TestVersionGoldenOutput/version.golden +++ b/cmd/testdata/TestVersionGoldenOutput/version.golden @@ -1 +1 @@ -airshipctl: v0.1.0 +airshipctl: v0.1.0 diff --git a/cmd/version_test.go b/cmd/version_test.go index 13624201a..d53c13916 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -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) + } } diff --git a/internal/test/utilities.go b/internal/test/utilities.go index 43a5feb1f..4b983e957 100644 --- a/internal/test/utilities.go +++ b/internal/test/utilities.go @@ -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 {