mirror of https://github.com/golang/go.git
cmd/compile: simplified test case (cleanup)
Follow-up on https://golang.org/cl/124595; no semantic changes. Updates #26411. Change-Id: Ic1c4622dbf79529ff61530f9c25ec742c2abe5ca Reviewed-on: https://go-review.googlesource.com/c/142720 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
0b63086f64
commit
e861c3e003
|
|
@ -29,9 +29,9 @@ func main() {
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
samples := []struct {
|
tests := []struct {
|
||||||
code string
|
code string
|
||||||
wantOutput []string
|
errors []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
code: `
|
code: `
|
||||||
|
|
@ -42,7 +42,7 @@ foo:
|
||||||
foo:
|
foo:
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
wantOutput: []string{
|
errors: []string{
|
||||||
"^.+:5:1: label foo defined and not used\n",
|
"^.+:5:1: label foo defined and not used\n",
|
||||||
".+:6:1: label foo already defined at .+:5:1\n$",
|
".+:6:1: label foo already defined at .+:5:1\n$",
|
||||||
},
|
},
|
||||||
|
|
@ -60,7 +60,7 @@ bar :
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
|
|
||||||
wantOutput: []string{
|
errors: []string{
|
||||||
"^.+:6:13: label bar defined and not used\n",
|
"^.+:6:13: label bar defined and not used\n",
|
||||||
".+:7:4: label bar already defined at .+:6:13\n",
|
".+:7:4: label bar already defined at .+:6:13\n",
|
||||||
".+:8:1: label bar already defined at .+:6:13\n",
|
".+:8:1: label bar already defined at .+:6:13\n",
|
||||||
|
|
@ -69,26 +69,24 @@ bar :
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, sample := range samples {
|
for i, test := range tests {
|
||||||
filename := filepath.Join(tmpdir, fmt.Sprintf("%d.go", i))
|
filename := filepath.Join(tmpdir, fmt.Sprintf("%d.go", i))
|
||||||
if err := ioutil.WriteFile(filename, []byte(sample.code), 0644); err != nil {
|
if err := ioutil.WriteFile(filename, []byte(test.code), 0644); err != nil {
|
||||||
log.Printf("#%d: failed to create file %s", i, filename)
|
log.Printf("#%d: failed to create file %s", i, filename)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
output, _ := exec.Command("go", "tool", "compile", filename).CombinedOutput()
|
output, _ := exec.Command("go", "tool", "compile", filename).CombinedOutput()
|
||||||
|
|
||||||
// Now match the output
|
// remove each matching error from the output
|
||||||
for _, regex := range sample.wantOutput {
|
for _, err := range test.errors {
|
||||||
reg := regexp.MustCompile(regex)
|
rx := regexp.MustCompile(err)
|
||||||
matches := reg.FindAll(output, -1)
|
match := rx.Find(output)
|
||||||
for _, match := range matches {
|
output = bytes.Replace(output, match, nil, 1) // remove match (which might be nil) from output
|
||||||
index := bytes.Index(output, match)
|
|
||||||
output = bytes.Join([][]byte{output[:index], output[index+len(match):]}, []byte(""))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// at this point all output should have been consumed
|
||||||
if len(output) != 0 {
|
if len(output) != 0 {
|
||||||
log.Printf("#%d: did not match all the output\nResidual output:\n\t%s", i, output)
|
log.Printf("Test case %d has unmatched errors:\n%s", i, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue