LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package oracle_test | 5 package oracle_test |
6 | 6 |
7 // This file defines a test framework for oracle queries. | 7 // This file defines a test framework for oracle queries. |
8 // | 8 // |
9 // The files beneath testdata/src/main contain Go programs containing | 9 // The files beneath testdata/src/main contain Go programs containing |
10 // query annotations of the form: | 10 // query annotations of the form: |
(...skipping 10 matching lines...) Expand all Loading... |
21 // (Location information is not included because it's too fragile to | 21 // (Location information is not included because it's too fragile to |
22 // display as text. TODO(adonovan): think about how we can test its | 22 // display as text. TODO(adonovan): think about how we can test its |
23 // correctness, since it is critical information.) | 23 // correctness, since it is critical information.) |
24 // | 24 // |
25 // Run this test with: | 25 // Run this test with: |
26 // % go test code.google.com/p/go.tools/oracle -update | 26 // % go test code.google.com/p/go.tools/oracle -update |
27 // to update the golden files. | 27 // to update the golden files. |
28 | 28 |
29 import ( | 29 import ( |
30 "bytes" | 30 "bytes" |
| 31 "encoding/json" |
31 "flag" | 32 "flag" |
32 "fmt" | 33 "fmt" |
33 "go/build" | 34 "go/build" |
34 "go/parser" | 35 "go/parser" |
35 "go/token" | 36 "go/token" |
36 "io" | 37 "io" |
37 "io/ioutil" | 38 "io/ioutil" |
38 "os" | 39 "os" |
39 "os/exec" | 40 "os/exec" |
40 "regexp" | 41 "regexp" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 156 |
156 // doQuery poses query q to the oracle and writes its response and | 157 // doQuery poses query q to the oracle and writes its response and |
157 // error (if any) to out. | 158 // error (if any) to out. |
158 func doQuery(out io.Writer, q *query, useJson bool) { | 159 func doQuery(out io.Writer, q *query, useJson bool) { |
159 fmt.Fprintf(out, "-------- @%s %s --------\n", q.verb, q.id) | 160 fmt.Fprintf(out, "-------- @%s %s --------\n", q.verb, q.id) |
160 | 161 |
161 var buildContext = build.Default | 162 var buildContext = build.Default |
162 buildContext.GOPATH = "testdata" | 163 buildContext.GOPATH = "testdata" |
163 res, err := oracle.Query([]string{q.filename}, | 164 res, err := oracle.Query([]string{q.filename}, |
164 q.verb, | 165 q.verb, |
165 » » fmt.Sprintf("%s %d-%d", q.filename, q.start, q.end), | 166 » » fmt.Sprintf("%s:%d-%d", q.filename, q.start, q.end), |
166 /*PTA-log=*/ nil, &buildContext) | 167 /*PTA-log=*/ nil, &buildContext) |
167 if err != nil { | 168 if err != nil { |
168 fmt.Fprintf(out, "\nError: %s\n", stripLocation(err.Error())) | 169 fmt.Fprintf(out, "\nError: %s\n", stripLocation(err.Error())) |
169 return | 170 return |
170 } | 171 } |
171 | 172 |
172 » if !useJson { | 173 » if useJson { |
| 174 » » // JSON output |
| 175 » » b, err := json.Marshal(res) |
| 176 » » if err != nil { |
| 177 » » » fmt.Fprintf(out, "JSON error: %s\n", err.Error()) |
| 178 » » » return |
| 179 » » } |
| 180 » » var buf bytes.Buffer |
| 181 » » if err := json.Indent(&buf, b, "", "\t"); err != nil { |
| 182 » » » fmt.Fprintf(out, "json.Indent failed: %s", err) |
| 183 » » » return |
| 184 » » } |
| 185 » » out.Write(buf.Bytes()) |
| 186 » } else { |
| 187 » » // "plain" (compiler diagnostic format) output |
173 capture := new(bytes.Buffer) // capture standard output | 188 capture := new(bytes.Buffer) // capture standard output |
174 res.WriteTo(capture) | 189 res.WriteTo(capture) |
175 for _, line := range strings.Split(capture.String(), "\n") { | 190 for _, line := range strings.Split(capture.String(), "\n") { |
176 fmt.Fprintf(out, "%s\n", stripLocation(line)) | 191 fmt.Fprintf(out, "%s\n", stripLocation(line)) |
177 } | 192 } |
178 } else { | |
179 b, err := res.ToResultJSON().ToIndentedBytes() | |
180 if err != nil { | |
181 fmt.Fprintf(out, "\nJSON error: %s\n", err.Error()) | |
182 return | |
183 } | |
184 out.Write(b) | |
185 } | 193 } |
186 } | 194 } |
187 | 195 |
188 func TestOracle(t *testing.T) { | 196 func TestOracle(t *testing.T) { |
189 switch runtime.GOOS { | 197 switch runtime.GOOS { |
190 case "windows": | 198 case "windows": |
191 t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS) | 199 t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS) |
192 } | 200 } |
193 | 201 |
194 for _, filename := range []string{ | 202 for _, filename := range []string{ |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 240 |
233 if *updateFlag { | 241 if *updateFlag { |
234 t.Logf("Updating %s...", golden) | 242 t.Logf("Updating %s...", golden) |
235 if err := exec.Command("/bin/cp", got, golden).R
un(); err != nil { | 243 if err := exec.Command("/bin/cp", got, golden).R
un(); err != nil { |
236 t.Errorf("Update failed: %s", err) | 244 t.Errorf("Update failed: %s", err) |
237 } | 245 } |
238 } | 246 } |
239 } | 247 } |
240 } | 248 } |
241 } | 249 } |
LEFT | RIGHT |