Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: boolean logic #98

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func getResourceScope(scope string) (cluster, namespace bool, err error) {
switch scope {
case "":
cluster = viper.GetString(constants.FlagNamespace) == ""
namespace = false
namespace = true
case "namespace":
cluster = false
namespace = true
Expand Down
56 changes: 56 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client

import "testing"

func TestGetResourceScope(t *testing.T) {
tests := map[string]struct {
scope string
wantCluster bool
wantNamespace bool
wantErr bool
Comment on lines +7 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for change, just a thought: If you introduce a struct like this

type result struct {
  cluster, namespace, err bool
}

you can do all three comparisons at once like so:

if diff := cmp.Diff(test.want, got); diff != "" {
  t.Errorf("getResourceScope(..) returned unexpected result (-want,+got)\n%s", diff)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! I'll keep that in mind for next time

}{
"no scope": {
scope: "",
wantCluster: true,
wantNamespace: true,
wantErr: false,
},
"namespace scope": {
scope: "namespace",
wantCluster: false,
wantNamespace: true,
wantErr: false,
},
"cluster scope": {
scope: "cluster",
wantCluster: true,
wantNamespace: false,
wantErr: false,
},
"unknown scope": {
scope: "unknown",
wantCluster: false,
wantNamespace: false,
wantErr: true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
gotCluster, gotNamespace, gotErr := getResourceScope(test.scope)

if gotCluster != test.wantCluster {
t.Fatalf("wrong cluster: got %t, want %t", gotCluster, test.wantNamespace)
}
if gotNamespace != test.wantNamespace {
t.Fatalf("wrong namespace: got %t, want %t", gotNamespace, test.wantNamespace)
}
if gotErr != nil && !test.wantErr {
t.Fatalf("unexpected error: %s", gotErr.Error())
}
if gotErr == nil && test.wantErr {
t.Fatal("expected error, got none")
}
})
}
}