Skip to content

Commit b84edb7

Browse files
committed
Fix client library version resolution
1 parent b72f0ae commit b84edb7

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

external/external.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"math"
87
"mime"
98
"net/http"
109
"os"
@@ -136,6 +135,10 @@ func ClientVersionFromCGVersion(owner, repo, cgVersion string) string {
136135
return "latest"
137136
}
138137

138+
return compatibleLibraryVersion(versions, cgVersion)
139+
}
140+
141+
func compatibleLibraryVersion(versions map[string]string, cgVersion string) string {
139142
// check exact match
140143
if v, ok := versions[cgVersion]; ok {
141144
return v
@@ -181,32 +184,33 @@ func ClientVersionFromCGVersion(owner, repo, cgVersion string) string {
181184
// check closest minor version above requested
182185
closestMinor := -1
183186
for _, v := range compatibleMinorVersions {
184-
if v > minor && float64(closestMinor-minor) > float64(v-minor) {
187+
if v > minor && (closestMinor == -1 || closestMinor-minor > v-minor) {
185188
closestMinor = v
186189
}
187190
}
188191
if closestMinor >= 0 {
189-
v := fmt.Sprintf("%s.%d", major, minor)
192+
v := versions[fmt.Sprintf("%s.%d", major, closestMinor)]
190193
cli.Warn("No exact version match found. Using client library version %s.", v)
191194
return v
192195
}
193196

194197
// check closest minor version below requested
195-
closestMinor = math.MaxInt
198+
closestMinor = -1
196199
for _, v := range compatibleMinorVersions {
197-
if v < minor && float64(minor-closestMinor) > float64(minor-v) {
200+
if v < minor && (closestMinor == -1 || minor-closestMinor > minor-v) {
198201
closestMinor = v
199202
}
200203
}
201204
if closestMinor >= 0 {
202-
v := fmt.Sprintf("%s.%d", major, minor)
203-
cli.Warn("No exact version match found. Using client library version %s.")
205+
v := versions[fmt.Sprintf("%s.%d", major, closestMinor)]
206+
cli.Warn("No exact version match found. Using client library version %s.", v)
204207
return v
205208
}
206209

207210
cli.Warn("No compatible client library version found. Using latest client library version.")
208211
return "latest"
209212
}
213+
210214
func HasContentType(h http.Header, mimetype string) bool {
211215
contentType := h.Get("Content-type")
212216
if contentType == "" {

external/external_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package external
2+
3+
import "testing"
4+
5+
func Test_compatibleLibraryVersion(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
versions map[string]string
9+
cgVersion string
10+
want string
11+
}{
12+
{name: "exact match", versions: map[string]string{
13+
"0.1": "1.2",
14+
"0.3": "1.4",
15+
"0.5": "1.6",
16+
"0.7": "1.8",
17+
"1.1": "2.2",
18+
"1.5": "2.6",
19+
"1.7": "2.8",
20+
}, cgVersion: "0.5", want: "1.6"},
21+
{name: "next highest minor", versions: map[string]string{
22+
"0.1": "1.2",
23+
"0.3": "1.4",
24+
"0.6": "1.6",
25+
"0.7": "1.8",
26+
"1.1": "2.2",
27+
"1.5": "2.6",
28+
"1.7": "2.8",
29+
}, cgVersion: "0.5", want: "1.6"},
30+
{name: "next lowest minor", versions: map[string]string{
31+
"0.1": "1.2",
32+
"0.3": "1.4",
33+
"1.1": "2.2",
34+
"1.5": "2.6",
35+
"1.7": "2.8",
36+
}, cgVersion: "0.5", want: "1.4"},
37+
{name: "none", versions: map[string]string{
38+
"0.1": "1.2",
39+
"0.3": "1.4",
40+
"1.1": "2.2",
41+
"1.5": "2.6",
42+
"1.7": "2.8",
43+
}, cgVersion: "2.5", want: "latest"},
44+
}
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
if got := compatibleLibraryVersion(tt.versions, tt.cgVersion); got != tt.want {
48+
t.Errorf("compatibleLibraryVersion() = %v, want %v", got, tt.want)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)