-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoenv_test.go
72 lines (61 loc) · 1.91 KB
/
goenv_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
package goenv_test
import (
"fmt"
"io"
"io/ioutil"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/skipor/goenv"
)
func TestSigpanic(t *testing.T) {
t.Parallel()
sp := goenv.Sigpanic()
assert.Equal(t, "runtime.sigpanic", sp.Name())
}
func TestGoRoot(t *testing.T) {
t.Log("file ", gorootIoGoFile)
t.Log("goroot ", goenv.GoRoot())
expectedGoRootSrc := strings.TrimSuffix(gorootIoGoFile, ioGoFileTrimmed)
assert.Equal(t, expectedGoRootSrc, goenv.GoRootSrc())
assert.Equal(t, ioGoFileTrimmed, goenv.TrimGoRootSrc(gorootIoGoFile))
assert.Equal(t, strings.TrimSuffix(expectedGoRootSrc, "src/"), goenv.GoRoot())
assert.True(t, goenv.InGoroot(gorootIoGoFile))
}
func TestGoPath(t *testing.T) {
pc, file, _, _ := runtime.Caller(0)
t.Log("file ", file)
t.Log("gopath ", goenv.GoPath())
t.Log("func name ", runtime.FuncForPC(pc).Name())
require.True(t, strings.HasSuffix(file, thisFileTrimmed), file)
expectedGoPathSrc := strings.TrimSuffix(file, thisFileTrimmed)
assert.Equal(t, expectedGoPathSrc, goenv.GoPathSrc())
assert.Equal(t, thisFileTrimmed, goenv.TrimGoPathSrc(file))
assert.Equal(t, strings.TrimSuffix(expectedGoPathSrc, "src/"), goenv.GoPath())
assert.False(t, goenv.InGoroot(file))
}
const ioGoFileTrimmed = "io/io.go"
const thisFileTrimmed = "github.com/skipor/goenv/goenv_test.go"
// Another way to get file in $GOROOT.
var gorootIoGoFile = func() string {
var w callerWriter
io.WriteString(&w, "test")
const ioFileSuffix = "io/io.go"
if !strings.HasSuffix(w.callerFile, ioFileSuffix) {
panic(fmt.Sprintf("Expect get $GOROOT%s, but get %s", ioFileSuffix, w.callerFile))
}
return w.callerFile
}()
type callerWriter struct {
callerFile string
}
func (w *callerWriter) Write(p []byte) (int, error) {
var ok bool
_, w.callerFile, _, ok = runtime.Caller(1)
if !ok {
panic("undefined caller")
}
return ioutil.Discard.Write(p)
}