Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Continue to refactor loader
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Dec 29, 2015
1 parent 409eb66 commit 0bfb67c
Show file tree
Hide file tree
Showing 8 changed files with 629 additions and 497 deletions.
446 changes: 57 additions & 389 deletions importer/build.go

Large diffs are not rendered by default.

443 changes: 443 additions & 0 deletions importer/pkg.go

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions importer/pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package importer

import (
"errors"
"reflect"
"runtime"
"testing"
)

func TestGoodOSArch(t *testing.T) {
var (
thisOS = runtime.GOOS
thisArch = runtime.GOARCH
otherOS = func() string {
if thisOS != "darwin" {
return "darwin"
}
return "linux"
}()
otherArch = func() string {
if thisArch != "amd64" {
return "amd64"
}
return "386"
}()
)
tests := []struct {
name string
result bool
}{
{"file.go", true},
{"file.c", true},
{"file_foo.go", true},
{"file_" + thisArch + ".go", true},
{"file_" + otherArch + ".go", false},
{"file_android.go", thisOS == "linux"},
{"file_" + thisOS + ".go", true},
{"file_" + otherOS + ".go", false},
{"file_" + thisOS + "_" + thisArch + ".go", true},
{"file_" + otherOS + "_" + thisArch + ".go", false},
{"file_" + thisOS + "_" + otherArch + ".go", false},
{"file_" + otherOS + "_" + otherArch + ".go", false},
{"file_foo_" + thisArch + ".go", true},
{"file_foo_" + otherArch + ".go", false},
{"file_" + thisOS + ".c", true},
{"file_" + otherOS + ".c", false},
}

for _, test := range tests {
if goodOSArchFile(thisOS, thisArch, test.name, make(map[string]bool)) != test.result {
t.Fatalf("goodOSArchFile(%q) != %v", test.name, test.result)
}
}
}

func TestExt(t *testing.T) {
tests := []struct {
name, want string
}{
{"file.go", ".go"},
{"file", ""},
{".go", ".go"},
}

for _, tt := range tests {
got := ext(tt.name)
if got != tt.want {
t.Errorf("ext(%q): got %q, want %q", tt.name, got, tt.want)
}
}
}

