4
4
"net/url"
5
5
"os"
6
6
"path/filepath"
7
- "runtime"
8
- "strings"
9
7
"testing"
10
8
11
9
"github.com/stretchr/testify/assert"
@@ -20,11 +18,12 @@ import (
20
18
"github.com/coder/coder/v2/testutil"
21
19
)
22
20
23
- func TestOpen (t * testing.T ) {
21
+ func TestOpenVSCode (t * testing.T ) {
24
22
t .Parallel ()
25
23
26
24
agentName := "agent1"
27
- agentDir := "/tmp"
25
+ agentDir , err := filepath .Abs (filepath .FromSlash ("/tmp" ))
26
+ require .NoError (t , err )
28
27
client , workspace , agentToken := setupWorkspaceForAgent (t , func (agents []* proto.Agent ) []* proto.Agent {
29
28
agents [0 ].Directory = agentDir
30
29
agents [0 ].Name = agentName
@@ -66,12 +65,13 @@ func TestOpen(t *testing.T) {
66
65
wantDir : agentDir ,
67
66
},
68
67
{
69
- name : "relative path error " ,
68
+ name : "ok relative path" ,
70
69
args : []string {"--test.open-error" , workspace .Name , "my/relative/path" },
71
- wantError : true ,
70
+ wantDir : filepath .Join (agentDir , filepath .FromSlash ("my/relative/path" )),
71
+ wantError : false ,
72
72
},
73
73
{
74
- name : "ok with abs path" ,
74
+ name : "ok with absolute path" ,
75
75
args : []string {"--test.open-error" , workspace .Name , agentDir },
76
76
wantDir : agentDir ,
77
77
},
@@ -140,9 +140,127 @@ func TestOpen(t *testing.T) {
140
140
assert .Equal (t , workspace .Name , qp .Get ("workspace" ))
141
141
assert .Equal (t , agentName , qp .Get ("agent" ))
142
142
if tt .wantDir != "" {
143
- if runtime .GOOS == "windows" {
144
- tt .wantDir = strings .TrimPrefix (tt .wantDir , "/" )
145
- }
143
+ assert .Contains (t , qp .Get ("folder" ), tt .wantDir )
144
+ } else {
145
+ assert .Empty (t , qp .Get ("folder" ))
146
+ }
147
+ if tt .wantToken {
148
+ assert .NotEmpty (t , qp .Get ("token" ))
149
+ } else {
150
+ assert .Empty (t , qp .Get ("token" ))
151
+ }
152
+
153
+ w .RequireSuccess ()
154
+ })
155
+ }
156
+ }
157
+
158
+ func TestOpenVSCode_NoAgentDirectory (t * testing.T ) {
159
+ t .Parallel ()
160
+
161
+ agentName := "agent1"
162
+ client , workspace , agentToken := setupWorkspaceForAgent (t , func (agents []* proto.Agent ) []* proto.Agent {
163
+ agents [0 ].Name = agentName
164
+ return agents
165
+ })
166
+
167
+ _ = agenttest .New (t , client .URL , agentToken )
168
+ _ = coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
169
+
170
+ insideWorkspaceEnv := map [string ]string {
171
+ "CODER" : "true" ,
172
+ "CODER_WORKSPACE_NAME" : workspace .Name ,
173
+ "CODER_WORKSPACE_AGENT_NAME" : agentName ,
174
+ }
175
+
176
+ wd , err := os .Getwd ()
177
+ require .NoError (t , err )
178
+
179
+ tests := []struct {
180
+ name string
181
+ args []string
182
+ env map [string ]string
183
+ wantDir string
184
+ wantToken bool
185
+ wantError bool
186
+ }{
187
+ {
188
+ name : "ok" ,
189
+ args : []string {"--test.open-error" , workspace .Name },
190
+ },
191
+ {
192
+ name : "no agent dir error relative path" ,
193
+ args : []string {"--test.open-error" , workspace .Name , "my/relative/path" },
194
+ wantDir : filepath .FromSlash ("my/relative/path" ),
195
+ wantError : true ,
196
+ },
197
+ {
198
+ name : "ok with absolute path" ,
199
+ args : []string {"--test.open-error" , workspace .Name , "/home/coder" },
200
+ wantDir : "/home/coder" ,
201
+ },
202
+ {
203
+ name : "ok with token" ,
204
+ args : []string {"--test.open-error" , workspace .Name , "--generate-token" },
205
+ wantToken : true ,
206
+ },
207
+ // Inside workspace, does not require --test.open-error.
208
+ {
209
+ name : "ok inside workspace" ,
210
+ env : insideWorkspaceEnv ,
211
+ args : []string {workspace .Name },
212
+ },
213
+ {
214
+ name : "ok inside workspace relative path" ,
215
+ env : insideWorkspaceEnv ,
216
+ args : []string {workspace .Name , "foo" },
217
+ wantDir : filepath .Join (wd , "foo" ),
218
+ },
219
+ {
220
+ name : "ok inside workspace token" ,
221
+ env : insideWorkspaceEnv ,
222
+ args : []string {workspace .Name , "--generate-token" },
223
+ wantToken : true ,
224
+ },
225
+ }
226
+
227
+ for _ , tt := range tests {
228
+ tt := tt
229
+ t .Run (tt .name , func (t * testing.T ) {
230
+ t .Parallel ()
231
+
232
+ inv , root := clitest .New (t , append ([]string {"open" , "vscode" }, tt .args ... )... )
233
+ clitest .SetupConfig (t , client , root )
234
+ pty := ptytest .New (t )
235
+ inv .Stdin = pty .Input ()
236
+ inv .Stdout = pty .Output ()
237
+
238
+ ctx := testutil .Context (t , testutil .WaitLong )
239
+ inv = inv .WithContext (ctx )
240
+ for k , v := range tt .env {
241
+ inv .Environ .Set (k , v )
242
+ }
243
+
244
+ w := clitest .StartWithWaiter (t , inv )
245
+
246
+ if tt .wantError {
247
+ w .RequireError ()
248
+ return
249
+ }
250
+
251
+ me , err := client .User (ctx , codersdk .Me )
252
+ require .NoError (t , err )
253
+
254
+ line := pty .ReadLine (ctx )
255
+ u , err := url .ParseRequestURI (line )
256
+ require .NoError (t , err , "line: %q" , line )
257
+
258
+ qp := u .Query ()
259
+ assert .Equal (t , client .URL .String (), qp .Get ("url" ))
260
+ assert .Equal (t , me .Username , qp .Get ("owner" ))
261
+ assert .Equal (t , workspace .Name , qp .Get ("workspace" ))
262
+ assert .Equal (t , agentName , qp .Get ("agent" ))
263
+ if tt .wantDir != "" {
146
264
assert .Contains (t , qp .Get ("folder" ), tt .wantDir )
147
265
} else {
148
266
assert .Empty (t , qp .Get ("folder" ))
0 commit comments