-
Notifications
You must be signed in to change notification settings - Fork 148
/
session_gomod_test.go
191 lines (157 loc) · 3.96 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
package gore
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"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 gomodSetup(t *testing.T) {
tempDir := newTempDir(t)
mod1Dir := filepath.Join(tempDir, "mod1")
require.NoError(t, os.Mkdir(mod1Dir, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(mod1Dir, "go.mod"), []byte(`module mod1
`), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(mod1Dir, "mod1.go"), []byte(`package mod1
const Value = 10
`), 0o600))
mod2Dir := filepath.Join(tempDir, "mod2")
require.NoError(t, os.Mkdir(mod2Dir, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(mod2Dir, "go.mod"), []byte(fmt.Sprintf(`module mod2
replace mod1 => %s
require mod1 v0.0.0-00010101000000-000000000000
`, strconv.Quote(mod1Dir))), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(mod2Dir, "mod2.go"), []byte(`package mod2
import "mod1"
func Foo() int {
return mod1.Value
}
`), 0o600))
mod3Dir := filepath.Join(mod2Dir, "mod3")
require.NoError(t, os.Mkdir(mod3Dir, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(mod3Dir, "mod3.go"), []byte(`package mod3
func Bar() string {
return "mod3"
}
`), 0o600))
mod4Dir := filepath.Join(mod2Dir, "mod4")
require.NoError(t, os.Mkdir(mod4Dir, 0o700))
t.Cleanup(chdir(mod2Dir))
}
func TestSessionEval_Gomod(t *testing.T) {
var stdout, stderr strings.Builder
gomodSetup(t)
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { 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) {
var stdout, stderr strings.Builder
gomodSetup(t)
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { 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) {
var stdout, stderr strings.Builder
gomodSetup(t)
require.NoError(t, os.Mkdir("tmp", 0o700))
require.NoError(t, os.Chdir("tmp"))
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { 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) {
var stdout, stderr strings.Builder
_ = newTempDir(t)
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { s.Clear() })
require.NoError(t, err)
codes := []string{
`:i github.com/x-motemen/gore`,
`gore.Session{}`,
}
for _, code := range codes {
_ = s.Eval(code)
}
assert.Equal(t, ``, stderr.String())
}
func TestSessionEval_Gomod_CompleteImport(t *testing.T) {
var stdout, stderr strings.Builder
gomodSetup(t)
s, err := NewSession(&stdout, &stderr)
t.Cleanup(func() { 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, "")
}