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

Commit

Permalink
unexport context.Import as context.importPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Dec 31, 2015
1 parent 8e47ba9 commit 702abf5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
7 changes: 4 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (p *Project) NewContext(opts ...func(*Context) error) (*Context, error) {
GOOS: ctx.gotargetos,
GOARCH: ctx.gotargetarch,
CgoEnabled: cgoEnabled(ctx.gohostos, ctx.gohostarch, ctx.gotargetos, ctx.gotargetarch),
ReleaseTags: releaseTags,
ReleaseTags: releaseTags, // from go/build, see gb.go
BuildTags: ctx.buildtags,
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (c *Context) loadPackage(stack []string, path string) (*Package, error) {
return false
}

p, err := c.Import(path)
p, err := c.importPackage(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -295,7 +295,8 @@ func (c *Context) loadPackage(stack []string, path string) (*Package, error) {
return &pkg, nil
}

func (c *Context) Import(path string) (*importer.Package, error) {
// importPackage loads a package using the backing set of importers.
func (c *Context) importPackage(path string) (*importer.Package, error) {
pkg, err := c.importers[0].Import(path)
if err == nil {
return pkg, nil
Expand Down
61 changes: 61 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package gb

import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"runtime/debug"
"strings"
"testing"

"github.com/constabulary/gb/importer"
)

func testImportCycle(pkg string, t *testing.T) {
Expand Down Expand Up @@ -208,3 +211,61 @@ func TestCgoEnabled(t *testing.T) {
}
}
}

func TestContextImportPackage(t *testing.T) {
proj := testProject(t)
tests := []struct {
path string
err error
}{{
path: "a",
}, {
path: "cgomain",
}, {
path: "net/http", // loaded from GOROOT
}, {
path: "cmd",
err: &importer.NoGoError{Dir: filepath.Join(proj.Projectdir(), "src", "cmd")},
}}

for _, tt := range tests {
ctx, err := proj.NewContext()
if err != nil {
t.Fatal(err)
}
_, err = ctx.importPackage(tt.path)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("importPackage(%q): got %v, want %v", tt.path, err, tt.err)
}
}
}

func TestContextMatchPackages(t *testing.T) {
tests := []struct {
pattern string
want []string
}{{
pattern: "all",
want: []string{"a", "aprime", "b", "c", "cgomain", "cgoonlynotest", "cgotest", "cmd/f", "cppmain"},
}, {
pattern: "...",
want: []string{"a", "aprime", "b", "c", "cgomain", "cgoonlynotest", "cgotest", "cmd/f", "cppmain"},
}, {
pattern: "cmd/...",
want: []string{"cmd/f"},
}, {
pattern: ".../f",
want: []string{"cmd/f"},
}, {
pattern: "net/http",
want: nil, // should match nothing
}}

for _, tt := range tests {
ctx := testContext(t)
got := matchPackages(ctx, tt.pattern)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("matchPackages(..., %q): got %v, want %v", tt.pattern, got, tt.want)
}
}
}

0 comments on commit 702abf5

Please sign in to comment.