Source file
test/fixedbugs/issue26411.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package main
14
15 import (
16 "bytes"
17 "fmt"
18 "io/ioutil"
19 "log"
20 "os"
21 "os/exec"
22 "path/filepath"
23 "regexp"
24 )
25
26 func main() {
27 tmpdir, err := ioutil.TempDir("", "issue26411")
28 if err != nil {
29 log.Fatalf("Failed to create temporary directory: %v", err)
30 }
31 defer os.RemoveAll(tmpdir)
32
33 tests := []struct {
34 code string
35 errors []string
36 }{
37 {
38 code: `
39 package main
40
41 func main() {
42 foo:
43 foo:
44 }
45 `,
46 errors: []string{
47 "^.+:5:1: label foo defined and not used\n",
48 ".+:6:1: label foo already defined at .+:5:1\n$",
49 },
50 },
51 {
52 code: `
53 package main
54
55 func main() {
56
57 bar:
58 bar:
59 bar:
60 bar :
61 }
62 `,
63
64 errors: []string{
65 "^.+:6:13: label bar defined and not used\n",
66 ".+:7:4: label bar already defined at .+:6:13\n",
67 ".+:8:1: label bar already defined at .+:6:13\n",
68 ".+:9:1: label bar already defined at .+:6:13\n$",
69 },
70 },
71 }
72
73 for i, test := range tests {
74 filename := filepath.Join(tmpdir, fmt.Sprintf("%d.go", i))
75 if err := ioutil.WriteFile(filename, []byte(test.code), 0644); err != nil {
76 log.Printf("#%d: failed to create file %s", i, filename)
77 continue
78 }
79 output, _ := exec.Command("go", "tool", "compile", "-p=p", filename).CombinedOutput()
80
81
82 for _, err := range test.errors {
83 rx := regexp.MustCompile(err)
84 match := rx.Find(output)
85 output = bytes.Replace(output, match, nil, 1)
86 }
87
88
89 if len(output) != 0 {
90 log.Printf("Test case %d has unmatched errors:\n%s", i, output)
91 }
92 }
93 }
94
View as plain text