1
0
mirror of https://github.com/golang/go synced 2024-10-06 11:31:21 -06:00
go/src/cmd/api/goapi_test.go
Russ Cox ebe1664d27 go/build: replace FindTree, ScanDir, Tree, DirInfo with Import, Package
This is an API change, but one I have been promising would
happen when it was clear what the go command needed.

This is basically a complete replacement of what used to be here.

build.Tree is gone.

build.DirInfo is expanded and now called build.Package.

build.FindTree is now build.Import(package, srcDir, build.FindOnly).
The returned *Package contains information that FindTree returned,
but applicable only to a single package.

build.ScanDir is now build.ImportDir.

build.FindTree+build.ScanDir is now build.Import.

The new Import API allows specifying the source directory,
in order to resolve local imports (import "./foo") and also allows
scanning of packages outside of $GOPATH.  They will come back
with less information in the Package, but they will still work.

The old go/build API exposed both too much and too little.
This API is much closer to what the go command needs,
and it works well enough in the other places where it is
used.  Path is gone, so it can no longer be misused.  (Fixes issue 2749.)

This CL updates clients of go/build other than the go command.
The go command changes are in a separate CL, to be submitted
at the same time.

R=golang-dev, r, alex.brainman, adg
CC=golang-dev
https://golang.org/cl/5713043
2012-03-01 12:12:09 -05:00

76 lines
1.5 KiB
Go

// Copyright 2011 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.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"testing"
)
var (
updateGolden = flag.Bool("updategolden", false, "update golden files")
)
func TestGolden(t *testing.T) {
td, err := os.Open("testdata/src/pkg")
if err != nil {
t.Fatal(err)
}
fis, err := td.Readdir(0)
if err != nil {
t.Fatal(err)
}
for _, fi := range fis {
if !fi.IsDir() {
continue
}
w := NewWalker()
w.wantedPkg[fi.Name()] = true
w.root = "testdata/src/pkg"
goldenFile := filepath.Join("testdata", "src", "pkg", fi.Name(), "golden.txt")
w.WalkPackage(fi.Name())
if *updateGolden {
os.Remove(goldenFile)
f, err := os.Create(goldenFile)
if err != nil {
t.Fatal(err)
}
for _, feat := range w.Features() {
fmt.Fprintf(f, "%s\n", feat)
}
f.Close()
}
bs, err := ioutil.ReadFile(goldenFile)
if err != nil {
t.Fatalf("opening golden.txt for package %q: %v", fi.Name(), err)
}
wanted := strings.Split(string(bs), "\n")
sort.Strings(wanted)
for _, feature := range wanted {
if feature == "" {
continue
}
_, ok := w.features[feature]
if !ok {
t.Errorf("package %s: missing feature %q", fi.Name(), feature)
}
delete(w.features, feature)
}
for _, feature := range w.Features() {
t.Errorf("package %s: extra feature not in golden file: %q", fi.Name(), feature)
}
}
}