func TestSplitQuoted(t *testing.T) {
tests := []struct {
str string
want []string
err error
}{{
str: `a b:"c d" 'e''f' "g\""`, want: []string{"a", "b:c d", "ef", `g"`},
}, {
str: `a b:"c d`, err: errors.New("unclosed quote"),
}, {
str: `a \`, err: errors.New("unfinished escaping"),
}}

for _, tt := range tests {
got, err := splitQuoted(tt.str)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("splitQuoted(%q): got err %v, want err %v", tt.str, err, tt.err)
continue
}
if err == nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("splitQuoted(%q): got %v, want %v", tt.str, got, tt.want)
}
}
}

func TestCgoEnabled(t *testing.T) {
tests := []struct {
gohostos, gohostarch string
gotargetos, gotargetarch string
want bool
}{{
"linux", "amd64", "linux", "amd64", true,
}, {
"linux", "amd64", "linux", "386", false,
}}

for _, tt := range tests {
got := cgoEnabled(tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch)
if got != tt.want {
t.Errorf("cgoEnabled(%q, %q, %q, %q): got %v, want %v", tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch, got, tt.want)
}
}
}
9 changes: 7 additions & 2 deletions importer/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (r *importReader) readImport(imports *[]string) {
// readComments is like ioutil.ReadAll, except that it only reads the leading
// block of comments in the file.
func readComments(f io.Reader) ([]byte, error) {
r := &importReader{b: bufio.NewReader(f)}
r := importReader{b: bufio.NewReader(f)}
r.peekByte(true)
if r.err == nil && !r.eof {
// Didn't reach EOF, so must have found a non-space byte. Remove it.
Expand All @@ -209,7 +209,12 @@ func readComments(f io.Reader) ([]byte, error) {

// readImports is like ioutil.ReadAll, except that it expects a Go file as input
// and stops reading the input once the imports have completed.
func readImports(f io.Reader, reportSyntaxError bool, imports *[]string) ([]byte, error) {
func readImports(f io.Reader) ([]byte, error) {
return readImports0(f, false)
}

func readImports0(f io.Reader, reportSyntaxError bool) ([]byte, error) {
var imports *[]string
r := &importReader{b: bufio.NewReader(f)}

r.readKeyword("package")
Expand Down
6 changes: 3 additions & 3 deletions importer/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func testRead(t *testing.T, tests []readTest, read func(io.Reader) ([]byte, erro
}

func TestReadImports(t *testing.T) {
testRead(t, readImportsTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
testRead(t, readImportsTests, readImports)
}

func TestReadComments(t *testing.T) {
Expand Down Expand Up @@ -207,7 +207,7 @@ var readFailuresTests = []readTest{

func TestReadFailures(t *testing.T) {
// Errors should be reported (true arg to readImports).
testRead(t, readFailuresTests, func(r io.Reader) ([]byte, error) { return readImports(r, true, nil) })
testRead(t, readFailuresTests, func(r io.Reader) ([]byte, error) { return readImports0(r, true) })
}

func TestReadFailuresIgnored(t *testing.T) {
Expand All @@ -222,5 +222,5 @@ func TestReadFailuresIgnored(t *testing.T) {
tt.err = ""
}
}
testRead(t, tests, func(r io.Reader) ([]byte, error) { return readImports(r, false, nil) })
testRead(t, tests, readImports)
}
8 changes: 0 additions & 8 deletions importer/syslist.go

This file was deleted.

96 changes: 1 addition & 95 deletions importer/syslist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,13 @@ import (
"os"
"path"
"runtime"
"testing"
)

// Default is the default Context for builds.
// It uses the GOARCH, GOOS, GOROOT, and GOPATH environment variables
// if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
var Default Context = defaultContext()

// Also known to cmd/dist/build.go.
var cgoEnabled = map[string]bool{
"darwin/386": true,
"darwin/amd64": true,
"darwin/arm": true,
"darwin/arm64": true,
"dragonfly/amd64": true,
"freebsd/386": true,
"freebsd/amd64": true,
"freebsd/arm": true,
"linux/386": true,
"linux/amd64": true,
"linux/arm": true,
"linux/arm64": true,
"linux/ppc64le": true,
"android/386": true,
"android/amd64": true,
"android/arm": true,
"netbsd/386": true,
"netbsd/amd64": true,
"netbsd/arm": true,
"openbsd/386": true,
"openbsd/amd64": true,
"solaris/amd64": true,
"windows/386": true,
"windows/amd64": true,
}

func envOr(name, def string) string {
s := os.Getenv(name)
if s == "" {
Expand All @@ -67,71 +38,6 @@ func defaultContext() Context {
// (perhaps it is the stub to use in that case) should say "+build !go1.x".
c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6"}

switch os.Getenv("CGO_ENABLED") {
case "1":
c.CgoEnabled = true
case "0":
c.CgoEnabled = false
default:
// cgo must be explicitly enabled for cross compilation builds
if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
break
}
c.CgoEnabled = false
}

c.CgoEnabled = cgoEnabled(runtime.GOOS, runtime.GOARCH, c.GOOS, c.GOARCH)
return c
}

var (
thisOS = runtime.GOOS
thisArch = runtime.GOARCH
otherOS = anotherOS()
otherArch = anotherArch()
)

func anotherOS() string {
if thisOS != "darwin" {
return "darwin"
}
return "linux"
}

func anotherArch() string {
if thisArch != "amd64" {
return "amd64"
}
return "386"
}

type GoodFileTest struct {
name string
result bool
}

var tests = []GoodFileTest{
{"file.go", true},
{"file.c", true},
{"file_foo.go", true},
{"file_" + thisArch + ".go", true},
{"file_" + otherArch + ".go", false},
{"file_" + thisOS + ".go", true},
{"file_" + otherOS + ".go", false},
{"file_" + thisOS + "_" + thisArch + ".go", true},
{"file_" + otherOS + "_" + thisArch + ".go", false},
{"file_" + thisOS + "_" + otherArch + ".go", false},
{"file_" + otherOS + "_" + otherArch + ".go", false},
{"file_foo_" + thisArch + ".go", true},
{"file_foo_" + otherArch + ".go", false},
{"file_" + thisOS + ".c", true},
{"file_" + otherOS + ".c", false},
}

func TestGoodOSArch(t *testing.T) {
for _, test := range tests {
if Default.goodOSArchFile(test.name, make(map[string]bool)) != test.result {
t.Fatalf("goodOSArchFile(%q) != %v", test.name, test.result)
}
}
}
3 changes: 3 additions & 0 deletions testdata/src/localimport/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package localimport

import "../localimport" // pure evil

0 comments on commit 0bfb67c

Please sign in to comment.