1 | // Copyright 2018 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 analysisflags_test |
6 | |
7 | import ( |
8 | "fmt" |
9 | "os" |
10 | "os/exec" |
11 | "runtime" |
12 | "strings" |
13 | "testing" |
14 | |
15 | "golang.org/x/tools/go/analysis" |
16 | "golang.org/x/tools/go/analysis/internal/analysisflags" |
17 | ) |
18 | |
19 | func main() { |
20 | fmt.Println(analysisflags.Parse([]*analysis.Analyzer{ |
21 | {Name: "a1", Doc: "a1"}, |
22 | {Name: "a2", Doc: "a2"}, |
23 | {Name: "a3", Doc: "a3"}, |
24 | }, true)) |
25 | os.Exit(0) |
26 | } |
27 | |
28 | // This test fork/execs the main function above. |
29 | func TestExec(t *testing.T) { |
30 | if runtime.GOOS != "linux" { |
31 | t.Skipf("skipping fork/exec test on this platform") |
32 | } |
33 | |
34 | progname := os.Args[0] |
35 | |
36 | if os.Getenv("ANALYSISFLAGS_CHILD") == "1" { |
37 | // child process |
38 | os.Args = strings.Fields(progname + " " + os.Getenv("FLAGS")) |
39 | main() |
40 | panic("unreachable") |
41 | } |
42 | |
43 | for _, test := range []struct { |
44 | flags string |
45 | want string |
46 | }{ |
47 | {"", "[a1 a2 a3]"}, |
48 | {"-a1=0", "[a2 a3]"}, |
49 | {"-a1=1", "[a1]"}, |
50 | {"-a1", "[a1]"}, |
51 | {"-a1=1 -a3=1", "[a1 a3]"}, |
52 | {"-a1=1 -a3=0", "[a1]"}, |
53 | } { |
54 | cmd := exec.Command(progname, "-test.run=TestExec") |
55 | cmd.Env = append(os.Environ(), "ANALYSISFLAGS_CHILD=1", "FLAGS="+test.flags) |
56 | |
57 | output, err := cmd.CombinedOutput() |
58 | if err != nil { |
59 | t.Fatalf("exec failed: %v; output=<<%s>>", err, output) |
60 | } |
61 | |
62 | got := strings.TrimSpace(string(output)) |
63 | if got != test.want { |
64 | t.Errorf("got %s, want %s", got, test.want) |
65 | } |
66 | } |
67 | } |
68 |
Members