Skip to content

Commit

Permalink
Update test assertions related to spf13/cobra.
Browse files Browse the repository at this point in the history
It now correctly prints errors to stderr (spf13/cobra#894).

Signed-off-by: Matt Moyer <moyerm@vmware.com>
  • Loading branch information
mattmoyer committed Dec 17, 2020
1 parent 6a45746 commit 3e15e18
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
6 changes: 1 addition & 5 deletions cmd/pinniped/cmd/get.go
Expand Up @@ -8,11 +8,7 @@ import (
)

//nolint: gochecknoglobals
var getCmd = &cobra.Command{
Use: "get",
Short: "get",
SilenceUsage: true, // do not print usage message when commands fail
}
var getCmd = &cobra.Command{Use: "get", Short: "get"}

//nolint: gochecknoinits
func init() {
Expand Down
30 changes: 15 additions & 15 deletions cmd/pinniped/cmd/kubeconfig_test.go
Expand Up @@ -81,7 +81,7 @@ func TestGetKubeconfig(t *testing.T) {
args: []string{},
getPathToSelfErr: fmt.Errorf("some OS error"),
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not determine the Pinniped executable path: some OS error
`),
},
Expand All @@ -91,7 +91,7 @@ func TestGetKubeconfig(t *testing.T) {
"--oidc-ca-bundle", "./does/not/exist",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not read --oidc-ca-bundle: open ./does/not/exist: no such file or directory
`),
},
Expand All @@ -101,7 +101,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig", "./does/not/exist",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not load --kubeconfig: stat ./does/not/exist: no such file or directory
`),
},
Expand All @@ -112,7 +112,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig-context", "invalid",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not load --kubeconfig/--kubeconfig-context: no such context "invalid"
`),
},
Expand All @@ -123,7 +123,7 @@ func TestGetKubeconfig(t *testing.T) {
},
getClientsetErr: fmt.Errorf("some kube error"),
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not configure Kubernetes client: some kube error
`),
},
Expand All @@ -135,7 +135,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: webhookauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found
`),
},
Expand All @@ -147,7 +147,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: jwtauthenticators.authentication.concierge.pinniped.dev "test-authenticator" not found
`),
},
Expand All @@ -159,7 +159,7 @@ func TestGetKubeconfig(t *testing.T) {
"--concierge-authenticator-name", "test-authenticator",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: invalid authenticator type "invalid", supported values are "webhook" and "jwt"
`),
},
Expand All @@ -178,7 +178,7 @@ func TestGetKubeconfig(t *testing.T) {
},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: failed to list JWTAuthenticator objects for autodiscovery: some list error
`),
},
Expand All @@ -197,7 +197,7 @@ func TestGetKubeconfig(t *testing.T) {
},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: failed to list WebhookAuthenticator objects for autodiscovery: some list error
`),
},
Expand All @@ -207,7 +207,7 @@ func TestGetKubeconfig(t *testing.T) {
"--kubeconfig", "./testdata/kubeconfig.yaml",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: no authenticators were found in namespace "pinniped-concierge" (try setting --concierge-namespace)
`),
},
Expand All @@ -224,7 +224,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator-4", Namespace: "test-namespace"}},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: multiple authenticators were found in namespace "test-namespace", so the --concierge-authenticator-type/--concierge-authenticator-name flags must be specified
`),
},
Expand All @@ -238,7 +238,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not autodiscover --oidc-issuer, and none was provided
`),
},
Expand All @@ -259,7 +259,7 @@ func TestGetKubeconfig(t *testing.T) {
},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: tried to autodiscover --oidc-ca-bundle, but JWTAuthenticator test-namespace/test-authenticator has invalid spec.tls.certificateAuthorityData: illegal base64 data at input byte 7
`),
},
Expand All @@ -275,7 +275,7 @@ func TestGetKubeconfig(t *testing.T) {
&conciergev1alpha1.WebhookAuthenticator{ObjectMeta: metav1.ObjectMeta{Name: "test-authenticator", Namespace: "test-namespace"}},
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: only one of --static-token and --static-token-env can be specified
`),
},
Expand Down
12 changes: 6 additions & 6 deletions cmd/pinniped/cmd/login_oidc_test.go
Expand Up @@ -79,7 +79,7 @@ func TestLoginOIDCCommand(t *testing.T) {
name: "missing required flags",
args: []string{},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: required flag(s) "issuer" not set
`),
},
Expand All @@ -91,7 +91,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--enable-concierge",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: invalid concierge parameters: endpoint must not be empty
`),
},
Expand All @@ -103,7 +103,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--ca-bundle", "./does/not/exist",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not read --ca-bundle: open ./does/not/exist: no such file or directory
`),
},
Expand All @@ -115,7 +115,7 @@ func TestLoginOIDCCommand(t *testing.T) {
"--ca-bundle-data", "invalid-base64",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not read --ca-bundle-data: illegal base64 data at input byte 7
`),
},
Expand All @@ -128,7 +128,7 @@ func TestLoginOIDCCommand(t *testing.T) {
loginErr: fmt.Errorf("some login error"),
wantOptionsCount: 3,
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not complete Pinniped login: some login error
`),
},
Expand All @@ -145,7 +145,7 @@ func TestLoginOIDCCommand(t *testing.T) {
conciergeErr: fmt.Errorf("some concierge error"),
wantOptionsCount: 3,
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not complete concierge credential exchange: some concierge error
`),
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/pinniped/cmd/login_static_test.go
Expand Up @@ -66,7 +66,7 @@ func TestLoginStaticCommand(t *testing.T) {
name: "missing required flags",
args: []string{},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: one of --token or --token-env must be set
`),
},
Expand All @@ -77,7 +77,7 @@ func TestLoginStaticCommand(t *testing.T) {
"--enable-concierge",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: invalid concierge parameters: endpoint must not be empty
`),
},
Expand All @@ -87,7 +87,7 @@ func TestLoginStaticCommand(t *testing.T) {
"--token-env", "TEST_TOKEN_ENV",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: --token-env variable "TEST_TOKEN_ENV" is not set
`),
},
Expand All @@ -100,7 +100,7 @@ func TestLoginStaticCommand(t *testing.T) {
"TEST_TOKEN_ENV": "",
},
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: --token-env variable "TEST_TOKEN_ENV" is empty
`),
},
Expand All @@ -125,7 +125,7 @@ func TestLoginStaticCommand(t *testing.T) {
},
conciergeErr: fmt.Errorf("some concierge error"),
wantError: true,
wantStdout: here.Doc(`
wantStderr: here.Doc(`
Error: could not complete concierge credential exchange: some concierge error
`),
},
Expand Down
10 changes: 6 additions & 4 deletions cmd/pinniped/cmd/version_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.pinniped.dev/internal/here"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestNewVersionCmd(t *testing.T) {
args []string
wantError bool
wantStdoutRegexp string
wantStderr string
wantStderrRegexp string
}{
{
name: "no flags",
Expand All @@ -57,7 +58,8 @@ func TestNewVersionCmd(t *testing.T) {
name: "arg passed",
args: []string{"tuna"},
wantError: true,
wantStdoutRegexp: `Error: unknown command "tuna" for "version"` + "\n" + knownGoodUsageRegexpForVersion,
wantStderrRegexp: `Error: unknown command "tuna" for "version"`,
wantStdoutRegexp: knownGoodUsageRegexpForVersion,
},
}
for _, tt := range tests {
Expand All @@ -76,8 +78,8 @@ func TestNewVersionCmd(t *testing.T) {
} else {
require.NoError(t, err)
}
require.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout")
require.Equal(t, tt.wantStderr, stderr.String(), "unexpected stderr")
assert.Regexp(t, tt.wantStdoutRegexp, stdout.String(), "unexpected stdout")
assert.Regexp(t, tt.wantStderrRegexp, stderr.String(), "unexpected stderr")
})
}
}

0 comments on commit 3e15e18

Please sign in to comment.