-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathload_test.go
147 lines (132 loc) · 3.44 KB
/
load_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package config
import (
"bytes"
"errors"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/wacul/ptr"
yaml "gopkg.in/yaml.v2"
)
func TestInGlobal(t *testing.T) {
os.Setenv("HOME", "/")
os.Setenv("GOPATH", "/")
os.Setenv("GOROOT", "/foo/bar/path/not/exists")
os.Setenv("PWD", "/home/kyoh86/go/src/github.com/kyoh86/richgo")
err := os.Setenv(LocalOnlyEnvName, "0")
if err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
sources := loadableSources()
if len(sources) == 0 {
t.Errorf("failed to get loadable sources")
}
}
func TestInGlobalWithNotCoveredEnv(t *testing.T) {
os.Clearenv()
os.Setenv("HOME", "/home/kyoh86")
os.Setenv("GOPATH", "/home/kyoh86/go")
os.Unsetenv("GOROOT")
os.Setenv("PWD", "/home/kyoh86/go/src/github.com/kyoh86/richgo")
isDirForTest = func(path string) bool {
return len(path) > 0
}
defer func() { isDirForTest = nil }()
err := os.Setenv(LocalOnlyEnvName, "0")
if err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
sources := loadableSources()
if len(sources) == 0 {
t.Errorf("failed to get loadable sources")
}
}
func TestInLocal(t *testing.T) {
err := os.Setenv(LocalOnlyEnvName, "1")
if err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
sources := loadableSources()
if len(sources) == 0 {
t.Errorf("failed to get loadable sources")
}
wd, err := os.Getwd()
if err != nil {
t.Errorf("failed to get working directory: %q", err)
t.FailNow()
}
for _, s := range sources {
rel, err := filepath.Rel(wd, s)
if err != nil {
t.Errorf("failed to get relative path to %q from %q: %q", s, wd, err)
}
if strings.HasPrefix(rel, "..") {
t.Errorf("expect that any source will be in local, but not (%q)", s)
}
}
}
func TestLoad(t *testing.T) {
t.Run("no trick", func(t *testing.T) {
if err := os.Setenv("PWD", "/home/kyoh86/go/src/github.com/kyoh86/richgo"); err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
if err := os.Setenv(LocalOnlyEnvName, "1"); err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
Load()
})
t.Run("with valid files", func(t *testing.T) {
if err := os.Setenv("PWD", "/home/kyoh86/go/src/github.com/kyoh86/richgo"); err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
if err := os.Setenv(LocalOnlyEnvName, "1"); err != nil {
t.Errorf("failed to set env: %s", err)
t.FailNow()
}
loadForTest = func(p string) ([]byte, error) {
return yaml.Marshal(&Config{
CoverThreshold: ptr.Int(10),
})
}
Load()
if C.CoverThreshold == nil {
t.Error("expect that a config loaded correctly but 'CoverThreshold' is nil")
t.FailNow()
}
if *C.CoverThreshold != 10 {
t.Errorf("expect that a 'CoverThreshold' is 10, but %d", *C.CoverThreshold)
}
})
t.Run("with invalid file", func(t *testing.T) {
loadForTest = func(p string) ([]byte, error) {
return nil, errors.New("test error")
}
w := bytes.Buffer{}
log.SetFlags(0)
log.SetOutput(&w)
Load()
if !strings.HasPrefix(w.String(), "error reading from") {
t.Error("expect that a Load func puts error in reading a file")
}
})
t.Run("with invalid yaml", func(t *testing.T) {
loadForTest = func(p string) ([]byte, error) {
return []byte(`":`), nil
}
w := bytes.Buffer{}
log.SetFlags(0)
log.SetOutput(&w)
Load()
if !strings.HasPrefix(w.String(), "error unmarshaling yaml from") {
t.Error("expect that a Load func puts error in unmarshaling a file")
}
})
}