-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathversion_test.go
More file actions
124 lines (113 loc) · 3.99 KB
/
Copy pathversion_test.go
File metadata and controls
124 lines (113 loc) · 3.99 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
package ipfs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSuffixFromForkPath(t *testing.T) {
tests := []struct {
name string
path string
expected string
}{
{name: "empty", path: "", expected: ""},
{name: "upstream", path: "github.com/ipfs/kubo", expected: ""},
{name: "github fork", path: "github.com/myorg/kubo", expected: "myorg"},
{name: "gitlab fork", path: "gitlab.com/myorg/kubo", expected: "myorg"},
{name: "codeberg fork", path: "codeberg.org/myorg/kubo", expected: "myorg"},
{name: "bitbucket fork", path: "bitbucket.org/myorg/kubo", expected: "myorg"},
{name: "github renamed repo", path: "github.com/myorg/kubo-experimental", expected: "myorg/kubo-experimental"},
{name: "unknown host canonical repo", path: "git.example.com/team/kubo", expected: "git.example.com/team"},
{name: "unknown host renamed repo", path: "git.example.com/team/kubo-fork", expected: "git.example.com/team/kubo-fork"},
{name: "unknown host nested path", path: "git.example.com/group/sub/kubo", expected: "git.example.com/group/sub"},
{name: "trailing slash", path: "github.com/myorg/kubo/", expected: "myorg"},
{name: "leading slash", path: "/github.com/myorg/kubo", expected: "myorg"},
{name: "single segment", path: "kubo", expected: "kubo"},
{name: "two segment fork on known host", path: "github.com/kubo", expected: "github.com/kubo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, suffixFromForkPath(tt.path))
})
}
}
func TestImplicitAgentSuffix_PrefersBuildOrigin(t *testing.T) {
orig := buildOrigin
t.Cleanup(func() { buildOrigin = orig })
buildOrigin = "github.com/myorg/kubo"
assert.Equal(t, "myorg", ImplicitAgentSuffix())
// Falls through to BuildInfo when origin matches upstream or is empty;
// BuildInfo.Main.Path is "github.com/ipfs/kubo" during `go test` of this
// package, so the implicit suffix is empty.
buildOrigin = ""
assert.Equal(t, "", ImplicitAgentSuffix())
buildOrigin = upstreamModulePath
assert.Equal(t, "", ImplicitAgentSuffix())
}
// TestGetUserAgentVersion verifies the user agent string used in libp2p
// identify and HTTP requests. Tagged release builds (where the commit matches
// the tag) skip the commit hash from the agent version, since the version
// number already identifies the exact source.
func TestGetUserAgentVersion(t *testing.T) {
origCommit := CurrentCommit
origTagged := taggedRelease
origSuffix := userAgentSuffix
t.Cleanup(func() {
CurrentCommit = origCommit
taggedRelease = origTagged
userAgentSuffix = origSuffix
})
tests := []struct {
name string
commit string
tagged string
suffix string
expected string
}{
// dev builds without ldflags
{
name: "no commit, no suffix",
expected: "kubo/" + CurrentVersionNumber,
},
// dev builds with commit set via ldflags
{
name: "with commit",
commit: "abc1234",
expected: "kubo/" + CurrentVersionNumber + "/abc1234",
},
{
name: "with suffix, no commit",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
{
name: "with commit and suffix",
commit: "abc1234",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/abc1234/test-suffix",
},
// tagged release builds: commit is redundant because the version
// number already maps to an exact git tag, so it is omitted to
// save bytes in identify and HTTP user-agent headers.
{
name: "tagged release ignores commit",
commit: "abc1234",
tagged: "1",
expected: "kubo/" + CurrentVersionNumber,
},
{
name: "tagged release with suffix ignores commit",
commit: "abc1234",
tagged: "1",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CurrentCommit = tt.commit
taggedRelease = tt.tagged
SetUserAgentSuffix(tt.suffix)
assert.Equal(t, tt.expected, GetUserAgentVersion())
})
}
}