golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gomote/push_test.go (about) 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 "reflect" 14 "regexp" 15 "runtime" 16 "slices" 17 "strings" 18 "testing" 19 ) 20 21 func testGOROOT(t *testing.T) string { 22 goroot := runtime.GOROOT() 23 if goroot == "" { 24 cmd := exec.Command("go", "env", "GOROOT") 25 var stderr bytes.Buffer 26 cmd.Stderr = &stderr 27 out, err := cmd.Output() 28 goroot = string(bytes.TrimSpace(out)) 29 if err != nil || goroot == "" { 30 t.Fatalf("go env GOROOT: %v\n%s", err, stderr.Bytes()) 31 } 32 } 33 return goroot 34 } 35 36 func TestGitIgnored(t *testing.T) { 37 goroot := testGOROOT(t) 38 _, err := os.Stat(filepath.Join(goroot, ".git")) 39 if err != nil { 40 t.Skipf("no $GOROOT/.git: %v", err) 41 } 42 43 m := map[string]string{ 44 filepath.Join(goroot, "bin/godoc"): "x", 45 filepath.Join(goroot, "bin/gofmt"): "x", 46 filepath.Join(goroot, "src/math/sin.go"): "x", 47 } 48 want := []string{ 49 filepath.Join(goroot, "bin/godoc"), 50 filepath.Join(goroot, "bin/gofmt"), 51 } 52 t.Logf("goroot: %s", goroot) 53 ignored := gitIgnored(goroot, m) 54 slices.Sort(ignored) 55 if !reflect.DeepEqual(ignored, want) { 56 t.Errorf("gitIgnored: wrong results\nhave %v\nwant %v", ignored, want) 57 } 58 } 59 60 func TestIsGoToolDistGenerated(t *testing.T) { 61 // This test verifies that all of the files reported by isGoToolDistGenerated 62 // are marked as generated by dist and vice-versa. 63 // This is the regexp given for such files by https://go.dev/s/generatedcode. 64 generatedRE := regexp.MustCompile(`^// Code generated by .*dist.*; DO NOT EDIT\.\n`) 65 66 // Add a trailing separator to follow GOROOT/src if is a symlink, as is 67 // apparently the case for some third-party distributions of the toolchain. 68 goroot := testGOROOT(t) 69 gorootSrc := filepath.Join(goroot, "src") + string(filepath.Separator) 70 t.Logf("Checking generated files in %s.", gorootSrc) 71 72 err := filepath.WalkDir(gorootSrc, func(path string, d fs.DirEntry, err error) error { 73 if err != nil { 74 return err 75 } 76 77 rel, err := filepath.Rel(goroot, path) 78 if err != nil { 79 return err 80 } 81 rel = filepath.ToSlash(rel) 82 83 got := isGoToolDistGenerated(rel) 84 85 if d.IsDir() { 86 if got { 87 t.Errorf("isGoToolDistGenerated(%q) = true, but %q is a directory", rel, rel) 88 } 89 return nil 90 } 91 92 if !got && !strings.HasPrefix(filepath.Base(path), "z") { 93 // By convention, fles generated by cmd/dist always have names starting with "z". 94 // If other files aren't matched by isGoToolDistGenerated, don't bother reading 95 // them to check. 96 return nil 97 } 98 99 b, err := os.ReadFile(path) 100 if err != nil { 101 return err 102 } 103 104 match := generatedRE.Find(b) 105 if match == nil { 106 if got { 107 t.Errorf("isGoToolDistGenerated(%q) = true; want false\n(no match for %v)", rel, generatedRE) 108 } 109 } else { 110 if !got || testing.Verbose() { 111 t.Logf("%s: %q", rel, match) 112 } 113 if !got { 114 t.Errorf("isGoToolDistGenerated(%q) = false; want true", rel) 115 } 116 } 117 return nil 118 }) 119 if err != nil { 120 t.Error(err) 121 } 122 }