1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:04:52 -07:00

go/packages/packagestest: Testing with multiple drivers

This extracts some of the test code from packages, and adds the ability to run
the same test with multiple drivers.
This should generally be useful for all tools that run on top of go/packages as
well when writing tests for them.

Change-Id: I88c596ad07c0782270c5798d92ae29f7549943cf
Reviewed-on: https://go-review.googlesource.com/c/140118
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Cottrell 2018-10-05 13:08:11 -04:00
parent 249abec53b
commit 06f26fdaaa
7 changed files with 1061 additions and 575 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,238 @@
// Copyright 2018 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 packagestest creates temporary projects on disk for testing go tools on.
By changing the exporter used, you can create projects for multiple build
systems from the same description, and run the same tests on them in many
cases.
*/
package packagestest
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/tools/go/packages"
)
const (
// gorootModule is a special module name that indicates it contains source files
// that should replace the normal GOROOT
// in general you should not use this, it only exists for some very specialized
// tests.
gorootModule = "GOROOT"
)
var (
skipCleanup = flag.Bool("skip-cleanup", false, "Do not delete the temporary export folders") // for debugging
)
// Module is a representation of a go module.
type Module struct {
// Name is the base name of the module as it would be in the go.mod file.
Name string
// Files is the set of source files for all packages that make up the module.
// The keys are the file fragment that follows the module name, the value can
// be a string or byte slice, in which case it is the contents of the
// file, otherwise it must be a Writer function.
Files map[string]interface{}
}
// A Writer is a function that writes out a test file.
// It is provided the name of the file to write, and may return an error if it
// cannot write the file.
// These are used as the content of the Files map in a Module.
type Writer func(filename string) error
// Exported is returned by the Export function to report the structure that was produced on disk.
type Exported struct {
// Config is a correctly configured packages.Config ready to be passed to packages.Load.
// Exactly what it will contain varies depending on the Exporter being used.
Config *packages.Config
temp string
primary string
written map[string]map[string]string
}
// Exporter implementations are responsible for converting from the generic description of some
// test data to a driver specific file layout.
type Exporter interface {
// Name reports the name of the exporter, used in logging and sub-test generation.
Name() string
// Filename reports the system filename for test data source file.
// It is given the base directory, the module the file is part of and the filename fragment to
// work from.
Filename(exported *Exported, module, fragment string) string
// Finalize is called once all files have been written to write any extra data needed and modify
// the Config to match. It is handed the full list of modules that were encountered while writing
// files.
Finalize(exported *Exported) error
}
// All is the list of known exporters.
// This is used by TestAll to run tests with all the exporters.
var All []Exporter
// TestAll invokes the testing function once for each exporter registered in
// the All global.
// Each exporter will be run as a sub-test named after the exporter being used.
func TestAll(t *testing.T, f func(*testing.T, Exporter)) {
for _, e := range All {
t.Run(e.Name(), func(t *testing.T) { f(t, e) })
}
}
// Export is called to write out a test directory from within a test function.
// It takes the exporter and the build system agnostic module descriptions, and
// uses them to build a temporary directory.
// It returns an Exported with the results of the export.
// The Exported.Config is prepared for loading from the exported data.
// You must invoke Exported.Cleanup on the returned value to clean up.
// The file deletion in the cleanup can be skipped by setting the skip-cleanup
// flag when invoking the test, allowing the temporary directory to be left for
// debugging tests.
func Export(t *testing.T, exporter Exporter, modules []Module) *Exported {
temp, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", "_", -1))
if err != nil {
t.Fatal(err)
}
exported := &Exported{
Config: &packages.Config{
Dir: temp,
Env: append(os.Environ(), "GOPACKAGESDRIVER=off"),
},
temp: temp,
written: map[string]map[string]string{},
}
defer func() {
if t.Failed() || t.Skipped() {
exported.Cleanup()
}
}()
for _, module := range modules {
if exported.primary == "" && module.Name != gorootModule {
exported.primary = module.Name
}
for fragment, value := range module.Files {
fullpath := exporter.Filename(exported, module.Name, filepath.FromSlash(fragment))
written, ok := exported.written[module.Name]
if !ok {
written = map[string]string{}
exported.written[module.Name] = written
}
written[fragment] = fullpath
if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil {
t.Fatal(err)
}
switch value := value.(type) {
case Writer:
if err := value(fullpath); err != nil {
t.Fatal(err)
}
case string:
if err := ioutil.WriteFile(fullpath, []byte(value), 0644); err != nil {
t.Fatal(err)
}
default:
t.Fatalf("Invalid type %T in files, must be string or Writer", value)
}
}
}
if err := exporter.Finalize(exported); err != nil {
t.Fatal(err)
}
return exported
}
// Script returns a Writer that writes out contents to the file and sets the
// executable bit on the created file.
// It is intended for source files that are shell scripts.
func Script(contents string) Writer {
return func(filename string) error {
return ioutil.WriteFile(filename, []byte(contents), 0755)
}
}
// Link returns a Writer that creates a hard link from the specified source to
// the required file.
// This is used to link testdata files into the generated testing tree.
func Link(source string) Writer {
return func(filename string) error {
return os.Link(source, filename)
}
}
// Symlink returns a Writer that creates a symlink from the specified source to the
// required file.
// This is used to link testdata files into the generated testing tree.
func Symlink(source string) Writer {
if !strings.HasPrefix(source, ".") {
if abspath, err := filepath.Abs(source); err == nil {
if _, err := os.Stat(source); !os.IsNotExist(err) {
source = abspath
}
}
}
return func(filename string) error {
return os.Symlink(source, filename)
}
}
// Copy returns a Writer that copies a file from the specified source to the
// required file.
// This is used to copy testdata files into the generated testing tree.
func Copy(source string) Writer {
return func(filename string) error {
stat, err := os.Stat(source)
if err != nil {
return err
}
if !stat.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("Cannot copy non regular file %s", source)
}
contents, err := ioutil.ReadFile(source)
if err != nil {
return err
}
return ioutil.WriteFile(filename, contents, stat.Mode())
}
}
// Cleanup removes the temporary directory (unless the --skip-cleanup flag was set)
// It is safe to call cleanup multiple times.
func (e *Exported) Cleanup() {
if e.temp == "" {
return
}
if *skipCleanup {
log.Printf("Skipping cleanup of temp dir: %s", e.temp)
return
}
os.RemoveAll(e.temp) // ignore errors
e.temp = ""
}
// Temp returns the temporary directory that was generated.
func (e *Exported) Temp() string {
return e.temp
}
// File returns the full path for the given module and file fragment.
func (e *Exported) File(module, fragment string) string {
if m := e.written[module]; m != nil {
return m[fragment]
}
return ""
}

