Debian Codegraph @

internal/test/utils.go

raw symbols SCIP
1 // Package test provides testing utilities.
2 package test
3
4 import (
5 "flag"
6 "io"
7 "log"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "testing"
13 )
14
15 var network = flag.Bool("net", false, "allow network access")
16
17 // RequiresNetwork declares that a test requires network access. The test is
18 // skipped if network access isn't enabled with the -net flag.
19 func RequiresNetwork(t *testing.T) {
20 t.Helper()
21 if !*network {
22 t.Skip("requires network: enable with -net flag")
23 }
24 }
25
26 // Assembles asserts that the given assembly code passes the go assembler.
27 func Assembles(t *testing.T, asm []byte) {
28 t.Helper()
29
30 dir, clean := TempDir(t)
31 defer clean()
32
33 asmfilename := filepath.Join(dir, "asm.s")
34 if err := os.WriteFile(asmfilename, asm, 0o600); err != nil {
35 t.Fatal(err)
36 }
37
38 objfilename := filepath.Join(dir, "asm.o")
39
40 goexec(t, "tool", "asm", "-e", "-o", objfilename, asmfilename)
41 }
42
43 // TempDir creates a temp directory. Returns the path to the directory and a
44 // cleanup function.
45 func TempDir(t *testing.T) (string, func()) {
46 t.Helper()
47
48 dir, err := os.MkdirTemp("", "avo")
49 if err != nil {
50 t.Fatal(err)
51 }
52
53 return dir, func() {
54 if err := os.RemoveAll(dir); err != nil {
55 t.Fatal(err)
56 }
57 }
58 }
59
60 // ExecCommand executes the command, logging the command and output and failing
61 // the test on error.
62 func ExecCommand(t *testing.T, cmd *exec.Cmd) {
63 t.Helper()
64 t.Logf("exec: %s", cmd.Args)
65 if cmd.Dir != "" {
66 t.Logf("dir: %s", cmd.Dir)
67 }
68 b, err := cmd.CombinedOutput()
69 t.Logf("output:\n%s\n", string(b))
70 if err != nil {
71 t.Fatal(err)
72 }
73 }
74
75 // Exec executes the named program with the given arguments, logging the command
76 // and output and failing the test on error.
77 func Exec(t *testing.T, name string, arg ...string) {
78 t.Helper()
79 cmd := exec.Command(name, arg...)
80 ExecCommand(t, cmd)
81 }
82
83 // GoTool returns a best guess path to the "go" binary.
84 func GoTool() string {
85 var exeSuffix string
86 if runtime.GOOS == "windows" {
87 exeSuffix = ".exe"
88 }
89 path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
90 if _, err := os.Stat(path); err == nil {
91 return path
92 }
93 return "go"
94 }
95
96 // goexec runs a "go" command and checks the output.
97 func goexec(t *testing.T, arg ...string) {
98 t.Helper()
99 Exec(t, GoTool(), arg...)
100 }
101
102 // Logger builds a logger that writes to the test object.
103 func Logger(tb testing.TB) *log.Logger {
104 tb.Helper()
105 return log.New(Writer(tb), "test", log.LstdFlags)
106 }
107
108 type writer struct {
109 tb testing.TB
110 }
111
112 // Writer builds a writer that logs all writes to the test object.
113 func Writer(tb testing.TB) io.Writer {
114 tb.Helper()
115 return writer{tb}
116 }
117
118 func (w writer) Write(p []byte) (n int, err error) {
119 w.tb.Log(string(p))
120 return len(p), nil
121 }