From 445bfbd6316a4d29d9e65506c27aa8d87b298840 Mon Sep 17 00:00:00 2001 From: David Bloss Date: Thu, 8 Aug 2024 13:06:48 -0500 Subject: [PATCH] separate tests out to cmd_test package --- src/cmd/team_test.go | 5 +++-- src/cmd/user_test.go | 5 +++-- src/cmd/util_helper_test.go | 9 +++++++++ src/cmd/util_test.go | 11 ++++++----- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 src/cmd/util_helper_test.go diff --git a/src/cmd/team_test.go b/src/cmd/team_test.go index fe7a66de..26610a67 100644 --- a/src/cmd/team_test.go +++ b/src/cmd/team_test.go @@ -1,8 +1,9 @@ -package cmd +package cmd_test import ( "testing" + "github.com/opslevel/cli/cmd" "github.com/opslevel/opslevel-go/v2024" ) @@ -21,7 +22,7 @@ func Test_TeamCRUD(t *testing.T) { } cliArgs := []string{teamToCreate.Name, "-f", teamFileName} - rootCmd.SetArgs(cliArgs) + cmd.RootCmd.SetArgs(cliArgs) // Create Team createOutput, err := execCmd(Create, "team", cliArgs...) diff --git a/src/cmd/user_test.go b/src/cmd/user_test.go index 1b9ea8b5..df16e57a 100644 --- a/src/cmd/user_test.go +++ b/src/cmd/user_test.go @@ -1,9 +1,10 @@ -package cmd +package cmd_test import ( "strings" "testing" + "github.com/opslevel/cli/cmd" "github.com/opslevel/opslevel-go/v2024" ) @@ -19,7 +20,7 @@ func Test_UserCRUD(t *testing.T) { Name: userName, } cliArgs := []string{expectedUser.Email, expectedUser.Name} - rootCmd.SetArgs(cliArgs) + cmd.RootCmd.SetArgs(cliArgs) // Create User createOutput, err := execCmd(Create, "user", cliArgs...) diff --git a/src/cmd/util_helper_test.go b/src/cmd/util_helper_test.go new file mode 100644 index 00000000..48c1d8b2 --- /dev/null +++ b/src/cmd/util_helper_test.go @@ -0,0 +1,9 @@ +package cmd + +// Workaround for testing unexported functions. +// +// Running `go help build` displays: +// When compiling packages, build ignores files that end in '_test.go'. +var ( + RootCmd = rootCmd +) diff --git a/src/cmd/util_test.go b/src/cmd/util_test.go index 7e30d585..e406c176 100644 --- a/src/cmd/util_test.go +++ b/src/cmd/util_test.go @@ -1,4 +1,4 @@ -package cmd +package cmd_test import ( "encoding/json" @@ -6,6 +6,7 @@ import ( "os" "strings" + "github.com/opslevel/cli/cmd" "gopkg.in/yaml.v2" ) @@ -23,15 +24,15 @@ const ( ) // execute any OpsLevel CLI command -func execCmd(cmd Operation, resource string, inputs ...string) ([]byte, error) { - cliArgs := []string{string(cmd), resource} +func execCmd(command Operation, resource string, inputs ...string) ([]byte, error) { + cliArgs := []string{string(command), resource} cliArgs = append(cliArgs, inputs...) r, oldStdout := redirectStdout() defer r.Close() - rootCmd.SetArgs(cliArgs) - err := rootCmd.Execute() + cmd.RootCmd.SetArgs(cliArgs) + err := cmd.RootCmd.Execute() output := captureOutput(r, oldStdout) return output, err