Debian Codegraph @

internal/xtools-internal/gocommand/invoke_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2020 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 gocommand_test
6
7 import (
8 "context"
9 "fmt"
10 "io/fs"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strings"
15 "testing"
16 "time"
17
18 "golang.org/x/sync/errgroup"
19 "honnef.co/go/tools/internal/xtools-internal/gocommand"
20 "honnef.co/go/tools/internal/xtools-internal/testenv"
21 )
22
23 func TestGoVersion(t *testing.T) {
24 testenv.NeedsTool(t, "go")
25
26 inv := gocommand.Invocation{
27 Verb: "version",
28 }
29 gocmdRunner := &gocommand.Runner{}
30 if _, err := gocmdRunner.Run(context.Background(), inv); err != nil {
31 t.Error(err)
32 }
33 }
34
35 // This is not a test of go/packages at all: it's a test of whether it
36 // is possible to delete the directory used by go list once it has
37 // finished. It is intended to evaluate the hypothesis (to explain
38 // issue #71544) that the go command, on Windows, occasionally fails
39 // to release all its handles to the temporary directory even when it
40 // should have finished.
41 //
42 // If this test ever fails, the combination of the gocommand package
43 // and the go command itself has a bug; this has been observed (#73503).
44 func TestRmdirAfterGoList_Runner(t *testing.T) {
45 t.Skip("flaky; see https://github.com/golang/go/issues/73736#issuecomment-2885407104")
46
47 testRmdirAfterGoList(t, func(ctx context.Context, dir string) {
48 var runner gocommand.Runner
49 stdout, stderr, friendlyErr, err := runner.RunRaw(ctx, gocommand.Invocation{
50 Verb: "list",
51 Args: []string{"-json", "example.com/p"},
52 WorkingDir: dir,
53 })
54 if ctx.Err() != nil {
55 return // don't report error if canceled
56 }
57 if err != nil || friendlyErr != nil {
58 t.Fatalf("go list failed: %v, %v (stdout=%s stderr=%s)",
59 err, friendlyErr, stdout, stderr)
60 }
61 })
62 }
63
64 // TestRmdirAfterGoList_Direct is a variant of
65 // TestRmdirAfterGoList_Runner that executes go list directly, to
66 // control for the substantial logic of the gocommand package.
67 //
68 // It has two variants: the first does not set WaitDelay; the second
69 // sets it to 30s. If the first variant ever fails, the go command
70 // itself has a bug; as of May 2025 this has never been observed.
71 //
72 // If the second variant fails, it indicates that the WaitDelay
73 // mechanism is responsible for causing Wait to return before the
74 // child process has naturally finished. This is to confirm the
75 // hypothesis at https://github.com/golang/go/issues/73736#issuecomment-2885407104.
76 func TestRmdirAfterGoList_Direct(t *testing.T) {
•77 t.Skip("Debian: fails in sbuild because go list cannot find package example.com/p (no network, package not in GOROOT/GOPATH)")
78 for _, delay := range []time.Duration{0, 30 * time.Second} {
79 t.Run(delay.String(), func(t *testing.T) {
80 testRmdirAfterGoList(t, func(ctx context.Context, dir string) {
81 cmd := exec.Command("go", "list", "-json", "example.com/p")
82 cmd.Dir = dir
83 cmd.Stdout = new(strings.Builder)
84 cmd.Stderr = new(strings.Builder)
85 cmd.WaitDelay = delay
86 err := cmd.Run()
87 if ctx.Err() != nil {
88 return // don't report error if canceled
89 }
90 if err != nil {
91 t.Fatalf("go list failed: %v (stdout=%s stderr=%s)",
92 err, cmd.Stdout, cmd.Stderr)
93 }
94 })
95 })
96 }
97 }
98
99 func testRmdirAfterGoList(t *testing.T, f func(ctx context.Context, dir string)) {
100 testenv.NeedsExec(t)
101
102 dir := t.TempDir()
103 if err := os.Mkdir(filepath.Join(dir, "p"), 0777); err != nil {
104 t.Fatalf("mkdir p: %v", err)
105 }
106
107 // Create a go.mod file and 100 trivial Go files for the go command to read.
108 if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com"), 0666); err != nil {
109 t.Fatal(err)
110 }
111 for i := range 100 {
112 filename := filepath.Join(dir, fmt.Sprintf("p/%d.go", i))
113 if err := os.WriteFile(filename, []byte("package p"), 0666); err != nil {
114 t.Fatal(err)
115 }
116 }
117
118 t0 := time.Now()
119 g, ctx := errgroup.WithContext(context.Background())
120 for range 10 {
121 g.Go(func() error {
122 f(ctx, dir)
123 // Return an error so that concurrent invocations are canceled.
124 return fmt.Errorf("oops")
125 })
126 }
127 g.Wait() // ignore error (expected)
128
129 t.Logf("10 concurrent executions (9 canceled) took %v", time.Since(t0))
130
131 // This is the critical operation.
132 if err := os.RemoveAll(dir); err != nil {
133 t.Errorf("failed to remove temp dir: %v", err)
134 // List the contents of the directory, for clues.
135 filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
136 t.Log(path, d, err)
137 return nil
138 }) // ignore error
139 }
140 }