Skip to content

Commit

Permalink
chore: fixing UT and adding HasCapability function (#7)
Browse files Browse the repository at this point in the history
chore: fixing UT and adding HasCapability function

Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
priteshbandi authored Jan 16, 2024
1 parent c077eda commit e3d2a30
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"os"
"sort"
"strings"

"github.com/notaryproject/notation-plugin-framework-go/internal/slices"
Expand Down Expand Up @@ -31,6 +32,7 @@ func getValidArgs(md *plugin.GetMetadataResponse) []string {
args = append(args, string(plugin.CommandVerifySignature))
}

sort.Strings(args)
return args
}

Expand Down
Binary file removed example/example
Binary file not shown.
15 changes: 15 additions & 0 deletions plugin/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ type GetMetadataResponse struct {
SupportedContractVersions []string `json:"supportedContractVersions,omitempty"`
Capabilities []Capability `json:"capabilities"`
}

// HasCapability return true if the metadata states that the
// capability is supported.
// Returns true if capability is empty.
func (resp *GetMetadataResponse) HasCapability(capability Capability) bool {
if capability == "" {
return true
}
for _, c := range resp.Capabilities {
if c == capability {
return true
}
}
return false
}
29 changes: 29 additions & 0 deletions plugin/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package plugin

import (
"testing"
)

func TestGetMetadataResponse_HasCapability(t *testing.T) {
type args struct {
capability Capability
}
tests := []struct {
name string
m *GetMetadataResponse
args args
want bool
}{
{"empty capabilities", &GetMetadataResponse{}, args{"cap"}, false},
{"other capabilities", &GetMetadataResponse{Capabilities: []Capability{"foo", "baz"}}, args{"cap"}, false},
{"empty target capability", &GetMetadataResponse{Capabilities: []Capability{"foo", "baz"}}, args{""}, true},
{"found", &GetMetadataResponse{Capabilities: []Capability{"foo", "baz"}}, args{"baz"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.HasCapability(tt.args.capability); got != tt.want {
t.Errorf("GetMetadataResponse.HasCapability() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e3d2a30

Please sign in to comment.