|
| 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