X Tutup
Skip to content

Commit 71f3237

Browse files
committed
Fix tests
1 parent 707220a commit 71f3237

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

pkg/cmd/codespace/create.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
112112
return errors.New("there are no available machine types for this repository")
113113
}
114114

115-
a.StartProgressIndicatorWithLabel("Creating codespace")
116-
codespace, err := a.apiClient.CreateCodespace(ctx, &api.CreateCodespaceParams{
115+
createParams := &api.CreateCodespaceParams{
117116
RepositoryID: repository.ID,
118117
Branch: branch,
119118
Machine: machine,
120119
Location: locationResult.Location,
121120
IdleTimeoutMinutes: int(opts.idleTimeout.Minutes()),
122121
PermissionsOptOut: opts.permissionsOptOut,
123-
})
122+
}
123+
124+
a.StartProgressIndicatorWithLabel("Creating codespace")
125+
codespace, err := a.apiClient.CreateCodespace(ctx, createParams)
124126
a.StopProgressIndicator()
125127

126128
if err != nil {
@@ -129,27 +131,11 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
129131
return fmt.Errorf("error creating codespace: %w", err)
130132
}
131133

132-
if err := a.handleAdditionalPermissions(ctx, aerr.AllowPermissionsURL); err != nil {
134+
codespace, err = a.handleAdditionalPermissions(ctx, createParams, aerr.AllowPermissionsURL)
135+
if err != nil {
133136
// this error could be a cmdutil.SilentError (in the case that the user opened the browser) so we don't want to wrap it
134137
return err
135138
}
136-
137-
// if the user chose to create the codespace without the permissions,
138-
// we can continue with the create opting out of the additional permissions
139-
a.StartProgressIndicatorWithLabel("Creating codespace")
140-
codespace, err = a.apiClient.CreateCodespace(ctx, &api.CreateCodespaceParams{
141-
RepositoryID: repository.ID,
142-
Branch: branch,
143-
Machine: machine,
144-
Location: locationResult.Location,
145-
IdleTimeoutMinutes: int(opts.idleTimeout.Minutes()),
146-
PermissionsOptOut: true,
147-
})
148-
a.StopProgressIndicator()
149-
150-
if err != nil {
151-
return fmt.Errorf("error creating codespace: %w", err)
152-
}
153139
}
154140

155141
if opts.showStatus {
@@ -162,7 +148,7 @@ func (a *App) Create(ctx context.Context, opts createOptions) error {
162148
return nil
163149
}
164150

165-
func (a *App) handleAdditionalPermissions(ctx context.Context, allowPermissionsURL string) error {
151+
func (a *App) handleAdditionalPermissions(ctx context.Context, createParams *api.CreateCodespaceParams, allowPermissionsURL string) (*api.Codespace, error) {
166152
var (
167153
isInteractive = a.io.CanPrompt()
168154
cs = a.io.ColorScheme()
@@ -174,7 +160,7 @@ func (a *App) handleAdditionalPermissions(ctx context.Context, allowPermissionsU
174160
if !isInteractive {
175161
fmt.Fprintf(a.io.ErrOut, "%s to continue in your web browser to accept: %s\n", cs.Bold("Open this URL"), displayURL)
176162
fmt.Fprintf(a.io.ErrOut, "Alternatively, you can run %q with the %q option to create the codespace without these additional permissions.\n", a.io.ColorScheme().Bold("create"), cs.Bold("--default-permissions"))
177-
return nil
163+
return nil, cmdutil.SilentError
178164
}
179165

180166
choices := []string{
@@ -199,20 +185,32 @@ func (a *App) handleAdditionalPermissions(ctx context.Context, allowPermissionsU
199185
}
200186

201187
if err := ask(permsSurvey, &answers); err != nil {
202-
return fmt.Errorf("error getting answers: %w", err)
188+
return nil, fmt.Errorf("error getting answers: %w", err)
203189
}
204190

205191
// if the user chose to continue in the browser, open the URL
206192
if answers.Accept == choices[0] {
207193
if err := a.browser.Browse(allowPermissionsURL); err != nil {
208-
return fmt.Errorf("error opening browser: %w", err)
194+
return nil, fmt.Errorf("error opening browser: %w", err)
209195
}
210196
// browser opened successfully but we do not know if they accepted the permissions
211197
// so we must exit and wait for the user to attempt the create again
212-
return cmdutil.SilentError
198+
return nil, cmdutil.SilentError
213199
}
214200

215-
return nil
201+
// if the user chose to create the codespace without the permissions,
202+
// we can continue with the create opting out of the additional permissions
203+
createParams.PermissionsOptOut = true
204+
205+
a.StartProgressIndicatorWithLabel("Creating codespace")
206+
codespace, err := a.apiClient.CreateCodespace(ctx, createParams)
207+
a.StopProgressIndicator()
208+
209+
if err != nil {
210+
return nil, fmt.Errorf("error creating codespace: %w", err)
211+
}
212+
213+
return codespace, nil
216214
}
217215

218216
// showStatus polls the codespace for a list of post create states and their status. It will keep polling

pkg/cmd/codespace/create_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/cli/cli/v2/internal/codespaces/api"
10+
"github.com/cli/cli/v2/pkg/cmdutil"
1011
"github.com/cli/cli/v2/pkg/iostreams"
1112
)
1213

@@ -18,7 +19,7 @@ func TestApp_Create(t *testing.T) {
1819
name string
1920
fields fields
2021
opts createOptions
21-
wantErr bool
22+
wantErr error
2223
wantStdout string
2324
wantStderr string
2425
}{
@@ -114,9 +115,10 @@ func TestApp_Create(t *testing.T) {
114115
showStatus: false,
115116
idleTimeout: 30 * time.Minute,
116117
},
117-
wantStdout: `You must accept or deny additional permissions requested by the repository before you can create a codespace.
118+
wantErr: cmdutil.SilentError,
119+
wantStderr: `You must accept or deny additional permissions requested by the repository before you can create a codespace.
118120
Open this URL to continue in your web browser to accept: example.com/permissions
119-
Alternatively, you can run "create" with the "--skip-permissions" option to create the codespace without these additional permissions.
121+
Alternatively, you can run "create" with the "--default-permissions" option to create the codespace without these additional permissions.
120122
`,
121123
},
122124
}
@@ -127,7 +129,7 @@ Alternatively, you can run "create" with the "--skip-permissions" option to crea
127129
io: io,
128130
apiClient: tt.fields.apiClient,
129131
}
130-
if err := a.Create(context.Background(), tt.opts); (err != nil) != tt.wantErr {
132+
if err := a.Create(context.Background(), tt.opts); err != tt.wantErr {
131133
t.Errorf("App.Create() error = %v, wantErr %v", err, tt.wantErr)
132134
}
133135
if got := stdout.String(); got != tt.wantStdout {

0 commit comments

Comments
 (0)
X Tutup