View File

@ -0,0 +1,68 @@
// Copyright 2018 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 packagestest_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"golang.org/x/tools/go/packages/packagestest"
)
var testdata = []packagestest.Module{{
Name: "golang.org/fake1",
Files: map[string]interface{}{
"a.go": packagestest.Symlink("testdata/a.go"),
"b.go": "package fake1",
},
}, {
Name: "golang.org/fake2",
Files: map[string]interface{}{
"other/a.go": "package fake2",
},
}}
type fileTest struct {
module, fragment, expect string
check func(t *testing.T, filename string)
}
func checkFiles(t *testing.T, exported *packagestest.Exported, tests []fileTest) {
for _, test := range tests {
expect := filepath.Join(exported.Temp(), filepath.FromSlash(test.expect))
got := exported.File(test.module, test.fragment)
if got == "" {
t.Errorf("File %v missing from the output", expect)
} else if got != expect {
t.Errorf("Got file %v, expected %v", got, expect)
}
if test.check != nil {
test.check(t, got)
}
}
}
func checkLink(expect string) func(t *testing.T, filename string) {
expect = filepath.FromSlash(expect)
return func(t *testing.T, filename string) {
if target, err := os.Readlink(filename); err != nil {
t.Errorf("Error checking link %v: %v", filename, err)
} else if target != expect {
t.Errorf("Link %v does not match, got %v expected %v", filename, target, expect)
}
}
}
func checkContent(expect string) func(t *testing.T, filename string) {
return func(t *testing.T, filename string) {
if content, err := ioutil.ReadFile(filename); err != nil {
t.Errorf("Error reading %v: %v", filename, err)
} else if string(content) != expect {
t.Errorf("Content of %v does not match, got %v expected %v", filename, string(content), expect)
}
}
}

View File

@ -0,0 +1,74 @@
// Copyright 2018 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 packagestest
import (
"path"
"path/filepath"
)
// GOPATH is the exporter that produces GOPATH layouts.
// Each "module" is put in it's own GOPATH entry to help test complex cases.
// Given the two files
// golang.org/repoa#a/a.go
// golang.org/repob#b/b.go
// You would get the directory layout
// /sometemporarydirectory
// ├── repoa
// │ └── src
// │ └── golang.org
// │ └── repoa
// │ └── a
// │ └── a.go
// └── repob
// └── src
// └── golang.org
// └── repob
// └── b
// └── b.go
// GOPATH would be set to
// /sometemporarydirectory/repoa;/sometemporarydirectory/repob
// and the working directory would be
// /sometemporarydirectory/repoa/src
var GOPATH = gopath{}
func init() {
All = append(All, GOPATH)
}
type gopath struct{}
func (gopath) Name() string {
return "GOPATH"
}
func (gopath) Filename(exported *Exported, module, fragment string) string {
return filepath.Join(gopathDir(exported, module), "src", module, fragment)
}
func (gopath) Finalize(exported *Exported) error {
exported.Config.Env = append(exported.Config.Env, "GO111MODULE=off")
gopath := ""
for module := range exported.written {
if gopath != "" {
gopath += string(filepath.ListSeparator)
}
dir := gopathDir(exported, module)
if module == gorootModule {
exported.Config.Env = append(exported.Config.Env, "GOROOT="+dir)
continue
}
gopath += dir
if module == exported.primary {
exported.Config.Dir = filepath.Join(dir, "src")
}
}
exported.Config.Env = append(exported.Config.Env, "GOPATH="+gopath)
return nil
}
func gopathDir(exported *Exported, module string) string {
return filepath.Join(exported.temp, path.Base(module))
}

