From 61553e5e8aa713f8344de45b0dc7fd3c75ff33ca Mon Sep 17 00:00:00 2001 From: Ashwin Jeyaseelan <8gitbrix@github.com> Date: Thu, 26 May 2022 17:01:30 +0000 Subject: [PATCH] Added nit, removed last test due to survey.ask call wanting a terminal --- pkg/cmd/codespace/edit.go | 16 ++--- pkg/cmd/codespace/edit_test.go | 106 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 pkg/cmd/codespace/edit_test.go diff --git a/pkg/cmd/codespace/edit.go b/pkg/cmd/codespace/edit.go index 5fa5b00e5..522a2263a 100644 --- a/pkg/cmd/codespace/edit.go +++ b/pkg/cmd/codespace/edit.go @@ -46,23 +46,19 @@ func (a *App) Edit(ctx context.Context, opts editOptions) error { } if userInputs.DisplayName == "" && userInputs.SKU == "" { - return fmt.Errorf("please pass in at least one valid argument to be edited") + return fmt.Errorf("at least one property has to be edited") } if userInputs.CodespaceName == "" { - a.StartProgressIndicatorWithLabel("Fetching codespaces") - codespaces, err := a.apiClient.ListCodespaces(ctx, -1) - a.StopProgressIndicator() - if err != nil { - return fmt.Errorf("error getting codespaces to select from: %w", err) - } - - selectedCodespace, err := chooseCodespaceFromList(ctx, codespaces) + selectedCodespace, err := chooseCodespace(ctx, a.apiClient) if err != nil { + if err == errNoCodespaces { + return err + } return fmt.Errorf("error choosing codespace: %w", err) } - opts.codespaceName = selectedCodespace.Name + userInputs.CodespaceName = selectedCodespace.Name } a.StartProgressIndicatorWithLabel("Editing codespace") diff --git a/pkg/cmd/codespace/edit_test.go b/pkg/cmd/codespace/edit_test.go new file mode 100644 index 000000000..eac08ff32 --- /dev/null +++ b/pkg/cmd/codespace/edit_test.go @@ -0,0 +1,106 @@ +package codespace + +import ( + "context" + "fmt" + "testing" + + "github.com/cli/cli/v2/internal/codespaces/api" + "github.com/cli/cli/v2/pkg/iostreams" +) + +func TestEdit(t *testing.T) { + + tests := []struct { + name string + opts editOptions + codespaces []*api.Codespace + mockCodespace *api.Codespace + editErr error + wantErr bool + wantStdout string + }{ + { + name: "edit codespace display name", + opts: editOptions{ + codespaceName: "hubot", + displayName: "hubot-changed", + machine: "", + }, + mockCodespace: &api.Codespace{ + Name: "hubot", + DisplayName: "hubot-changed", + }, + wantStdout: "", + wantErr: false, + }, + { + name: "edit codespace machine", + opts: editOptions{ + codespaceName: "hubot", + displayName: "", + machine: "machine", + }, + mockCodespace: &api.Codespace{ + Name: "hubot", + Machine: api.CodespaceMachine{ + Name: "machine", + }, + }, + wantStdout: "", + wantErr: false, + }, + { + name: "trying to edit a codespace without anything to edit should return an error", + opts: editOptions{ + codespaceName: "hubot", + displayName: "", + machine: "", + }, + editErr: fmt.Errorf("at least one property has to be edited"), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + apiMock := &apiClientMock{ + EditCodespaceFunc: func(_ context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error) { + if tt.editErr != nil { + return tt.mockCodespace, tt.editErr + } + return tt.mockCodespace, nil + }, + } + + if tt.opts.codespaceName == "" { + apiMock.ListCodespacesFunc = func(_ context.Context, num int) ([]*api.Codespace, error) { + return tt.codespaces, nil + } + } + + opts := tt.opts + + ios, _, stdout, _ := iostreams.Test() + ios.SetStdinTTY(true) + ios.SetStdoutTTY(true) + ios.SetStderrTTY(true) + a := NewApp(ios, nil, apiMock, nil) + + err := a.Edit(context.Background(), opts) + + if (err != nil) != tt.wantErr { + t.Errorf("App.Edit() error = %v, wantErr %v", err, tt.wantErr) + } + + if out := stdout.String(); out != tt.wantStdout { + t.Errorf("stdout = %q, want %q", out, tt.wantStdout) + } + + if tt.wantErr && err.Error() != tt.editErr.Error() { + t.Errorf("stderr = %v, expected error %v", err, tt.editErr) + } + + }) + } +}