-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_test.go
More file actions
119 lines (110 loc) · 2.65 KB
/
version_test.go
File metadata and controls
119 lines (110 loc) · 2.65 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
// Copyright 2025 Matthew Gall <me@matthewgall.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"testing"
"golang.org/x/mod/semver"
)
func TestVersionComparison(t *testing.T) {
tests := []struct {
name string
v1 string
v2 string
expected int // -1: v1 < v2, 0: v1 == v2, 1: v1 > v2
}{
{
name: "same version",
v1: "v1.5.0",
v2: "v1.5.0",
expected: 0,
},
{
name: "major version difference",
v1: "v1.5.0",
v2: "v2.0.0",
expected: -1,
},
{
name: "minor version difference",
v1: "v1.5.0",
v2: "v1.6.0",
expected: -1,
},
{
name: "patch version difference",
v1: "v1.5.1",
v2: "v1.5.2",
expected: -1,
},
{
name: "double digit minor version",
v1: "v1.9.0",
v2: "v1.10.0",
expected: -1,
},
{
name: "newer major version",
v1: "v2.0.0",
v2: "v1.9.0",
expected: 1,
},
{
name: "newer minor version",
v1: "v1.10.0",
v2: "v1.9.0",
expected: 1,
},
{
name: "prerelease version",
v1: "v1.5.0-beta",
v2: "v1.5.0",
expected: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := semver.Compare(tt.v1, tt.v2)
if result != tt.expected {
t.Errorf("semver.Compare(%s, %s) = %d, want %d", tt.v1, tt.v2, result, tt.expected)
}
})
}
}
func TestGetVersion(t *testing.T) {
v := GetVersion()
if v == "" {
t.Error("GetVersion() should not return empty string")
}
}
func TestGetUserAgent(t *testing.T) {
ua := GetUserAgent()
if ua == "" {
t.Error("GetUserAgent() should not return empty string")
}
if !contains(ua, "matthewgall/octojoin") {
t.Errorf("GetUserAgent() = %s, should contain 'matthewgall/octojoin'", ua)
}
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > 0 && len(substr) > 0 && findSubstring(s, substr)))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}