-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuse_int_test.go
More file actions
261 lines (208 loc) · 7.04 KB
/
use_int_test.go
File metadata and controls
261 lines (208 loc) · 7.04 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package integration
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/testhelpers/e2e"
"github.com/ActiveState/cli/internal/testhelpers/suite"
"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
)
type UseIntegrationTestSuite struct {
tagsuite.Suite
}
func (suite *UseIntegrationTestSuite) TestUse() {
suite.OnlyRunForTags(tagsuite.Use)
ts := e2e.New(suite.T(), false)
defer ts.Close()
// Checkout.
cp := ts.Spawn("checkout", "ActiveState-CLI/Python3")
cp.Expect("Checked out project", e2e.RuntimeSourcingTimeoutOpt)
cp.ExpectExitCode(0)
// Use.
cp = ts.Spawn("use", "ActiveState-CLI/Python3")
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
// Verify runtime works.
pythonExe := filepath.Join(ts.Dirs.DefaultBin, "python3"+osutils.ExeExtension)
cp = ts.SpawnCmd(pythonExe, "--version")
cp.Expect("Python 3")
cp.ExpectExitCode(0)
// Checkout another project.
cp = ts.Spawn("checkout", "ActiveState-CLI/Python-3.9")
cp.Expect("Checked out project", e2e.RuntimeSourcingTimeoutOpt)
cp.ExpectExitCode(0)
// Use it.
cp = ts.Spawn("use", "ActiveState-CLI/Python-3.9")
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
// Verify the new runtime works.
cp = ts.SpawnCmdWithOpts(pythonExe, e2e.OptArgs("--version"))
cp.Expect("Python 3")
cp.ExpectExitCode(0)
// Switch back using just the project name.
cp = ts.Spawn("use", "Python3")
cp.Expect("Switched to project", e2e.RuntimeSourcingTimeoutOpt)
cp.ExpectExitCode(0)
// Verify the first runtime is set up correctly and usable.
cp = ts.SpawnCmdWithOpts(pythonExe, e2e.OptArgs("--version"))
cp.Expect("Python 3")
cp.ExpectExitCode(0)
// Test failure switching to project name that was not checked out.
cp = ts.Spawn("use", "NotCheckedOut")
cp.Expect("Cannot find the NotCheckedOut project.")
cp.ExpectExitCode(1)
ts.IgnoreLogErrors()
}
func (suite *UseIntegrationTestSuite) TestUseCwd() {
suite.OnlyRunForTags(tagsuite.Use)
ts := e2e.New(suite.T(), false)
defer ts.Close()
projDir := filepath.Join(ts.Dirs.Work, "MyEmpty")
cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", "ActiveState-CLI/Empty", projDir))
cp.Expect("Checked out project")
cp.ExpectExitCode(0)
cp = ts.SpawnWithOpts(
e2e.OptArgs("use"),
e2e.OptWD(projDir),
)
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
emptyDir := filepath.Join(ts.Dirs.Work, "EmptyDir")
suite.Require().NoError(fileutils.Mkdir(emptyDir))
cp = ts.SpawnWithOpts(
e2e.OptArgs("use"),
e2e.OptWD(emptyDir),
)
cp.Expect("Unable to use project")
cp.ExpectExitCode(1)
ts.IgnoreLogErrors()
}
func (suite *UseIntegrationTestSuite) TestReset() {
suite.OnlyRunForTags(tagsuite.Use)
ts := e2e.New(suite.T(), false)
defer ts.Close()
ts.SetupRCFile()
cp := ts.Spawn("checkout", "ActiveState-CLI/Python3")
cp.Expect("Checked out project", e2e.RuntimeSourcingTimeoutOpt)
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "ActiveState-CLI/Python3")
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
python3Exe := filepath.Join(ts.Dirs.DefaultBin, "python3"+osutils.ExeExtension)
suite.True(fileutils.TargetExists(python3Exe), python3Exe+" not found")
cfg, err := config.New()
suite.NoError(err)
rcfile, err := subshell.New(cfg).RcFile()
suite.NoError(err)
if runtime.GOOS != "windows" {
fileutils.FileExists(rcfile)
suite.Contains(string(fileutils.ReadFileUnsafe(rcfile)), ts.Dirs.DefaultBin, "PATH does not have your project in it")
}
cp = ts.Spawn("use", "reset")
cp.Expect("Continue?")
cp.SendLine("n")
cp.Expect("Reset aborted by user")
cp.ExpectExitCode(1)
ts.IgnoreLogErrors()
cp = ts.Spawn("use", "reset", "--non-interactive")
cp.Expect("Stopped using your project runtime")
cp.Expect("Note you may need to")
cp.ExpectExitCode(0)
suite.False(fileutils.TargetExists(python3Exe), python3Exe+" still exists")
cp = ts.Spawn("use", "reset")
cp.Expect("No project to stop using")
cp.ExpectExitCode(1)
if runtime.GOOS != "windows" && fileutils.FileExists(rcfile) {
suite.NotContains(string(fileutils.ReadFileUnsafe(rcfile)), ts.Dirs.DefaultBin, "PATH still has your project in it")
}
}
func (suite *UseIntegrationTestSuite) TestShow() {
suite.OnlyRunForTags(tagsuite.Use)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.Spawn("use", "show")
cp.Expect("No project is being used")
cp.ExpectExitCode(1)
ts.IgnoreLogErrors()
cp = ts.Spawn("checkout", "ActiveState-CLI/Empty")
cp.Expect("Checked out project")
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "ActiveState-CLI/Empty")
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "show")
cp.Expect("The active project is ActiveState-CLI/Empty")
projectDir := filepath.Join(ts.Dirs.Work, "Empty")
if runtime.GOOS != "windows" {
cp.Expect(projectDir)
} else {
// Windows uses the long path here.
longPath, err := fileutils.GetLongPathName(projectDir)
suite.Require().NoError(err)
cp.Expect(longPath)
}
cp.Expect(ts.Dirs.Cache)
cp.Expect("exec")
cp.ExpectExitCode(0)
err := os.RemoveAll(projectDir)
suite.Require().NoError(err)
cp = ts.Spawn("use", "show")
cp.Expect("Cannot find your project")
// Both Windows and MacOS can run into path comparison issues with symlinks and long paths.
if runtime.GOOS == "linux" {
cp.Expect(fmt.Sprintf("Could not find project at %s", projectDir))
}
cp.ExpectExitCode(1)
cp = ts.Spawn("use", "reset", "--non-interactive")
cp.Expect("Stopped using your project runtime")
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "show")
cp.Expect("No project is being used")
cp.ExpectExitCode(1)
}
func (suite *UseIntegrationTestSuite) TestSetupNotice() {
suite.OnlyRunForTags(tagsuite.Use)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.Spawn("checkout", "ActiveState-CLI/Empty")
cp.Expect(locale.T("install_runtime"))
cp.Expect("Checked out project")
cp.ExpectExitCode(0)
suite.Require().NoError(os.RemoveAll(filepath.Join(ts.Dirs.Work, "Empty"))) // runtime marker still exists
cp = ts.Spawn("checkout", "ActiveState-CLI/Empty#265f9914-ad4d-4e0a-a128-9d4e8c5db820")
cp.Expect("Checked out project")
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "Empty")
cp.Expect("Switched to project")
cp.ExpectExitCode(0)
}
func (suite *UseIntegrationTestSuite) TestJSON() {
suite.OnlyRunForTags(tagsuite.Use, tagsuite.JSON)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.Spawn("checkout", "ActiveState-CLI/Empty", ".")
cp.Expect("Checked out")
cp.ExpectExitCode(0)
cp = ts.Spawn("use", "-o", "json")
cp.Expect(`"namespace":`)
cp.Expect(`"path":`)
cp.Expect(`"executables":`)
cp.ExpectExitCode(0)
AssertValidJSON(suite.T(), cp)
cp = ts.Spawn("use", "show", "--output", "json")
cp.Expect(`"namespace":`)
cp.Expect(`"path":`)
cp.Expect(`"executables":`)
cp.ExpectExitCode(0)
AssertValidJSON(suite.T(), cp)
}
func TestUseIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(UseIntegrationTestSuite))
}