1
1
package cli_test
2
2
3
3
import (
4
- "context"
5
4
"net/url"
5
+ "os"
6
+ "path/filepath"
7
+ "runtime"
8
+ "strings"
6
9
"testing"
7
10
8
11
"github.com/stretchr/testify/assert"
@@ -20,101 +23,137 @@ import (
20
23
func TestOpen (t * testing.T ) {
21
24
t .Parallel ()
22
25
23
- t .Run ("VS Code Local" , func (t * testing.T ) {
24
- t .Parallel ()
25
-
26
- client , workspace , agentToken := setupWorkspaceForAgent (t , func (agents []* proto.Agent ) []* proto.Agent {
27
- agents [0 ].Directory = "/tmp"
28
- agents [0 ].Name = "agent1"
29
- return agents
30
- })
31
-
32
- _ = agenttest .New (t , client .URL , agentToken )
33
- _ = coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
34
-
35
- inv , root := clitest .New (t , "open" , "vscode" , "--test.no-open" , workspace .Name )
36
- clitest .SetupConfig (t , client , root )
37
- pty := ptytest .New (t )
38
- inv .Stdin = pty .Input ()
39
- inv .Stdout = pty .Output ()
40
-
41
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
42
- defer cancel ()
43
-
44
- cmdDone := tGo (t , func () {
45
- err := inv .WithContext (ctx ).Run ()
46
- assert .NoError (t , err )
47
- })
48
-
49
- me , err := client .User (ctx , codersdk .Me )
50
- require .NoError (t , err )
51
-
52
- // --test.no-open forces the command to print the URI.
53
- line := pty .ReadLine (ctx )
54
- u , err := url .ParseRequestURI (line )
55
- require .NoError (t , err , "line: %q" , line )
56
-
57
- qp := u .Query ()
58
- assert .Equal (t , client .URL .String (), qp .Get ("url" ))
59
- assert .Equal (t , me .Username , qp .Get ("owner" ))
60
- assert .Equal (t , workspace .Name , qp .Get ("workspace" ))
61
- assert .Equal (t , "agent1" , qp .Get ("agent" ))
62
- assert .Contains (t , "tmp" , qp .Get ("folder" )) // Soft check for windows compat.
63
- assert .Equal (t , "" , qp .Get ("token" ))
64
-
65
- <- cmdDone
26
+ agentName := "agent1"
27
+ agentDir := "/tmp"
28
+ client , workspace , agentToken := setupWorkspaceForAgent (t , func (agents []* proto.Agent ) []* proto.Agent {
29
+ agents [0 ].Directory = agentDir
30
+ agents [0 ].Name = agentName
31
+ return agents
66
32
})
67
- t .Run ("VS Code Inside Workspace Prints URI" , func (t * testing.T ) {
68
- t .Parallel ()
69
-
70
- agentName := "agent1"
71
- client , workspace , agentToken := setupWorkspaceForAgent (t , func (agents []* proto.Agent ) []* proto.Agent {
72
- agents [0 ].Directory = "/tmp"
73
- agents [0 ].Name = agentName
74
- return agents
75
- })
76
33
77
- _ = agenttest .New (t , client .URL , agentToken )
78
- _ = coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
79
-
80
- t .Log (client .SessionToken ())
81
-
82
- inv , root := clitest .New (t , "open" , "vscode" , "--generate-token" , workspace .Name )
83
- clitest .SetupConfig (t , client , root )
84
-
85
- t .Log (root .Session ().Read ())
86
-
87
- pty := ptytest .New (t )
88
- inv .Stdin = pty .Input ()
89
- inv .Stdout = pty .Output ()
90
-
91
- inv .Environ .Set ("CODER" , "true" )
92
- inv .Environ .Set ("CODER_WORKSPACE_NAME" , workspace .Name )
93
- inv .Environ .Set ("CODER_WORKSPACE_AGENT_NAME" , agentName )
94
-
95
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
96
- defer cancel ()
97
-
98
- cmdDone := tGo (t , func () {
99
- err := inv .WithContext (ctx ).Run ()
100
- assert .NoError (t , err )
34
+ _ = agenttest .New (t , client .URL , agentToken )
35
+ _ = coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
36
+
37
+ insideWorkspaceEnv := map [string ]string {
38
+ "CODER" : "true" ,
39
+ "CODER_WORKSPACE_NAME" : workspace .Name ,
40
+ "CODER_WORKSPACE_AGENT_NAME" : agentName ,
41
+ }
42
+
43
+ wd , err := os .Getwd ()
44
+ require .NoError (t , err )
45
+
46
+ tests := []struct {
47
+ name string
48
+ args []string
49
+ env map [string ]string
50
+ wantDir string
51
+ wantToken bool
52
+ wantError bool
53
+ }{
54
+ {
55
+ name : "no args" ,
56
+ wantError : true ,
57
+ },
58
+ {
59
+ name : "nonexistent workspace" ,
60
+ args : []string {"--test.no-open" , workspace .Name + "bad" },
61
+ wantError : true ,
62
+ },
63
+ {
64
+ name : "ok" ,
65
+ args : []string {"--test.no-open" , workspace .Name },
66
+ wantDir : agentDir ,
67
+ },
68
+ {
69
+ name : "relative path error" ,
70
+ args : []string {"--test.no-open" , workspace .Name , "my/relative/path" },
71
+ wantError : true ,
72
+ },
73
+ {
74
+ name : "ok with abs path" ,
75
+ args : []string {"--test.no-open" , workspace .Name , agentDir },
76
+ wantDir : agentDir ,
77
+ },
78
+ {
79
+ name : "ok with token" ,
80
+ args : []string {"--test.no-open" , workspace .Name , "--generate-token" },
81
+ wantDir : agentDir ,
82
+ wantToken : true ,
83
+ },
84
+ // Inside workspace, does not require --test.no-open.
85
+ {
86
+ name : "ok inside workspace" ,
87
+ env : insideWorkspaceEnv ,
88
+ args : []string {workspace .Name },
89
+ wantDir : agentDir ,
90
+ },
91
+ {
92
+ name : "ok inside workspace relative path" ,
93
+ env : insideWorkspaceEnv ,
94
+ args : []string {workspace .Name , "foo" },
95
+ wantDir : filepath .Join (wd , "foo" ),
96
+ },
97
+ {
98
+ name : "ok inside workspace token" ,
99
+ env : insideWorkspaceEnv ,
100
+ args : []string {workspace .Name , "--generate-token" },
101
+ wantDir : agentDir ,
102
+ wantToken : true ,
103
+ },
104
+ }
105
+
106
+ for _ , tt := range tests {
107
+ tt := tt
108
+ t .Run (tt .name , func (t * testing.T ) {
109
+ t .Parallel ()
110
+
111
+ inv , root := clitest .New (t , append ([]string {"open" , "vscode" }, tt .args ... )... )
112
+ clitest .SetupConfig (t , client , root )
113
+ pty := ptytest .New (t )
114
+ inv .Stdin = pty .Input ()
115
+ inv .Stdout = pty .Output ()
116
+
117
+ ctx := testutil .Context (t , testutil .WaitLong )
118
+ inv = inv .WithContext (ctx )
119
+ for k , v := range tt .env {
120
+ inv .Environ .Set (k , v )
121
+ }
122
+
123
+ w := clitest .StartWithWaiter (t , inv )
124
+
125
+ if tt .wantError {
126
+ w .RequireError ()
127
+ return
128
+ }
129
+
130
+ me , err := client .User (ctx , codersdk .Me )
131
+ require .NoError (t , err )
132
+
133
+ line := pty .ReadLine (ctx )
134
+ u , err := url .ParseRequestURI (line )
135
+ require .NoError (t , err , "line: %q" , line )
136
+
137
+ qp := u .Query ()
138
+ assert .Equal (t , client .URL .String (), qp .Get ("url" ))
139
+ assert .Equal (t , me .Username , qp .Get ("owner" ))
140
+ assert .Equal (t , workspace .Name , qp .Get ("workspace" ))
141
+ assert .Equal (t , agentName , qp .Get ("agent" ))
142
+ if tt .wantDir != "" {
143
+ if runtime .GOOS == "windows" {
144
+ tt .wantDir = strings .TrimPrefix (tt .wantDir , "/" )
145
+ }
146
+ assert .Contains (t , qp .Get ("folder" ), tt .wantDir )
147
+ } else {
148
+ assert .Empty (t , qp .Get ("folder" ))
149
+ }
150
+ if tt .wantToken {
151
+ assert .NotEmpty (t , qp .Get ("token" ))
152
+ } else {
153
+ assert .Empty (t , qp .Get ("token" ))
154
+ }
155
+
156
+ w .RequireSuccess ()
101
157
})
102
-
103
- me , err := client .User (ctx , codersdk .Me )
104
- require .NoError (t , err )
105
-
106
- line := pty .ReadLine (ctx )
107
- u , err := url .ParseRequestURI (line )
108
- require .NoError (t , err , "line: %q" , line )
109
-
110
- qp := u .Query ()
111
- assert .Equal (t , client .URL .String (), qp .Get ("url" ))
112
- assert .Equal (t , me .Username , qp .Get ("owner" ))
113
- assert .Equal (t , workspace .Name , qp .Get ("workspace" ))
114
- assert .Equal (t , "agent1" , qp .Get ("agent" ))
115
- assert .Equal (t , "/tmp" , qp .Get ("folder" ))
116
- assert .NotEmpty (t , qp .Get ("token" ))
117
-
118
- <- cmdDone
119
- })
158
+ }
120
159
}
0 commit comments