Skip to content

Commit ec7d628

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/gomote: add missing entry in isGoToolDistGenerated
A generated file (src/time/tzdata/zzipdata.go) was added in CL 455357. Also add a test to catch any generated files added in the future. Updates golang/go#43350. Change-Id: I4965744c7023a68a68609506b9cdc99a6f27ea4a Reviewed-on: https://go-review.googlesource.com/c/build/+/463155 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
1 parent cf38df6 commit ec7d628

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

cmd/gomote/push.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ func isGoToolDistGenerated(path string) bool {
328328
"src/cmd/internal/objabi/zbootstrap.go",
329329
"src/go/build/zcgo.go",
330330
"src/internal/buildcfg/zbootstrap.go",
331-
"src/runtime/internal/sys/zversion.go":
331+
"src/runtime/internal/sys/zversion.go",
332+
"src/time/tzdata/zzipdata.go":
332333
return true
333334
}
334335
return false

cmd/gomote/push_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2023 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 main
6+
7+
import (
8+
"bytes"
9+
"io/fs"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"regexp"
14+
"runtime"
15+
"strings"
16+
"testing"
17+
)
18+
19+
func TestIsGoToolDistGenerated(t *testing.T) {
20+
// This test verifies that all of the files reported by isGoToolDistGenerated
21+
// are marked as generated by dist and vice-versa.
22+
// This is the regexp given for such files by https://go.dev/s/generatedcode.
23+
generatedRE := regexp.MustCompile(`^// Code generated by .*dist.*; DO NOT EDIT\.\n`)
24+
25+
goroot := runtime.GOROOT()
26+
if goroot == "" {
27+
cmd := exec.Command("go", "env", "GOROOT")
28+
cmd.Stderr = new(strings.Builder)
29+
out, err := cmd.Output()
30+
goroot = string(bytes.TrimSpace(out))
31+
if err != nil || goroot == "" {
32+
t.Fatalf("Unable to locate GOROOT.\n%v: %v\n%s", cmd, err, cmd.Stderr)
33+
}
34+
}
35+
36+
// Add a trailing separator to follow GOROOT/src if is a symlink, as is
37+
// apparently the case for some third-party distributions of the toolchain.
38+
gorootSrc := filepath.Join(goroot, "src") + string(filepath.Separator)
39+
t.Logf("Checking generated files in %s.", gorootSrc)
40+
41+
err := filepath.WalkDir(gorootSrc, func(path string, d fs.DirEntry, err error) error {
42+
if err != nil {
43+
return err
44+
}
45+
46+
rel, err := filepath.Rel(goroot, path)
47+
if err != nil {
48+
return err
49+
}
50+
rel = filepath.ToSlash(rel)
51+
52+
got := isGoToolDistGenerated(rel)
53+
54+
if d.IsDir() {
55+
if got {
56+
t.Errorf("isGoToolDistGenerated(%q) = true, but %q is a directory", rel, rel)
57+
}
58+
return nil
59+
}
60+
61+
if !got && !strings.HasPrefix(filepath.Base(path), "z") {
62+
// By convention, fles generated by cmd/dist always have names starting with "z".
63+
// If other files aren't matched by isGoToolDistGenerated, don't bother reading
64+
// them to check.
65+
return nil
66+
}
67+
68+
b, err := os.ReadFile(path)
69+
if err != nil {
70+
return err
71+
}
72+
73+
match := generatedRE.Find(b)
74+
if match == nil {
75+
if got {
76+
t.Errorf("isGoToolDistGenerated(%q) = true; want false\n(no match for %v)", rel, generatedRE)
77+
}
78+
} else {
79+
if !got || testing.Verbose() {
80+
t.Logf("%s: %q", rel, match)
81+
}
82+
if !got {
83+
t.Errorf("isGoToolDistGenerated(%q) = false; want true", rel)
84+
}
85+
}
86+
return nil
87+
})
88+
if err != nil {
89+
t.Error(err)
90+
}
91+
}

0 commit comments

Comments
 (0)