Skip to content

Commit

Permalink
separate tests out to cmd_test package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Aug 8, 2024
1 parent 63a1c69 commit 445bfbd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/cmd/team_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmd
package cmd_test

import (
"testing"

"github.com/opslevel/cli/cmd"
"github.com/opslevel/opslevel-go/v2024"
)

Expand All @@ -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...)
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/user_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd
package cmd_test

import (
"strings"
"testing"

"github.com/opslevel/cli/cmd"
"github.com/opslevel/opslevel-go/v2024"
)

Expand All @@ -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...)
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/util_helper_test.go
Original file line number Diff line number Diff line change
@@ -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
)
11 changes: 6 additions & 5 deletions src/cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd
package cmd_test

import (
"encoding/json"
"io"
"os"
"strings"

"github.com/opslevel/cli/cmd"
"gopkg.in/yaml.v2"
)

Expand All @@ -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
Expand Down

0 comments on commit 445bfbd

Please sign in to comment.