forked from juju/testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimports_test.go
86 lines (75 loc) · 1.78 KB
/
imports_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing_test
import (
"go/build"
"os"
"path/filepath"
"text/template"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
type importsSuite struct {
testing.CleanupSuite
}
var _ = gc.Suite(&importsSuite{})
var pkgs = [][]string{{
"arble.com/foo", "arble.com/bar", "arble.com/baz", "fmt",
}, {
"arble.com/bar", "arble.com/baz",
}, {
"arble.com/baz", "math",
}, {
"arble.com/bar", "furble.com/fur",
}, {
"furble.com/fur", "fmt", "C",
}}
var importsTests = []struct {
pkgName string
prefix string
expect []string
}{{
pkgName: "arble.com/foo",
prefix: "arble.com/",
expect: []string{"bar", "baz"},
}, {
pkgName: "arble.com/foo",
prefix: "furble.com/",
expect: []string{"fur"},
}, {
pkgName: "furble.com/fur",
prefix: "arble.com/",
expect: nil,
}}
func (s *importsSuite) TestImports(c *gc.C) {
goPath := writePkgs(c)
s.PatchValue(&build.Default.GOPATH, goPath)
c.Logf("gopath %q", build.Default.GOPATH)
for i, test := range importsTests {
c.Logf("test %d: %s %s", i, test.pkgName, test.prefix)
imports, err := testing.FindImports(test.pkgName, test.prefix)
c.Assert(err, gc.IsNil)
c.Assert(imports, jc.DeepEquals, test.expect)
}
}
func writePkgs(c *gc.C) (goPath string) {
goPath = c.MkDir()
for _, p := range pkgs {
dir := filepath.Join(goPath, "src", p[0])
err := os.MkdirAll(dir, 0777)
c.Assert(err, gc.IsNil)
f, err := os.Create(filepath.Join(dir, "pkg.go"))
c.Assert(err, gc.IsNil)
defer f.Close()
err = sourceTemplate.Execute(f, p[1:])
c.Assert(err, gc.IsNil)
}
return
}
var sourceTemplate = template.Must(template.New("").Parse(`
package pkg
import ({{range $f := $}}
_ {{printf "%q" .}}
{{end}})
`))