Skip to content

Commit 67bf1c9

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: fix (*mvsReqs).Max when the second argument is the empty string
As far as I can tell, this bug had gone unnoticed because everything that uses Max so far happened to only ever present the empty string as the first argument. For #37438 Change-Id: Ie8c42313157d2c2c17e4058c53b5bb026b95a1c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/266860 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent a19a4dc commit 67bf1c9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/cmd/go/internal/modload/mvs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r *mvsReqs) Required(mod module.Version) ([]module.Version, error) {
5858
// be chosen over other versions of the same module in the module dependency
5959
// graph.
6060
func (*mvsReqs) Max(v1, v2 string) string {
61-
if v1 != "" && semver.Compare(v1, v2) == -1 {
61+
if v1 != "" && (v2 == "" || semver.Compare(v1, v2) == -1) {
6262
return v2
6363
}
6464
return v1
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package modload_test
6+
7+
import (
8+
"testing"
9+
10+
"cmd/go/internal/modload"
11+
)
12+
13+
func TestReqsMax(t *testing.T) {
14+
type testCase struct {
15+
a, b, want string
16+
}
17+
reqs := modload.Reqs()
18+
for _, tc := range []testCase{
19+
{a: "v0.1.0", b: "v0.2.0", want: "v0.2.0"},
20+
{a: "v0.2.0", b: "v0.1.0", want: "v0.2.0"},
21+
{a: "", b: "v0.1.0", want: ""}, // "" is Target.Version
22+
{a: "v0.1.0", b: "", want: ""},
23+
{a: "none", b: "v0.1.0", want: "v0.1.0"},
24+
{a: "v0.1.0", b: "none", want: "v0.1.0"},
25+
{a: "none", b: "", want: ""},
26+
{a: "", b: "none", want: ""},
27+
} {
28+
max := reqs.Max(tc.a, tc.b)
29+
if max != tc.want {
30+
t.Errorf("Reqs().Max(%q, %q) = %q; want %q", tc.a, tc.b, max, tc.want)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)