github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/analysis/internal/analysisflags/flags_test.go (about) 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 // output should contain want 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 {"-V=full", "analysisflags.test version devel"}, 54 } { 55 cmd := exec.Command(progname, "-test.run=TestExec") 56 cmd.Env = append(os.Environ(), "ANALYSISFLAGS_CHILD=1", "FLAGS="+test.flags) 57 58 output, err := cmd.CombinedOutput() 59 if err != nil { 60 t.Fatalf("exec failed: %v; output=<<%s>>", err, output) 61 } 62 63 got := strings.TrimSpace(string(output)) 64 if !strings.Contains(got, test.want) { 65 t.Errorf("got %q, does not contain %q", got, test.want) 66 } 67 } 68 }