forked from mattn/gom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
78 lines (76 loc) · 1.44 KB
/
exec_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
)
func TestExec(t *testing.T) {
dir, err := ioutil.TempDir("", "gom")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(dir)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(cwd)
f, err := ioutil.TempFile(dir, "gom")
if err != nil {
t.Fatal(err)
}
vendor := filepath.Join(dir, vendorFolder)
err = os.MkdirAll(vendorSrc(vendor), 0755)
if err != nil {
t.Fatal(err)
}
oldstdout := stdout
defer func() {
stdout = oldstdout
}()
stdout = f
err = run([]string{"go", "env"}, None)
if err != nil {
t.Fatal(err)
}
f.Close()
stdout = oldstdout
b, err := ioutil.ReadFile(f.Name())
if err != nil {
t.Fatal(err)
}
gopath := ""
for _, line := range strings.Split(string(b), "\n") {
if runtime.GOOS == "windows" {
item := strings.SplitN(line, " ", 2)
if len(item) < 2 {
continue
}
if strings.HasPrefix(item[1], "GOPATH=") {
gopath = item[1][7:]
}
} else if strings.HasPrefix(line, "GOPATH=") {
gopath, _ = strconv.Unquote(line[7:])
}
}
found := false
vendorInfo, _ := os.Stat(vendor)
for _, s := range strings.Split(gopath, string(filepath.ListSeparator)) {
currentInfo, _ := os.Stat(s)
if os.SameFile(vendorInfo, currentInfo) {
found = true
break
}
}
if !found {
t.Fatalf("Expected %v, but %v:", vendor, gopath)
}
}