1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
12
"github.com/juju/testing"
13
jc "github.com/juju/testing/checkers"
14
gc "gopkg.in/check.v1"
17
type importsSuite struct {
21
var _ = gc.Suite(&importsSuite{})
23
var pkgs = [][]string{{
24
"arble.com/foo", "arble.com/bar", "arble.com/baz", "fmt",
26
"arble.com/bar", "arble.com/baz",
28
"arble.com/baz", "math",
30
"arble.com/bar", "furble.com/fur",
32
"furble.com/fur", "fmt", "C",
35
var importsTests = []struct {
40
pkgName: "arble.com/foo",
42
expect: []string{"bar", "baz"},
44
pkgName: "arble.com/foo",
45
prefix: "furble.com/",
46
expect: []string{"fur"},
48
pkgName: "furble.com/fur",
53
func (s *importsSuite) TestImports(c *gc.C) {
54
goPath := writePkgs(c)
55
s.PatchValue(&build.Default.GOPATH, goPath)
57
c.Logf("gopath %q", build.Default.GOPATH)
58
for i, test := range importsTests {
59
c.Logf("test %d: %s %s", i, test.pkgName, test.prefix)
60
imports, err := testing.FindImports(test.pkgName, test.prefix)
61
c.Assert(err, gc.IsNil)
62
c.Assert(imports, jc.DeepEquals, test.expect)
66
func writePkgs(c *gc.C) (goPath string) {
68
for _, p := range pkgs {
69
dir := filepath.Join(goPath, "src", p[0])
70
err := os.MkdirAll(dir, 0777)
71
c.Assert(err, gc.IsNil)
72
f, err := os.Create(filepath.Join(dir, "pkg.go"))
73
c.Assert(err, gc.IsNil)
75
err = sourceTemplate.Execute(f, p[1:])
76
c.Assert(err, gc.IsNil)
81
var sourceTemplate = template.Must(template.New("").Parse(`
83
import ({{range $f := $}}