Debian Codegraph @

fs/helpers_test.go

raw symbols SCIP
1 package fs_test
2
3 import (
4 "errors"
5 "flag"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
10 )
11
12 var childHelpers = map[string]func(){}
13
14 type childProcess struct {
15 name string
16 fn func()
17 }
18
19 var _ flag.Value = (*childProcess)(nil)
20
21 func (c *childProcess) String() string {
22 return c.name
23 }
24
25 func (c *childProcess) Set(s string) error {
26 fn, ok := childHelpers[s]
27 if !ok {
28 return errors.New("helper not found")
29 }
30 c.name = s
31 c.fn = fn
32 return nil
33 }
34
35 var childMode childProcess
36
37 func init() {
38 flag.Var(&childMode, "fuse.internal.child", "internal use only")
39 }
40
41 // childCmd prepares a test function to be run in a subprocess, with
42 // childMode set to true. Caller must still call Run or Start.
43 //
44 // Re-using the test executable as the subprocess is useful because
45 // now test executables can e.g. be cross-compiled, transferred
46 // between hosts, and run in settings where the whole Go development
47 // environment is not installed.
48 func childCmd(childName string) (*exec.Cmd, error) {
49 // caller may set cwd, so we can't rely on relative paths
50 executable, err := filepath.Abs(os.Args[0])
51 if err != nil {
52 return nil, err
53 }
54 cmd := exec.Command(executable, "-fuse.internal.child="+childName)
55 cmd.Stdout = os.Stdout
56 cmd.Stderr = os.Stderr
57 return cmd, nil
58 }
59
60 func TestMain(m *testing.M) {
61 flag.Parse()
62 if childMode.fn != nil {
63 childMode.fn()
64 os.Exit(0)
65 }
66 os.Exit(m.Run())
67 }