Skip to content

Commit 3b003c3

Browse files
author
Jay Conrod
committed
cmd/go/internal/module: fix inverted condition in MatchPathMajor
This was spotted in CL 200767. This change just ensures internal packages match their equivalents in x/mod. Also pulled in test added in CL 201517. Change-Id: I51d23d62697c256548f411930fcb6bccce51bf34 Reviewed-on: https://go-review.googlesource.com/c/go/+/201497 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 02196d3 commit 3b003c3

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/cmd/go/internal/module/module.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,17 @@ func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) {
503503
// MatchPathMajor reports whether the semantic version v
504504
// matches the path major version pathMajor.
505505
//
506-
// MatchPathMajor returns true if and only if CheckPathMajor returns non-nil.
506+
// MatchPathMajor returns true if and only if CheckPathMajor returns nil.
507507
func MatchPathMajor(v, pathMajor string) bool {
508-
return CheckPathMajor(v, pathMajor) != nil
508+
return CheckPathMajor(v, pathMajor) == nil
509509
}
510510

511511
// CheckPathMajor returns a non-nil error if the semantic version v
512512
// does not match the path major version pathMajor.
513513
func CheckPathMajor(v, pathMajor string) error {
514+
// TODO(jayconrod): return errors or panic for invalid inputs. This function
515+
// (and others) was covered by integration tests for cmd/go, and surrounding
516+
// code protected against invalid inputs like non-canonical versions.
514517
if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") {
515518
pathMajor = strings.TrimSuffix(pathMajor, "-unstable")
516519
}

src/cmd/go/internal/module/module_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,26 @@ func TestUnescapePath(t *testing.T) {
318318
}
319319
}
320320
}
321+
322+
func TestMatchPathMajor(t *testing.T) {
323+
for _, test := range []struct {
324+
v, pathMajor string
325+
want bool
326+
}{
327+
{"v0.0.0", "", true},
328+
{"v0.0.0", "/v2", false},
329+
{"v0.0.0", ".v0", true},
330+
{"v0.0.0-20190510104115-cbcb75029529", ".v1", true},
331+
{"v1.0.0", "/v2", false},
332+
{"v1.0.0", ".v1", true},
333+
{"v1.0.0", ".v1-unstable", true},
334+
{"v2.0.0+incompatible", "", true},
335+
{"v2.0.0", "", false},
336+
{"v2.0.0", "/v2", true},
337+
{"v2.0.0", ".v2", true},
338+
} {
339+
if got := MatchPathMajor(test.v, test.pathMajor); got != test.want {
340+
t.Errorf("MatchPathMajor(%q, %q) = %v, want %v", test.v, test.pathMajor, got, test.want)
341+
}
342+
}
343+
}

0 commit comments

Comments
 (0)