2013-08-27 16:49:13 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
package oracle_test
|
|
|
|
|
|
|
|
// This file defines a test framework for oracle queries.
|
|
|
|
//
|
|
|
|
// The files beneath testdata/src/main contain Go programs containing
|
|
|
|
// query annotations of the form:
|
|
|
|
//
|
|
|
|
// @verb id "select"
|
|
|
|
//
|
|
|
|
// where verb is the query mode (e.g. "callers"), id is a unique name
|
|
|
|
// for this query, and "select" is a regular expression matching the
|
|
|
|
// substring of the current line that is the query's input selection.
|
|
|
|
//
|
|
|
|
// The expected output for each query is provided in the accompanying
|
|
|
|
// .golden file.
|
|
|
|
//
|
|
|
|
// (Location information is not included because it's too fragile to
|
|
|
|
// display as text. TODO(adonovan): think about how we can test its
|
|
|
|
// correctness, since it is critical information.)
|
|
|
|
//
|
|
|
|
// Run this test with:
|
2014-11-09 14:50:40 -07:00
|
|
|
// % go test golang.org/x/tools/oracle -update
|
2013-08-27 15:58:26 -06:00
|
|
|
// to update the golden files.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-09-03 13:29:02 -06:00
|
|
|
"encoding/json"
|
2013-08-27 15:58:26 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2013-08-28 22:04:05 -06:00
|
|
|
"runtime"
|
2013-08-27 15:58:26 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/oracle"
|
2013-08-27 15:58:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var updateFlag = flag.Bool("update", false, "Update the golden files.")
|
|
|
|
|
|
|
|
type query struct {
|
2014-02-21 08:46:02 -07:00
|
|
|
id string // unique id
|
|
|
|
verb string // query mode, e.g. "callees"
|
|
|
|
posn token.Position // position of of query
|
|
|
|
filename string
|
|
|
|
queryPos string // value of -pos flag
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseRegexp(text string) (*regexp.Regexp, error) {
|
|
|
|
pattern, err := strconv.Unquote(text)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unquote %s", text)
|
|
|
|
}
|
|
|
|
return regexp.Compile(pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseQueries parses and returns the queries in the named file.
|
|
|
|
func parseQueries(t *testing.T, filename string) []*query {
|
|
|
|
filedata, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the file once to discover the test queries.
|
2014-11-17 11:50:23 -07:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, filename, filedata, parser.ParseComments)
|
2013-08-27 15:58:26 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := bytes.Split(filedata, []byte("\n"))
|
|
|
|
|
|
|
|
var queries []*query
|
|
|
|
queriesById := make(map[string]*query)
|
|
|
|
|
|
|
|
// Find all annotations of these forms:
|
|
|
|
expectRe := regexp.MustCompile(`@([a-z]+)\s+(\S+)\s+(\".*)$`) // @verb id "regexp"
|
|
|
|
for _, c := range f.Comments {
|
|
|
|
text := strings.TrimSpace(c.Text())
|
|
|
|
if text == "" || text[0] != '@' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
posn := fset.Position(c.Pos())
|
|
|
|
|
|
|
|
// @verb id "regexp"
|
|
|
|
match := expectRe.FindStringSubmatch(text)
|
|
|
|
if match == nil {
|
|
|
|
t.Errorf("%s: ill-formed query: %s", posn, text)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id := match[2]
|
|
|
|
if prev, ok := queriesById[id]; ok {
|
|
|
|
t.Errorf("%s: duplicate id %s", posn, id)
|
|
|
|
t.Errorf("%s: previously used here", prev.posn)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
q := &query{
|
|
|
|
id: id,
|
|
|
|
verb: match[1],
|
|
|
|
filename: filename,
|
2014-02-21 08:46:02 -07:00
|
|
|
posn: posn,
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
2014-02-21 08:46:02 -07:00
|
|
|
|
|
|
|
if match[3] != `"nopos"` {
|
|
|
|
selectRe, err := parseRegexp(match[3])
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: %s", posn, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find text of the current line, sans query.
|
|
|
|
// (Queries must be // not /**/ comments.)
|
|
|
|
line := lines[posn.Line-1][:posn.Column-1]
|
|
|
|
|
|
|
|
// Apply regexp to current line to find input selection.
|
|
|
|
loc := selectRe.FindIndex(line)
|
|
|
|
if loc == nil {
|
|
|
|
t.Errorf("%s: selection pattern %s doesn't match line %q",
|
|
|
|
posn, match[3], string(line))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes ASCII. TODO(adonovan): test on UTF-8.
|
|
|
|
linestart := posn.Offset - (posn.Column - 1)
|
|
|
|
|
|
|
|
// Compute the file offsets.
|
|
|
|
q.queryPos = fmt.Sprintf("%s:#%d,#%d",
|
|
|
|
filename, linestart+loc[0], linestart+loc[1])
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
queries = append(queries, q)
|
|
|
|
queriesById[id] = q
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the slice, not map, for deterministic iteration.
|
|
|
|
return queries
|
|
|
|
}
|
|
|
|
|
2013-10-01 08:17:26 -06:00
|
|
|
// WriteResult writes res (-format=plain) to w, stripping file locations.
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
func WriteResult(w io.Writer, q *oracle.Query) {
|
2013-10-01 08:17:26 -06:00
|
|
|
capture := new(bytes.Buffer) // capture standard output
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
q.WriteTo(capture)
|
2013-10-01 08:17:26 -06:00
|
|
|
for _, line := range strings.Split(capture.String(), "\n") {
|
|
|
|
// Remove a "file:line: " prefix.
|
|
|
|
if i := strings.Index(line, ": "); i >= 0 {
|
|
|
|
line = line[i+2:]
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s\n", line)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doQuery poses query q to the oracle and writes its response and
|
|
|
|
// error (if any) to out.
|
2013-09-03 13:29:02 -06:00
|
|
|
func doQuery(out io.Writer, q *query, useJson bool) {
|
2013-08-27 15:58:26 -06:00
|
|
|
fmt.Fprintf(out, "-------- @%s %s --------\n", q.verb, q.id)
|
|
|
|
|
|
|
|
var buildContext = build.Default
|
|
|
|
buildContext.GOPATH = "testdata"
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
query := oracle.Query{
|
|
|
|
Mode: q.verb,
|
|
|
|
Pos: q.queryPos,
|
|
|
|
Build: &buildContext,
|
|
|
|
Scope: []string{q.filename},
|
|
|
|
Reflection: true,
|
|
|
|
}
|
|
|
|
if err := oracle.Run(&query); err != nil {
|
2013-10-01 08:17:26 -06:00
|
|
|
fmt.Fprintf(out, "\nError: %s\n", err)
|
2013-09-03 13:29:02 -06:00
|
|
|
return
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:29:02 -06:00
|
|
|
if useJson {
|
|
|
|
// JSON output
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
b, err := json.MarshalIndent(query.Serial(), "", "\t")
|
2013-09-03 13:29:02 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(out, "JSON error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2013-09-24 13:08:14 -06:00
|
|
|
out.Write(b)
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
fmt.Fprintln(out)
|
2013-09-03 13:29:02 -06:00
|
|
|
} else {
|
|
|
|
// "plain" (compiler diagnostic format) output
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
WriteResult(out, &query)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOracle(t *testing.T) {
|
2013-08-28 22:04:05 -06:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:58:26 -06:00
|
|
|
for _, filename := range []string{
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"testdata/src/calls/main.go",
|
|
|
|
"testdata/src/describe/main.go",
|
|
|
|
"testdata/src/freevars/main.go",
|
|
|
|
"testdata/src/implements/main.go",
|
|
|
|
"testdata/src/implements-methods/main.go",
|
|
|
|
"testdata/src/imports/main.go",
|
|
|
|
"testdata/src/peers/main.go",
|
|
|
|
"testdata/src/pointsto/main.go",
|
2015-03-31 08:39:18 -06:00
|
|
|
"testdata/src/referrers/main.go",
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"testdata/src/reflection/main.go",
|
|
|
|
"testdata/src/what/main.go",
|
|
|
|
"testdata/src/whicherrs/main.go",
|
2013-09-03 13:29:02 -06:00
|
|
|
// JSON:
|
2013-12-13 16:00:55 -07:00
|
|
|
// TODO(adonovan): most of these are very similar; combine them.
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"testdata/src/calls-json/main.go",
|
|
|
|
"testdata/src/peers-json/main.go",
|
|
|
|
"testdata/src/describe-json/main.go",
|
|
|
|
"testdata/src/implements-json/main.go",
|
|
|
|
"testdata/src/implements-methods-json/main.go",
|
|
|
|
"testdata/src/pointsto-json/main.go",
|
|
|
|
"testdata/src/referrers-json/main.go",
|
|
|
|
"testdata/src/what-json/main.go",
|
2013-08-27 15:58:26 -06:00
|
|
|
} {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
useJson := strings.Contains(filename, "-json/")
|
2013-08-27 15:58:26 -06:00
|
|
|
queries := parseQueries(t, filename)
|
|
|
|
golden := filename + "lden"
|
|
|
|
got := filename + "t"
|
|
|
|
gotfh, err := os.Create(got)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Create(%s) failed: %s", got, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer gotfh.Close()
|
2014-04-10 11:17:20 -06:00
|
|
|
defer os.Remove(got)
|
2013-08-27 15:58:26 -06:00
|
|
|
|
|
|
|
// Run the oracle on each query, redirecting its output
|
|
|
|
// and error (if any) to the foo.got file.
|
|
|
|
for _, q := range queries {
|
2013-09-03 13:29:02 -06:00
|
|
|
doQuery(gotfh, q, useJson)
|
2013-08-27 15:58:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare foo.got with foo.golden.
|
2014-02-21 13:53:57 -07:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
cmd = exec.Command("/bin/diff", "-c", golden, got)
|
|
|
|
default:
|
|
|
|
cmd = exec.Command("/usr/bin/diff", "-u", golden, got)
|
|
|
|
}
|
2013-08-27 15:58:26 -06:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd.Stdout = buf
|
2014-04-15 13:39:38 -06:00
|
|
|
cmd.Stderr = os.Stderr
|
2013-08-27 15:58:26 -06:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Errorf("Oracle tests for %s failed: %s.\n%s\n",
|
|
|
|
filename, err, buf)
|
|
|
|
|
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", golden)
|
|
|
|
if err := exec.Command("/bin/cp", got, golden).Run(); err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|