View File

@ -0,0 +1,27 @@
// Copyright 2018 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 packagestest_test
import (
"path/filepath"
"testing"
"golang.org/x/tools/go/packages/packagestest"
)
func TestGOPATHExport(t *testing.T) {
exported := packagestest.Export(t, packagestest.GOPATH, testdata)
defer exported.Cleanup()
// Check that the cfg contains all the right bits
var expectDir = filepath.Join(exported.Temp(), "fake1", "src")
if exported.Config.Dir != expectDir {
t.Errorf("Got working directory %v expected %v", exported.Config.Dir, expectDir)
}
checkFiles(t, exported, []fileTest{
{"golang.org/fake1", "a.go", "fake1/src/golang.org/fake1/a.go", checkLink("testdata/a.go")},
{"golang.org/fake1", "b.go", "fake1/src/golang.org/fake1/b.go", checkContent("package fake1")},
{"golang.org/fake2", "other/a.go", "fake2/src/golang.org/fake2/other/a.go", checkContent("package fake2")},
})
}

View File

@ -0,0 +1,82 @@
// Copyright 2018 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.
// +build go1.11
package packagestest
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"path/filepath"
)
// Modules is the exporter that produces module layouts.
// Each "repository" is put in it's own module, and the module file generated
// will have replace directives for all other modules.
// Given the two files
// golang.org/repoa#a/a.go
// golang.org/repob#b/b.go
// You would get the directory layout
// /sometemporarydirectory
// ├── repoa
// │ ├── a
// │ │ └── a.go
// │ └── go.mod
// └── repob
// ├── b
// │ └── b.go
// └── go.mod
// and the working directory would be
// /sometemporarydirectory/repoa
var Modules = modules{}
func init() {
All = append(All, Modules)
}
type modules struct{}
func (modules) Name() string {
return "Modules"
}
func (modules) Filename(exported *Exported, module, fragment string) string {
return filepath.Join(moduleDir(exported, module), fragment)
}
func (modules) Finalize(exported *Exported) error {
exported.Config.Env = append(exported.Config.Env, "GO111MODULE=on")
for module, files := range exported.written {
dir := gopathDir(exported, module)
if module == gorootModule {
exported.Config.Env = append(exported.Config.Env, "GOROOT="+dir)
continue
}
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "module %v\n", module)
// add replace directives to the paths of all other modules written
for other := range exported.written {
if other == gorootModule || other == module {
continue
}
fmt.Fprintf(buf, "replace %v => %v\n", other, moduleDir(exported, other))
}
modfile := filepath.Join(dir, "go.mod")
if err := ioutil.WriteFile(modfile, buf.Bytes(), 0644); err != nil {
return err
}
files["go.mod"] = modfile
if module == exported.primary {
exported.Config.Dir = dir
}
}
return nil
}
func moduleDir(exported *Exported, module string) string {
return filepath.Join(exported.temp, path.Base(module))
}

View File

@ -0,0 +1,31 @@
// Copyright 2018 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.
// +build go1.11
package packagestest_test
import (
"path/filepath"
"testing"
"golang.org/x/tools/go/packages/packagestest"
)
func TestModulesExport(t *testing.T) {
exported := packagestest.Export(t, packagestest.Modules, testdata)
defer exported.Cleanup()
// Check that the cfg contains all the right bits
var expectDir = filepath.Join(exported.Temp(), "fake1")
if exported.Config.Dir != expectDir {
t.Errorf("Got working directory %v expected %v", exported.Config.Dir, expectDir)
}
checkFiles(t, exported, []fileTest{
{"golang.org/fake1", "go.mod", "fake1/go.mod", nil},
{"golang.org/fake1", "a.go", "fake1/a.go", checkLink("testdata/a.go")},
{"golang.org/fake1", "b.go", "fake1/b.go", checkContent("package fake1")},
{"golang.org/fake2", "go.mod", "fake2/go.mod", nil},
{"golang.org/fake2", "other/a.go", "fake2/other/a.go", checkContent("package fake2")},
})
}