forked from x-motemen/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_gomod_test.go
213 lines (177 loc) · 4.54 KB
/
session_gomod_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package gore
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func chdir(dir string) func() {
d, _ := os.Getwd()
os.Chdir(dir)
return func() { os.Chdir(d) }
}
func setenv(name, value string) func() {
v := os.Getenv(name)
os.Setenv(name, value)
return func() { os.Setenv(name, v) }
}
func gomodSetup(t *testing.T) func() {
tempDir, err := ioutil.TempDir("", "gore-")
require.NoError(t, err)
mod1Dir := filepath.Join(tempDir, "mod1")
require.NoError(t, os.Mkdir(mod1Dir, 0700))
require.NoError(t, ioutil.WriteFile(filepath.Join(mod1Dir, "go.mod"), []byte(`module mod1
`), 0600))
require.NoError(t, ioutil.WriteFile(filepath.Join(mod1Dir, "mod1.go"), []byte(`package mod1
const Value = 10
`), 0600))
mod2Dir := filepath.Join(tempDir, "mod2")
require.NoError(t, os.Mkdir(mod2Dir, 0700))
require.NoError(t, ioutil.WriteFile(filepath.Join(mod2Dir, "go.mod"), []byte(fmt.Sprintf(`module mod2
replace mod1 => %s
require mod1 v0.0.0-00010101000000-000000000000
`, strconv.Quote(mod1Dir))), 0600))
require.NoError(t, ioutil.WriteFile(filepath.Join(mod2Dir, "mod2.go"), []byte(`package mod2
import "mod1"
func Foo() int {
return mod1.Value
}
`), 0600))
mod3Dir := filepath.Join(mod2Dir, "mod3")
require.NoError(t, os.Mkdir(mod3Dir, 0700))
require.NoError(t, ioutil.WriteFile(filepath.Join(mod3Dir, "mod3.go"), []byte(`package mod3
func Bar() string {
return "mod3"
}
`), 0600))
mod4Dir := filepath.Join(mod2Dir, "mod4")
require.NoError(t, os.Mkdir(mod4Dir, 0700))
restore := chdir(mod2Dir)
return func() {
defer os.RemoveAll(tempDir)
defer restore()
}
}
func TestSessionEval_Gomod(t *testing.T) {
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
defer gomodSetup(t)()
s, err := NewSession(stdout, stderr)
defer s.Clear()
require.NoError(t, err)
codes := []string{
`:i mod2`,
`mod2.Foo()`,
`mod2.Foo() + mod2.Foo()`,
`:clear`,
`:i mod2`,
`mod2.Foo()`,
`:i mod1`,
`3 * mod1.Value`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, "10\n20\n10\n30\n", stdout.String())
assert.Equal(t, ``, stderr.String())
}
func TestSessionEval_Gomod_AutoImport(t *testing.T) {
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
defer gomodSetup(t)()
s, err := NewSession(stdout, stderr)
defer s.Clear()
require.NoError(t, err)
s.autoImport = true
codes := []string{
`mod2.Foo()`,
`mod2.Foo() + mod2.Foo()`,
`:clear`,
`mod2.Foo()`,
`3 * mod1.Value`,
`:t mod2.Foo`,
`:d mod2.Foo`,
`mod3.Bar()`,
`:t mod3.Bar`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, `10
20
10
30
func() int
package mod2 // import "mod2"
func Foo() int
"mod3"
func() string
`, stdout.String())
assert.Equal(t, ``, stderr.String())
}
func TestSessionEval_Gomod_DeepDir(t *testing.T) {
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
defer gomodSetup(t)()
require.NoError(t, os.Mkdir("tmp", 0700))
require.NoError(t, os.Chdir("tmp"))
s, err := NewSession(stdout, stderr)
defer s.Clear()
require.NoError(t, err)
codes := []string{
`:i mod2`,
`mod2.Foo()`,
`mod2.Foo() + mod2.Foo()`,
`:clear`,
`:i mod2`,
`mod2.Foo()`,
`:i mod1`,
`3 * mod1.Value`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, "10\n20\n10\n30\n", stdout.String())
assert.Equal(t, ``, stderr.String())
}
func TestSessionEval_Gomod_Outside(t *testing.T) {
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
tempDir, _ := ioutil.TempDir("", "gore-")
defer chdir(tempDir)()
defer os.RemoveAll(tempDir)
s, err := NewSession(stdout, stderr)
defer s.Clear()
require.NoError(t, err)
codes := []string{
`:i github.com/motemen/gore`,
`gore.Session{}`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, ``, stderr.String())
}
func TestSessionEval_Gomod_CompleteImport(t *testing.T) {
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
defer gomodSetup(t)()
s, err := NewSession(stdout, stderr)
defer s.Clear()
require.NoError(t, err)
pre, cands, post := s.completeWord(":i ", 3)
assert.Equal(t, ":i ", pre)
assert.Subset(t, cands, []string{"mod2", "mod1"})
assert.Equal(t, post, "")
pre, cands, post = s.completeWord(":i mod2/", 8)
assert.Equal(t, ":i ", pre)
assert.Subset(t, cands, []string{"mod2/mod3", "mod2/mod4"})
assert.Equal(t, post, "")
}
func TestGetCurrentModule(t *testing.T) {
directives := listModuleDirectives()
pwd, _ := os.Getwd()
assert.Equal(t, []string{
"replace github.com/motemen/gore => " + strconv.Quote(pwd),
}, directives)
}