~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/testing/imports_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package testing_test
 
5
 
 
6
import (
 
7
        "go/build"
 
8
        "os"
 
9
        "path/filepath"
 
10
        "text/template"
 
11
 
 
12
        "github.com/juju/testing"
 
13
        jc "github.com/juju/testing/checkers"
 
14
        gc "gopkg.in/check.v1"
 
15
)
 
16
 
 
17
type importsSuite struct {
 
18
        testing.CleanupSuite
 
19
}
 
20
 
 
21
var _ = gc.Suite(&importsSuite{})
 
22
 
 
23
var pkgs = [][]string{{
 
24
        "arble.com/foo", "arble.com/bar", "arble.com/baz", "fmt",
 
25
}, {
 
26
        "arble.com/bar", "arble.com/baz",
 
27
}, {
 
28
        "arble.com/baz", "math",
 
29
}, {
 
30
        "arble.com/bar", "furble.com/fur",
 
31
}, {
 
32
        "furble.com/fur", "fmt", "C",
 
33
}}
 
34
 
 
35
var importsTests = []struct {
 
36
        pkgName string
 
37
        prefix  string
 
38
        expect  []string
 
39
}{{
 
40
        pkgName: "arble.com/foo",
 
41
        prefix:  "arble.com/",
 
42
        expect:  []string{"bar", "baz"},
 
43
}, {
 
44
        pkgName: "arble.com/foo",
 
45
        prefix:  "furble.com/",
 
46
        expect:  []string{"fur"},
 
47
}, {
 
48
        pkgName: "furble.com/fur",
 
49
        prefix:  "arble.com/",
 
50
        expect:  nil,
 
51
}}
 
52
 
 
53
func (s *importsSuite) TestImports(c *gc.C) {
 
54
        goPath := writePkgs(c)
 
55
        s.PatchValue(&build.Default.GOPATH, goPath)
 
56
 
 
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)
 
63
        }
 
64
}
 
65
 
 
66
func writePkgs(c *gc.C) (goPath string) {
 
67
        goPath = c.MkDir()
 
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)
 
74
                defer f.Close()
 
75
                err = sourceTemplate.Execute(f, p[1:])
 
76
                c.Assert(err, gc.IsNil)
 
77
        }
 
78
        return
 
79
}
 
80
 
 
81
var sourceTemplate = template.Must(template.New("").Parse(`
 
82
package pkg
 
83
import ({{range $f := $}}
 
84
_ {{printf "%q" .}}
 
85
{{end}})
 
86
`))