| 1 | // Copyright 2023 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 main_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | |
| 18 | "golang.org/x/tools/internal/testenv" |
| 19 | "golang.org/x/tools/txtar" |
| 20 | ) |
| 21 | |
| 22 | // Test runs the deadcode command on each scenario |
| 23 | // described by a testdata/*.txtar file. |
| 24 | func Test(t *testing.T) { |
| 25 | testenv.NeedsTool(t, "go") |
| 26 | if runtime.GOOS == "android" { |
| 27 | t.Skipf("the dependencies are not available on android") |
| 28 | } |
| 29 | |
| 30 | exe := buildDeadcode(t) |
| 31 | |
| 32 | matches, err := filepath.Glob("testdata/*.txtar") |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | for _, filename := range matches { |
| 37 | t.Run(filename, func(t *testing.T) { |
| 38 | t.Parallel() |
| 39 | |
| 40 | ar, err := txtar.ParseFile(filename) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
| 45 | // Write the archive files to the temp directory. |
| 46 | tmpdir := t.TempDir() |
| 47 | for _, f := range ar.Files { |
| 48 | filename := filepath.Join(tmpdir, f.Name) |
| 49 | if err := os.MkdirAll(filepath.Dir(filename), 0777); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err := os.WriteFile(filename, f.Data, 0666); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Parse archive comment as directives of these forms: |
| 58 | // |
| 59 | // [!]deadcode args... command-line arguments |
| 60 | // [!]want arg expected/unwanted string in output (or stderr) |
| 61 | // |
| 62 | // Args may be Go-quoted strings. |
| 63 | type testcase struct { |
| 64 | linenum int |
| 65 | args []string |
| 66 | wantErr bool |
| 67 | want map[string]bool // string -> sense |
| 68 | } |
| 69 | var cases []*testcase |
| 70 | var current *testcase |
| 71 | for i, line := range strings.Split(string(ar.Comment), "\n") { |
| 72 | line = strings.TrimSpace(line) |
| 73 | if line == "" || line[0] == '#' { |
| 74 | continue // skip blanks and comments |
| 75 | } |
| 76 | |
| 77 | words, err := words(line) |
| 78 | if err != nil { |
| 79 | t.Fatalf("cannot break line into words: %v (%s)", err, line) |
| 80 | } |
| 81 | switch kind := words[0]; kind { |
| 82 | case "deadcode", "!deadcode": |
| 83 | current = &testcase{ |
| 84 | linenum: i + 1, |
| 85 | want: make(map[string]bool), |
| 86 | args: words[1:], |
| 87 | wantErr: kind[0] == '!', |
| 88 | } |
| 89 | cases = append(cases, current) |
| 90 | case "want", "!want": |
| 91 | if current == nil { |
| 92 | t.Fatalf("'want' directive must be after 'deadcode'") |
| 93 | } |
| 94 | if len(words) != 2 { |
| 95 | t.Fatalf("'want' directive needs argument <<%s>>", line) |
| 96 | } |
| 97 | current.want[words[1]] = kind[0] != '!' |
| 98 | default: |
| 99 | t.Fatalf("%s: invalid directive %q", filename, kind) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | for _, tc := range cases { |
| 104 | t.Run(fmt.Sprintf("L%d", tc.linenum), func(t *testing.T) { |
| 105 | // Run the command. |
| 106 | cmd := exec.Command(exe, tc.args...) |
| 107 | cmd.Stdout = new(bytes.Buffer) |
| 108 | cmd.Stderr = new(bytes.Buffer) |
| 109 | cmd.Dir = tmpdir |
| 110 | cmd.Env = append(os.Environ(), "GOPROXY=", "GO111MODULE=on") |
| 111 | var got string |
| 112 | if err := cmd.Run(); err != nil { |
| 113 | if !tc.wantErr { |
| 114 | t.Fatalf("deadcode failed: %v (stderr=%s)", err, cmd.Stderr) |
| 115 | } |
| 116 | got = fmt.Sprint(cmd.Stderr) |
| 117 | } else { |
| 118 | if tc.wantErr { |
| 119 | t.Fatalf("deadcode succeeded unexpectedly (stdout=%s)", cmd.Stdout) |
| 120 | } |
| 121 | got = fmt.Sprint(cmd.Stdout) |
| 122 | } |
| 123 | |
| 124 | // Check each want directive. |
| 125 | for str, sense := range tc.want { |
| 126 | ok := true |
| 127 | if strings.Contains(got, str) != sense { |
| 128 | if sense { |
| 129 | t.Errorf("missing %q", str) |
| 130 | } else { |
| 131 | t.Errorf("unwanted %q", str) |
| 132 | } |
| 133 | ok = false |
| 134 | } |
| 135 | if !ok { |
| 136 | t.Errorf("got: <<%s>>", got) |
| 137 | } |
| 138 | } |
| 139 | }) |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // buildDeadcode builds the deadcode executable. |
| 146 | // It returns its path, and a cleanup function. |
| 147 | func buildDeadcode(t *testing.T) string { |
| 148 | bin := filepath.Join(t.TempDir(), "deadcode") |
| 149 | if runtime.GOOS == "windows" { |
| 150 | bin += ".exe" |
| 151 | } |
| 152 | cmd := exec.Command("go", "build", "-o", bin) |
| 153 | if out, err := cmd.CombinedOutput(); err != nil { |
| 154 | t.Fatalf("Building deadcode: %v\n%s", err, out) |
| 155 | } |
| 156 | return bin |
| 157 | } |
| 158 | |
| 159 | // words breaks a string into words, respecting |
| 160 | // Go string quotations around words with spaces. |
| 161 | func words(s string) ([]string, error) { |
| 162 | var words []string |
| 163 | for s != "" { |
| 164 | s = strings.TrimSpace(s) |
| 165 | var word string |
| 166 | if s[0] == '"' || s[0] == '`' { |
| 167 | prefix, err := strconv.QuotedPrefix(s) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | s = s[len(prefix):] |
| 172 | word, _ = strconv.Unquote(prefix) |
| 173 | } else { |
| 174 | prefix, rest, _ := strings.Cut(s, " ") |
| 175 | s = rest |
| 176 | word = prefix |
| 177 | } |
| 178 | words = append(words, word) |
| 179 | } |
| 180 | return words, nil |
| 181 | } |