Repository navigation
Expand file tree
/
Copy pathitems_test.go
More file actions
81 lines (72 loc) · 2.31 KB
/
Copy pathitems_test.go
File metadata and controls
81 lines (72 loc) · 2.31 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
package main
import (
"strings"
"testing"
"go.vanburen.xyz/ok"
"time"
modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestShortenSourceControlURL(t *testing.T) {
t.Parallel()
tests := []struct {
name string
url string
want string
}{
{
name: "github commit URL is shortened to owner/repo@shortSHA",
url: "https://github.com/bufbuild/registry/commit/abc123def4567890fabc",
want: "github.com/bufbuild/registry@abc123def456",
},
{
name: "gitlab-style commits URL is shortened",
url: "https://gitlab.com/owner/repo/commits/deadbeefcafe1234",
want: "gitlab.com/owner/repo@deadbeefcafe",
},
{
name: "short ref is kept as-is",
url: "https://github.com/bufbuild/registry/commit/abc123",
want: "github.com/bufbuild/registry@abc123",
},
{
name: "non-hex ref longer than 12 chars is kept as-is",
url: "https://example.com/bufbuild/registry/commit/not-a-hex-sha-ref",
want: "example.com/bufbuild/registry@not-a-hex-sha-ref",
},
{
name: "unrecognized shape falls back to host",
url: "https://example.com/some/other/path",
want: "example.com",
},
{
name: "invalid URL is returned unchanged",
url: "not a url",
want: "not a url",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ok.Equal(t, shortenSourceControlURL(tt.url), tt.want, ok.Sprintf("shortenSourceControlURL(%q)", tt.url))
})
}
}
func TestCommitDescription_SourceControlURL(t *testing.T) {
t.Parallel()
base := &commit{underlying: &modulev1.Commit{
Id: "abc123",
CreateTime: timestamppb.New(time.Now()),
}}
descWithoutURL := base.Description()
ok.True(t, !strings.Contains(descWithoutURL, "@"),
ok.Sprintf("description %q should not contain a source control marker when the URL is unset", descWithoutURL))
withURL := &commit{underlying: &modulev1.Commit{
Id: "abc123",
CreateTime: timestamppb.New(time.Now()),
SourceControlUrl: "https://github.com/bufbuild/registry/commit/abc123def4567890",
}}
descWithURL := withURL.Description()
ok.True(t, strings.Contains(descWithURL, "github.com/bufbuild/registry@abc123def456"),
ok.Sprintf("description %q should contain the shortened source control URL", descWithURL))
}