| |
| |
| |
|
|
| package moddeps_test |
|
|
| import ( |
| "bytes" |
| "encoding/json" |
| "fmt" |
| "internal/testenv" |
| "io" |
| "io/fs" |
| "os" |
| "path/filepath" |
| "slices" |
| "sort" |
| "strings" |
| "sync" |
| "testing" |
|
|
| "golang.org/x/mod/module" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func TestAllDependencies(t *testing.T) { |
| goBin := testenv.GoToolPath(t) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for _, m := range findGorootModules(t) { |
| |
| |
| t.Run(m.Path+"(quick)", func(t *testing.T) { |
| t.Logf("module %s in directory %s", m.Path, m.Dir) |
|
|
| if m.hasVendor { |
| |
| |
| |
| cmd := testenv.Command(t, goBin, "list", "-mod=vendor", "-deps", "./...") |
| cmd.Dir = m.Dir |
| cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") |
| cmd.Stderr = new(strings.Builder) |
| _, err := cmd.Output() |
| if err != nil { |
| t.Errorf("%#q: %v\n%s", cmd, err, cmd.Stderr) |
| t.Logf("(Run 'go mod vendor' in %s to ensure that dependencies have been vendored.)", m.Dir) |
| } |
| return |
| } |
|
|
| |
| |
| cmd := testenv.Command(t, goBin, "list", "-mod=readonly", "-m", "all") |
| cmd.Dir = m.Dir |
| cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") |
| cmd.Stderr = new(strings.Builder) |
| out, err := cmd.Output() |
| if err != nil { |
| t.Fatalf("%#q: %v\n%s", cmd, err, cmd.Stderr) |
| } |
| if strings.TrimSpace(string(out)) != m.Path { |
| t.Errorf("%#q reported active modules other than %s:\n%s", cmd, m.Path, out) |
| t.Logf("(Run 'go mod tidy' in %s to ensure that no extraneous dependencies were added, or 'go mod vendor' to copy in imported packages.)", m.Dir) |
| } |
| }) |
| } |
|
|
| |
| |
| if testing.Short() { |
| return |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| testenv.MustHaveExternalNetwork(t) |
| if haveDiff := func() bool { |
| diff, err := testenv.Command(t, "diff", "--recursive", "--unified", ".", ".").CombinedOutput() |
| if err != nil || len(diff) != 0 { |
| return false |
| } |
| diff, err = testenv.Command(t, "diff", "--recursive", "--unified", ".", "..").CombinedOutput() |
| if err == nil || len(diff) == 0 { |
| return false |
| } |
| return true |
| }(); !haveDiff { |
| |
| |
| |
| |
| t.Skip("skipping because a diff command with support for --recursive and --unified flags is unavailable") |
| } |
|
|
| |
| |
| |
| |
| |
| var modcacheEnv []string |
| { |
| out, err := testenv.Command(t, goBin, "env", "GOMODCACHE").Output() |
| if err != nil { |
| t.Fatalf("%s env GOMODCACHE: %v", goBin, err) |
| } |
| modcacheOk := false |
| if gomodcache := string(bytes.TrimSpace(out)); gomodcache != "" { |
| if _, err := os.Stat(gomodcache); err == nil { |
| modcacheOk = true |
| } |
| } |
| if !modcacheOk { |
| modcacheEnv = []string{ |
| "GOMODCACHE=" + t.TempDir(), |
| "GOFLAGS=" + os.Getenv("GOFLAGS") + " -modcacherw", |
| } |
| } |
| } |
|
|
| |
| |
| bundleDir := t.TempDir() |
| r := runner{ |
| Dir: filepath.Join(testenv.GOROOT(t), "src/cmd"), |
| Env: append(os.Environ(), modcacheEnv...), |
| } |
| r.run(t, goBin, "build", "-mod=readonly", "-o", bundleDir, "golang.org/x/tools/cmd/bundle") |
|
|
| var gorootCopyDir string |
| for _, m := range findGorootModules(t) { |
| |
| |
| |
| |
| |
| |
| |
| |
| if gorootCopyDir == "" { |
| gorootCopyDir = makeGOROOTCopy(t) |
| } |
|
|
| t.Run(m.Path+"(thorough)", func(t *testing.T) { |
| t.Logf("module %s in directory %s", m.Path, m.Dir) |
|
|
| defer func() { |
| if t.Failed() { |
| |
| |
| |
| |
| gorootCopyDir = "" |
| } |
| }() |
|
|
| rel, err := filepath.Rel(testenv.GOROOT(t), m.Dir) |
| if err != nil { |
| t.Fatalf("filepath.Rel(%q, %q): %v", testenv.GOROOT(t), m.Dir, err) |
| } |
| r := runner{ |
| Dir: filepath.Join(gorootCopyDir, rel), |
| Env: append(append(os.Environ(), modcacheEnv...), |
| |
| "GOROOT="+gorootCopyDir, |
| |
| "PATH="+filepath.Join(gorootCopyDir, "bin")+string(filepath.ListSeparator)+ |
| bundleDir+string(filepath.ListSeparator)+os.Getenv("PATH"), |
| "GOWORK=off", |
| ), |
| } |
| goBinCopy := filepath.Join(gorootCopyDir, "bin", "go") |
| r.run(t, goBinCopy, "mod", "tidy") |
| r.run(t, goBinCopy, "mod", "verify") |
| r.run(t, goBinCopy, "mod", "vendor") |
| pkgs := packagePattern(m.Path) |
| r.run(t, goBinCopy, "generate", `-run=^//go:generate bundle `, pkgs) |
| advice := "$ cd " + m.Dir + "\n" + |
| "$ go mod tidy # to remove extraneous dependencies\n" + |
| "$ go mod vendor # to vendor dependencies\n" + |
| "$ go generate -run=bundle " + pkgs + " # to regenerate bundled packages\n" |
| if m.Path == "std" { |
| r.run(t, goBinCopy, "generate", "syscall", "internal/syscall/...") |
| advice += "$ go generate syscall internal/syscall/... # to regenerate syscall packages\n" |
| } |
| |
|
|
| diff, err := testenv.Command(t, "diff", "--recursive", "--unified", r.Dir, m.Dir).CombinedOutput() |
| if err != nil || len(diff) != 0 { |
| t.Errorf(`Module %s in %s is not tidy (-want +got): |
| |
| %s |
| To fix it, run: |
| |
| %s |
| (If module %[1]s is definitely tidy, this could mean |
| there's a problem in the go or bundle command.)`, m.Path, m.Dir, diff, advice) |
| } |
| }) |
| } |
| } |
|
|
| |
| |
| func packagePattern(modulePath string) string { |
| if modulePath == "std" { |
| return "std" |
| } |
| return modulePath + "/..." |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func makeGOROOTCopy(t *testing.T) string { |
| t.Helper() |
|
|
| gorootCopyDir := t.TempDir() |
| err := filepath.Walk(testenv.GOROOT(t), func(src string, info os.FileInfo, err error) error { |
| if err != nil { |
| return err |
| } |
| if info.IsDir() && src == filepath.Join(testenv.GOROOT(t), ".git") { |
| return filepath.SkipDir |
| } |
|
|
| rel, err := filepath.Rel(testenv.GOROOT(t), src) |
| if err != nil { |
| return fmt.Errorf("filepath.Rel(%q, %q): %v", testenv.GOROOT(t), src, err) |
| } |
| dst := filepath.Join(gorootCopyDir, rel) |
|
|
| if info.IsDir() && (src == filepath.Join(testenv.GOROOT(t), "bin") || |
| src == filepath.Join(testenv.GOROOT(t), "pkg")) { |
| |
| |
| if err := os.Symlink(src, dst); err == nil { |
| return filepath.SkipDir |
| } |
| } |
|
|
| perm := info.Mode() & os.ModePerm |
| if info.Mode()&os.ModeSymlink != 0 { |
| info, err = os.Stat(src) |
| if err != nil { |
| return err |
| } |
| perm = info.Mode() & os.ModePerm |
| } |
|
|
| |
| if info.IsDir() { |
| return os.MkdirAll(dst, perm|0200) |
| } |
|
|
| |
| |
| |
| s, err := os.Open(src) |
| if err != nil { |
| return err |
| } |
| defer s.Close() |
| d, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm) |
| if err != nil { |
| return err |
| } |
| _, err = io.Copy(d, s) |
| if err != nil { |
| d.Close() |
| return err |
| } |
| return d.Close() |
| }) |
| if err != nil { |
| t.Fatal(err) |
| } |
| t.Logf("copied GOROOT from %s to %s", testenv.GOROOT(t), gorootCopyDir) |
| return gorootCopyDir |
| } |
|
|
| type runner struct { |
| Dir string |
| Env []string |
| } |
|
|
| |
| func (r runner) run(t *testing.T, args ...string) { |
| t.Helper() |
| cmd := testenv.Command(t, args[0], args[1:]...) |
| cmd.Dir = r.Dir |
| cmd.Env = slices.Clip(r.Env) |
| if r.Dir != "" { |
| cmd.Env = append(cmd.Env, "PWD="+r.Dir) |
| } |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Logf("> %s\n", strings.Join(args, " ")) |
| t.Fatalf("command failed: %s\n%s", err, out) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func TestDependencyVersionsConsistent(t *testing.T) { |
| |
| type requirement struct { |
| Required module.Version |
| Replacement module.Version |
| } |
| seen := map[string]map[requirement][]gorootModule{} |
| for _, m := range findGorootModules(t) { |
| if !m.hasVendor { |
| |
| continue |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| vendor, err := os.ReadFile(filepath.Join(m.Dir, "vendor", "modules.txt")) |
| if err != nil { |
| t.Error(err) |
| continue |
| } |
|
|
| for _, line := range strings.Split(strings.TrimSpace(string(vendor)), "\n") { |
| parts := strings.Fields(line) |
| if len(parts) < 3 || parts[0] != "#" { |
| continue |
| } |
|
|
| |
| var r requirement |
| r.Required.Path = parts[1] |
| r.Required.Version = parts[2] |
| if len(parts) >= 5 && parts[3] == "=>" { |
| r.Replacement.Path = parts[4] |
| if module.CheckPath(r.Replacement.Path) != nil { |
| |
| |
| |
| |
| |
| |
| t.Errorf("cannot check consistency for filesystem-local replacement in module %s (%s):\n%s", m.Path, m.Dir, line) |
| } |
|
|
| if len(parts) >= 6 { |
| r.Replacement.Version = parts[5] |
| } |
| } |
|
|
| if seen[r.Required.Path] == nil { |
| seen[r.Required.Path] = make(map[requirement][]gorootModule) |
| } |
| seen[r.Required.Path][r] = append(seen[r.Required.Path][r], m) |
| } |
| } |
|
|
| |
| for path, versions := range seen { |
| if len(versions) > 1 { |
| t.Errorf("Modules within GOROOT require different versions of %s.", path) |
| for r, mods := range versions { |
| desc := new(strings.Builder) |
| desc.WriteString(r.Required.Version) |
| if r.Replacement.Path != "" { |
| fmt.Fprintf(desc, " => %s", r.Replacement.Path) |
| if r.Replacement.Version != "" { |
| fmt.Fprintf(desc, " %s", r.Replacement.Version) |
| } |
| } |
|
|
| for _, m := range mods { |
| t.Logf("%s\trequires %v", m.Path, desc) |
| } |
| } |
| } |
| } |
| } |
|
|
| type gorootModule struct { |
| Path string |
| Dir string |
| hasVendor bool |
| } |
|
|
| |
| func findGorootModules(t *testing.T) []gorootModule { |
| t.Helper() |
| goBin := testenv.GoToolPath(t) |
|
|
| goroot.once.Do(func() { |
| |
| |
| |
| root := testenv.GOROOT(t) |
| if !os.IsPathSeparator(root[len(root)-1]) { |
| root += string(filepath.Separator) |
| } |
| goroot.err = filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error { |
| if err != nil { |
| return err |
| } |
| if info.IsDir() && path != root && (info.Name() == "vendor" || info.Name() == "testdata") { |
| return filepath.SkipDir |
| } |
| if info.IsDir() && path == filepath.Join(testenv.GOROOT(t), "pkg") { |
| |
| |
| |
| |
| |
| return filepath.SkipDir |
| } |
| if info.IsDir() && path != root && (strings.HasPrefix(info.Name(), "_") || strings.HasPrefix(info.Name(), ".")) { |
| |
| |
| |
| return filepath.SkipDir |
| } |
| if info.IsDir() || info.Name() != "go.mod" { |
| return nil |
| } |
| dir := filepath.Dir(path) |
|
|
| |
| |
| cmd := testenv.Command(t, goBin, "list", "-json", "-m") |
| cmd.Dir = dir |
| cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") |
| cmd.Stderr = new(strings.Builder) |
| out, err := cmd.Output() |
| if err != nil { |
| return fmt.Errorf("'go list -json -m' in %s: %w\n%s", dir, err, cmd.Stderr) |
| } |
|
|
| var m gorootModule |
| if err := json.Unmarshal(out, &m); err != nil { |
| return fmt.Errorf("decoding 'go list -json -m' in %s: %w", dir, err) |
| } |
| if m.Path == "" || m.Dir == "" { |
| return fmt.Errorf("'go list -json -m' in %s failed to populate Path and/or Dir", dir) |
| } |
| if _, err := os.Stat(filepath.Join(dir, "vendor")); err == nil { |
| m.hasVendor = true |
| } |
| goroot.modules = append(goroot.modules, m) |
| return nil |
| }) |
| if goroot.err != nil { |
| return |
| } |
|
|
| |
| |
| |
| |
| knownGOROOTModules := [...]string{ |
| "std", |
| "cmd", |
| |
| } |
| var seen = make(map[string]bool) |
| for _, m := range goroot.modules { |
| seen[m.Path] = true |
| } |
| for _, m := range knownGOROOTModules { |
| if !seen[m] { |
| goroot.err = fmt.Errorf("findGorootModules didn't find the well-known module %q", m) |
| break |
| } |
| } |
| sort.Slice(goroot.modules, func(i, j int) bool { |
| return goroot.modules[i].Dir < goroot.modules[j].Dir |
| }) |
| }) |
| if goroot.err != nil { |
| t.Fatal(goroot.err) |
| } |
| return goroot.modules |
| } |
|
|
| |
| var goroot struct { |
| once sync.Once |
| modules []gorootModule |
| err error |
| } |
|
|