-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcommits_test.go
More file actions
142 lines (126 loc) · 4.61 KB
/
Copy pathcommits_test.go
File metadata and controls
142 lines (126 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package forges
import (
"context"
"errors"
"strings"
"testing"
)
const (
testCommitSHA = "8e8c483db84b4bee98b60c0593521ed34d9990e8"
testCommitSHAUpper = "8E8C483DB84B4BEE98B60C0593521ED34D9990E8"
)
func TestIsFullCommitSHA(t *testing.T) {
tests := []struct {
name string
ref string
want bool
}{
{name: "lowercase full SHA", ref: testCommitSHA, want: true},
{name: "uppercase full SHA", ref: testCommitSHAUpper, want: true},
{name: "mixed case full SHA", ref: "8e8C483db84B4bee98b60C0593521ed34d9990E8", want: true},
{name: "abbreviated SHA", ref: "8e8c483", want: false},
{name: "branch name", ref: "main", want: false},
{name: "tag name", ref: "v4.2.1", want: false},
{name: "empty", ref: "", want: false},
{name: "too short by one", ref: testCommitSHA[1:], want: false},
{name: "too long by one", ref: testCommitSHA + "0", want: false},
{name: "right length with non-hex character", ref: strings.Repeat("g", FullCommitSHALength), want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsFullCommitSHA(tt.ref); got != tt.want {
t.Errorf("IsFullCommitSHA(%q) = %v, want %v", tt.ref, got, tt.want)
}
})
}
}
func TestValidateCommitRef(t *testing.T) {
tests := []struct {
name string
owner, repo, ref string
wantErr bool
}{
{name: "all set", owner: "actions", repo: "checkout", ref: "v4", wantErr: false},
{name: "empty owner", owner: "", repo: "checkout", ref: "v4", wantErr: true},
{name: "empty repo", owner: "actions", repo: "", ref: "v4", wantErr: true},
{name: "empty ref", owner: "actions", repo: "checkout", ref: "", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateCommitRef(tt.owner, tt.repo, tt.ref)
if tt.wantErr && !errors.Is(err, ErrCommitRefRequired) {
t.Errorf("ValidateCommitRef() error = %v, want ErrCommitRefRequired", err)
}
if !tt.wantErr && err != nil {
t.Errorf("ValidateCommitRef() error = %v, want nil", err)
}
})
}
}
func TestResolvedCommitSHA(t *testing.T) {
t.Run("normalizes case and whitespace", func(t *testing.T) {
got, err := ResolvedCommitSHA("actions", "checkout", "v4", " "+testCommitSHAUpper+"\n")
if err != nil {
t.Fatalf("ResolvedCommitSHA() error = %v", err)
}
if got != testCommitSHA {
t.Errorf("ResolvedCommitSHA() = %q, want %q", got, testCommitSHA)
}
})
t.Run("rejects empty SHA", func(t *testing.T) {
if _, err := ResolvedCommitSHA("actions", "checkout", "v4", " \n"); err == nil {
t.Fatal("ResolvedCommitSHA() error = nil, want empty SHA error")
}
})
t.Run("rejects abbreviated SHA", func(t *testing.T) {
if _, err := ResolvedCommitSHA("actions", "checkout", "v4", "deadbeef"); err == nil {
t.Fatal("ResolvedCommitSHA() error = nil, want invalid SHA error")
}
})
}
func TestCommitRefErrorUnwraps(t *testing.T) {
err := CommitRefError("actions", "checkout", "missing", ErrNotFound)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("CommitRefError() = %v, want an error wrapping ErrNotFound", err)
}
for _, want := range []string{"actions/checkout", `"missing"`} {
if !strings.Contains(err.Error(), want) {
t.Errorf("CommitRefError() = %q, want it to mention %s", err.Error(), want)
}
}
}
func TestClientResolveCommitRoutes(t *testing.T) {
mock := &mockForge{commitService: &mockCommitService{sha: testCommitSHA}}
c := &Client{
forges: map[string]Forge{"example.com": mock},
tokens: make(map[string]string),
}
sha, err := c.ResolveCommit(context.Background(), "https://example.com/test/repo", "v1.0.0")
if err != nil {
t.Fatalf("ResolveCommit() error = %v", err)
}
if sha != testCommitSHA {
t.Errorf("ResolveCommit() = %q, want %q", sha, testCommitSHA)
}
cs := mock.commitService
if cs.lastOwner != "test" || cs.lastRepo != "repo" || cs.lastRef != "v1.0.0" {
t.Errorf("backend received owner=%q repo=%q ref=%q, want test/repo at v1.0.0",
cs.lastOwner, cs.lastRepo, cs.lastRef)
}
}
func TestClientResolveCommitPropagatesBackendError(t *testing.T) {
mock := &mockForge{commitService: &mockCommitService{err: ErrNotFound}}
c := &Client{
forges: map[string]Forge{"example.com": mock},
tokens: make(map[string]string),
}
if _, err := c.ResolveCommit(context.Background(), "https://example.com/test/repo", "nope"); !errors.Is(err, ErrNotFound) {
t.Errorf("ResolveCommit() error = %v, want ErrNotFound", err)
}
}
func TestClientResolveCommitUnregisteredDomain(t *testing.T) {
c := NewClient()
if _, err := c.ResolveCommit(context.Background(), "https://example.com/test/repo", "v1.0.0"); err == nil {
t.Error("ResolveCommit() error = nil, want error for unregistered domain")
}
}