Skip to content

Commit

Permalink
wip increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Aug 13, 2024
1 parent 3d4d32b commit 1b5f148
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/cmd/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"reflect"
"testing"

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

var (
Expand Down Expand Up @@ -76,13 +78,13 @@ func TestSerialize(t *testing.T) {
err := json.Unmarshal([]byte(tt.input), &got)
if err != nil {
if tt.wantErr == nil {
t.Fatalf("unexpected error: %v", err)
assert.Fail(t, "unexpected error", err)
} else if err.Error() != tt.wantErr.Error() {
t.Errorf("expected error %v, got %v", tt.wantErr, err)
assert.Equal(t, tt.wantErr, err)
}
return
} else if tt.wantErr != nil {
t.Errorf("expected error %v, got nil", tt.wantErr)
assert.Fail(t, "expected error, got nil", tt.wantErr)
return
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/cmd/view-secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -47,6 +49,52 @@ func TestParseArgs(t *testing.T) {
}
}

func TestNewCmdViewSecret(t *testing.T) {
tests := map[string]struct {
args []string
wantErr error
}{
"all": {args: []string{"test", "--all"}},
"custom ctx": {args: []string{"test", "--context", "gotest"}},
"custom kubecfg": {args: []string{"test", "--kubeconfig", "cfg"}},
"custom ns": {args: []string{"test", "--namespace", "bob"}},
"impersonate group": {args: []string{"test", "--as-group", "golovers"}},
"impersonate user": {args: []string{"test", "--as", "gopher"}},
"quiet": {args: []string{"test", "--all", "--quiet"}},
"unknown flag": {args: []string{"--version"}, wantErr: errors.New("unknown flag: --version")},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

cmd := NewCmdViewSecret()
outBuf := bytes.NewBufferString("")

cmd.SetOut(outBuf)
cmd.SetArgs(tt.args)

err := cmd.Execute()
if err != nil {
if tt.wantErr == nil {
assert.Fail(t, "unexpected error", err)
} else if err.Error() != tt.wantErr.Error() {
assert.Equal(t, tt.wantErr, err)
}
return
} else if tt.wantErr != nil {
assert.Fail(t, "expected error, got nil", tt.wantErr)
return
}

_, err = io.ReadAll(outBuf)
if err != nil {
t.Fatal(err)
}
})
}
}

func TestProcessSecret(t *testing.T) {
tests := map[string]struct {
secretData SecretData
Expand Down

0 comments on commit 1b5f148

Please sign in to comment.