Skip to content

Commit

Permalink
Add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed May 18, 2022
1 parent d196160 commit 74dd3ae
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
go-version: '1.18'

- run: |-
go test -count=1 -race ./...
go test -count=1 -shuffle=on -timeout=10m -race ./...
8 changes: 4 additions & 4 deletions resolver/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewActions(ctx context.Context) (*Actions, error) {
}

func (g *Actions) Resolve(ctx context.Context, value string) (string, error) {
githubRef, err := ParseRef(value)
githubRef, err := ParseActionRef(value)
if err != nil {
return "", fmt.Errorf("failed to parse github ref: %w", err)
}
Expand All @@ -68,16 +68,16 @@ func (g *Actions) Resolve(ctx context.Context, value string) (string, error) {
return fmt.Sprintf("%s/%s@%s", owner, repo, sha), nil
}

func ParseRef(s string) (*GitHubRef, error) {
func ParseActionRef(s string) (*GitHubRef, error) {
parts := strings.SplitN(s, "/", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("bad cutset: %q", s)
return nil, fmt.Errorf("missing owner/repo in actions reference: %q", s)
}
owner, rest := parts[0], parts[1]

smallerParts := strings.SplitN(rest, "@", 2)
if len(smallerParts) < 2 {
return nil, fmt.Errorf("bad cutset2: %q", s)
return nil, fmt.Errorf("missing @ in actions reference: %q", s)
}
ref := smallerParts[1]

Expand Down
79 changes: 79 additions & 0 deletions resolver/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package resolver

import (
"reflect"
"strings"
"testing"
)

func TestParseRef(t *testing.T) {
t.Parallel()

cases := []struct {
name string
in string
exp *GitHubRef
err string
}{
{
name: "empty",
in: "",
err: "missing owner/repo",
},
{
name: "no_slash",
in: "foo_bar_baz@v0",
err: "missing owner/repo",
},
{
name: "no_ref",
in: "foo/bar",
err: "missing @",
},
{
name: "ref",
in: "foo/bar@v0",
exp: &GitHubRef{
owner: "foo",
repo: "bar",
path: "",
ref: "v0",
},
},
{
name: "ref_path",
in: "foo/bar/baz@v0",
exp: &GitHubRef{
owner: "foo",
repo: "bar",
path: "baz",
ref: "v0",
},
},
}

for _, tc := range cases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ref, err := ParseActionRef(tc.in)
if err != nil {
if tc.err == "" {
t.Fatal(err)
} else {
if str := err.Error(); !strings.Contains(str, tc.err) {
t.Errorf("expected %q to contain %q", str, tc.err)
}
}
} else if tc.err != "" {
t.Fatalf("expected error, but got %#v", ref)
}

if got, want := ref, tc.exp; !reflect.DeepEqual(got, want) {
t.Errorf("expected %#v to be %#v", got, want)
}
})
}
}

0 comments on commit 74dd3ae

Please sign in to comment.