forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_test.go
More file actions
128 lines (121 loc) · 3.32 KB
/
code_test.go
File metadata and controls
128 lines (121 loc) · 3.32 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
package codespace
import (
"context"
"testing"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/pkg/iostreams"
)
func TestApp_VSCode(t *testing.T) {
type args struct {
codespaceName string
useInsiders bool
useWeb bool
}
tests := []struct {
name string
args args
wantErr bool
wantURL string
}{
{
name: "open VS Code",
args: args{
codespaceName: "monalisa-cli-cli-abcdef",
useInsiders: false,
},
wantErr: false,
wantURL: "vscode://github.codespaces/connect?name=monalisa-cli-cli-abcdef",
},
{
name: "open VS Code Insiders",
args: args{
codespaceName: "monalisa-cli-cli-abcdef",
useInsiders: true,
},
wantErr: false,
wantURL: "vscode-insiders://github.codespaces/connect?name=monalisa-cli-cli-abcdef",
},
{
name: "open VS Code web",
args: args{
codespaceName: "monalisa-cli-cli-abcdef",
useInsiders: false,
useWeb: true,
},
wantErr: false,
wantURL: "https://monalisa-cli-cli-abcdef.github.dev",
},
{
name: "open VS Code web with Insiders",
args: args{
codespaceName: "monalisa-cli-cli-abcdef",
useInsiders: true,
useWeb: true,
},
wantErr: false,
wantURL: "https://monalisa-cli-cli-abcdef.github.dev?vscodeChannel=insiders",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &browser.Stub{}
ios, _, stdout, stderr := iostreams.Test()
a := &App{
browser: b,
apiClient: testCodeApiMock(),
io: ios,
}
if err := a.VSCode(context.Background(), tt.args.codespaceName, tt.args.useInsiders, tt.args.useWeb); (err != nil) != tt.wantErr {
t.Errorf("App.VSCode() error = %v, wantErr %v", err, tt.wantErr)
}
b.Verify(t, tt.wantURL)
if got := stdout.String(); got != "" {
t.Errorf("stdout = %q, want %q", got, "")
}
if got := stderr.String(); got != "" {
t.Errorf("stderr = %q, want %q", got, "")
}
})
}
}
func TestPendingOperationDisallowsCode(t *testing.T) {
app := testingCodeApp()
if err := app.VSCode(context.Background(), "disabledCodespace", false, false); err != nil {
if err.Error() != "codespace is disabled while it has a pending operation: Some pending operation" {
t.Errorf("expected pending operation error, but got: %v", err)
}
} else {
t.Error("expected pending operation error, but got nothing")
}
}
func testingCodeApp() *App {
ios, _, _, _ := iostreams.Test()
return NewApp(ios, nil, testCodeApiMock(), nil)
}
func testCodeApiMock() *apiClientMock {
user := &api.User{Login: "monalisa"}
testingCodespace := &api.Codespace{
Name: "monalisa-cli-cli-abcdef",
WebURL: "https://monalisa-cli-cli-abcdef.github.dev",
}
disabledCodespace := &api.Codespace{
Name: "disabledCodespace",
PendingOperation: true,
PendingOperationDisabledReason: "Some pending operation",
}
return &apiClientMock{
GetCodespaceFunc: func(_ context.Context, name string, _ bool) (*api.Codespace, error) {
if name == "disabledCodespace" {
return disabledCodespace, nil
}
return testingCodespace, nil
},
GetUserFunc: func(_ context.Context) (*api.User, error) {
return user, nil
},
AuthorizedKeysFunc: func(_ context.Context, _ string) ([]string, error) {
return []string{}, nil
},
}
}