-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.go
76 lines (68 loc) · 1.61 KB
/
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
// Package tester provides the functionality needed to test a package.
package tester
import (
"io"
"os"
"os/exec"
"path"
"strings"
)
// Test contains the details to fetch packages and run tests
type Test struct {
// Gopath used to fetch the package and run the tests
Gopath string
// Stdout writer to get the command's output
Stdout io.Writer
// Stderr writer to get the command's output
Stderr io.Writer
}
func (t *Test) env() []string {
env := os.Environ()
result := make([]string, len(env))
for _, v := range env {
if strings.HasPrefix(v, "GOPATH=") {
continue
}
if strings.HasPrefix(v, "GOROOT=") {
continue
}
result = append(result, v)
}
result = append(result, "GOPATH="+t.Gopath)
return result
}
// Get fetches the package and its dependencies
func (t *Test) Get(pkg string) error {
cmd := exec.Command("go", "get", "-d", "-t", pkg)
t.prepareCmd(cmd)
if err := cmd.Run(); err != nil {
return err
}
cmd = exec.Command("go", "get", "-t", "./...")
cmd.Dir = path.Join(t.Gopath, "src", pkg)
t.prepareCmd(cmd)
return cmd.Run()
}
// Test runs the tests form the package
func (t *Test) Test(pkg string) error {
cmd := exec.Command("go", "test", "./...")
cmd.Dir = path.Join(t.Gopath, "src", pkg)
t.prepareCmd(cmd)
return cmd.Run()
}
// Build builds the package
func (t *Test) Build(pkg string) error {
cmd := exec.Command("go", "build", "./...")
cmd.Dir = path.Join(t.Gopath, "src", pkg)
t.prepareCmd(cmd)
return cmd.Run()
}
func (t *Test) prepareCmd(cmd *exec.Cmd) {
cmd.Env = t.env()
if t.Stdout != nil {
cmd.Stdout = t.Stdout
}
if t.Stderr != nil {
cmd.Stderr = t.Stderr
}
}