Skip to content

Commit 4ebcfff

Browse files
committed
x/exp/cmd/gorelease: avoid using -mod=mod and support lazy module loading
Details: - Replaces `go list -mod=mod` with `go list -mod=readonly`. - Switches to `go get -d` to download dependencies rather than relying on `go list` doing so. - Missing go.sum/go.mod data is now an error. This CL will be followed by a CL that ensures the filepath.Walk stops at submodules, and a test asserting that. I figured it'd be good to do separately since this CL is getting to be hefty. Give a shout if you prefer otherwise! Updates golang/go#41456 Change-Id: I66f12fe0325e4fed432d8b10c1fe977068e4a030 Reviewed-on: https://go-review.googlesource.com/c/exp/+/263178 Run-TryBot: Jean de Klerk <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Trust: Jean de Klerk <[email protected]>
1 parent eab1b5e commit 4ebcfff

8 files changed

+337
-106
lines changed

cmd/gorelease/gorelease.go

Lines changed: 185 additions & 98 deletions
Large diffs are not rendered by default.

cmd/gorelease/path.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,30 @@ func hasFilePathPrefix(s, prefix string) bool {
5252
}
5353
}
5454

55+
// trimFilePathPrefix returns the given filesystem path s without the leading
56+
// prefix.
57+
func trimFilePathPrefix(s, prefix string) string {
58+
sv := strings.ToUpper(filepath.VolumeName(s))
59+
pv := strings.ToUpper(filepath.VolumeName(prefix))
60+
s = s[len(sv):]
61+
prefix = prefix[len(pv):]
62+
63+
if !hasFilePathPrefix(s, prefix) || len(prefix) == 0 {
64+
return s
65+
}
66+
if len(s) == len(prefix) {
67+
return ""
68+
}
69+
if prefix[len(prefix)-1] == filepath.Separator {
70+
return strings.TrimPrefix(s, prefix)
71+
}
72+
return s[len(prefix)+1:]
73+
}
74+
5575
// trimPathPrefix returns p without the leading prefix. Unlike
56-
// strings.TrimPrefix, the prefix will only match on slash-separted comopnent
76+
// strings.TrimPrefix, the prefix will only match on slash-separted component
5777
// boundaries, so trimPathPrefix("aa/b", "aa") returns "b", but
58-
// trimPathPrefix("aa/b", "a") retunrs "aa/b".
78+
// trimPathPrefix("aa/b", "a") returns "aa/b".
5979
func trimPathPrefix(p, prefix string) string {
6080
if prefix == "" {
6181
return p

cmd/gorelease/path_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,80 @@ func TestHasFilePathPrefix(t *testing.T) {
113113
}
114114
}
115115

116+
func TestTrimFilePathPrefix(t *testing.T) {
117+
type test struct {
118+
desc, path, prefix, want string
119+
}
120+
var tests []test
121+
if runtime.GOOS == "windows" {
122+
tests = []test{
123+
// Note: these two cases in which the result preserves the leading \
124+
// don't come up in reality in gorelease. That's because prefix is
125+
// always far to the right of the path parts (ex github.com/foo/bar
126+
// in C:\Users\foo\AppData\Local\Temp\...\github.com\foo\bar).
127+
{
128+
desc: "empty_prefix",
129+
path: `c:\a\b`,
130+
prefix: "",
131+
want: `\a\b`,
132+
}, {
133+
desc: "partial_component",
134+
path: `c:\aa\b`,
135+
prefix: `c:\a`,
136+
want: `\aa\b`,
137+
},
138+
139+
{
140+
desc: "drive_prefix",
141+
path: `c:\a\b`,
142+
prefix: `c:\`,
143+
want: `a\b`,
144+
}, {
145+
desc: "partial_prefix",
146+
path: `c:\a\b`,
147+
prefix: `c:\a`,
148+
want: `b`,
149+
}, {
150+
desc: "full_prefix",
151+
path: `c:\a\b`,
152+
prefix: `c:\a\b`,
153+
want: "",
154+
},
155+
}
156+
} else {
157+
tests = []test{
158+
{
159+
desc: "empty_prefix",
160+
path: "/a/b",
161+
prefix: "",
162+
want: "/a/b",
163+
}, {
164+
desc: "partial_prefix",
165+
path: "/a/b",
166+
prefix: "/a",
167+
want: "b",
168+
}, {
169+
desc: "full_prefix",
170+
path: "/a/b",
171+
prefix: "/a/b",
172+
want: "",
173+
}, {
174+
desc: "partial_component",
175+
path: "/aa/b",
176+
prefix: "/a",
177+
want: "/aa/b",
178+
},
179+
}
180+
}
181+
for _, test := range tests {
182+
t.Run(test.desc, func(t *testing.T) {
183+
if got := trimFilePathPrefix(test.path, test.prefix); got != test.want {
184+
t.Errorf("hasFilePathPrefix(%q, %q): got %v, want %v", test.path, test.prefix, got, test.want)
185+
}
186+
})
187+
}
188+
}
189+
116190
func TestTrimPathPrefix(t *testing.T) {
117191
for _, test := range []struct {
118192
desc, path, prefix, want string

cmd/gorelease/testdata/tidy/empty_sum.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
mod=example.com/tidy
22
base=v0.0.1
3-
success=0
3+
success=false
44
-- want --
5-
go.sum: one or more sums are missing.
6-
Run 'go mod tidy' to add missing sums.
5+
go.sum: one or more sums are missing. Run 'go mod tidy' to add missing sums.
76
-- go.mod --
87
module example.com/tidy
98

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod=example.com/tidy
2+
base=v0.0.1
3+
success=false
4+
-- want --
5+
example.com/tidy/subdir
6+
-----------------------
7+
Compatible changes:
8+
- package added
9+
10+
go.mod: the following requirements are needed
11+
example.com/[email protected]
12+
Run 'go mod tidy' to add missing requirements.
13+
-- go.mod --
14+
module example.com/tidy
15+
16+
go 1.12
17+
-- go.mod --
18+
module example.com/tidy
19+
20+
go 1.12
21+
-- tidy.go --
22+
package tidy
23+
-- subdir/tidy.go --
24+
package subpkg
25+
26+
import _ "example.com/basic/a"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod=example.com/tidy
2+
base=v0.0.1
3+
success=false
4+
-- want --
5+
example.com/tidy/subdir/subsubdir
6+
---------------------------------
7+
Compatible changes:
8+
- package added
9+
10+
go.mod: the following requirements are needed
11+
example.com/[email protected]
12+
Run 'go mod tidy' to add missing requirements.
13+
-- go.mod --
14+
module example.com/tidy
15+
16+
go 1.12
17+
-- go.mod --
18+
module example.com/tidy
19+
20+
go 1.12
21+
-- tidy.go --
22+
package tidy
23+
-- subdir/subsubdir/tidy.go --
24+
package subpkg
25+
26+
import _ "example.com/basic/a"

cmd/gorelease/testdata/tidy/no_sum.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
mod=example.com/tidy
22
base=v0.0.1
3-
success=0
3+
success=false
44
-- want --
5-
go.sum: one or more sums are missing.
6-
Run 'go mod tidy' to add missing sums.
5+
go.sum: one or more sums are missing. Run 'go mod tidy' to add missing sums.
76
-- go.mod --
87
module example.com/tidy
98

0 commit comments

Comments
 (0)