-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
99 lines (96 loc) · 2.53 KB
/
main_test.go
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
package main
import (
"errors"
"strings"
"testing"
modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"github.com/bufbuild/protovalidate-go"
"go.akshayshah.org/attest"
"google.golang.org/protobuf/testing/protocmp"
)
func Test_parseReference(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
reference string
wantRemote string
wantResourceRef *modulev1.ResourceRef_Name
wantError bool
wantValidationError bool
}{
{
reference: "bufbuild/registry",
wantRemote: "",
wantResourceRef: &modulev1.ResourceRef_Name{
Owner: "bufbuild",
Module: "registry",
},
wantError: false,
},
{
reference: "buf.build/bufbuild/registry",
wantRemote: "buf.build",
wantResourceRef: &modulev1.ResourceRef_Name{
Owner: "bufbuild",
Module: "registry",
},
wantError: false,
},
{
reference: "buf.build/bufbuild/registry:569d290ee4cc4ed38499daf2c4fe39e6",
wantRemote: "buf.build",
wantResourceRef: &modulev1.ResourceRef_Name{
Owner: "bufbuild",
Module: "registry",
Child: &modulev1.ResourceRef_Name_Ref{
Ref: "569d290ee4cc4ed38499daf2c4fe39e6",
},
},
wantError: false,
},
{
// We don't support URL schemes.
reference: "https://buf.build/bufbuild/registry:569d290ee4cc4ed38499daf2c4fe39e6",
wantError: true,
},
{
reference: "bufbuild/bufbuild/registry:569d290ee4cc4ed38499daf2c4fe39e6",
// TODO: We don't currently validate the hostname.
wantRemote: "bufbuild",
wantResourceRef: &modulev1.ResourceRef_Name{
Owner: "bufbuild",
Module: "registry",
Child: &modulev1.ResourceRef_Name_Ref{
Ref: "569d290ee4cc4ed38499daf2c4fe39e6",
},
},
wantError: false,
},
{
reference: strings.Repeat("a", 33) + "/abc",
// Owner name too long, based on protovalidate rules.
wantError: true,
wantValidationError: true,
},
{
reference: "abc/a",
// Repository name too short, based on protovalidate rules.
wantError: true,
wantValidationError: true,
},
} {
t.Run("reference: "+tc.reference, func(t *testing.T) {
t.Parallel()
gotRemote, gotResourceRef, gotErr := parseReference(tc.reference)
if tc.wantError {
attest.Error(t, gotErr)
if tc.wantValidationError {
err := &protovalidate.ValidationError{}
attest.True(t, errors.As(gotErr, &err))
}
} else {
attest.Equal(t, gotRemote, tc.wantRemote)
attest.Equal(t, gotResourceRef, tc.wantResourceRef, attest.Cmp(protocmp.Transform()))
}
})
}
}