From 81893b60f22f560eacb1e6d110ccb6bca9dd863f Mon Sep 17 00:00:00 2001 From: Ashwin Jeyaseelan <8gitbrix@github.com> Date: Wed, 25 May 2022 01:08:20 +0000 Subject: [PATCH] Added tests --- pkg/cmd/codespace/edit_test.go | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 pkg/cmd/codespace/edit_test.go diff --git a/pkg/cmd/codespace/edit_test.go b/pkg/cmd/codespace/edit_test.go new file mode 100644 index 000000000..76bf7b439 --- /dev/null +++ b/pkg/cmd/codespace/edit_test.go @@ -0,0 +1,127 @@ +package codespace + +import ( + "context" + "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 + wantCodespace string + wantStdout string + wantStderr 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: "", + }, + wantStderr: "please pass in at least one valid argument to be edited", + wantErr: true, + }, + { + name: "select codespace to edit when no codespace input is given", + opts: editOptions{ + codespaceName: "", + displayName: "monalisa-new", + machine: "", + }, + codespaces: []*api.Codespace{ + { + Name: "monalisa-123", + DisplayName: "monalisa-old", + }, + { + Name: "hubot-robawt-abc", + DisplayName: "hubot", + }, + { + Name: "monalisa-spoonknife-c4f3", + DisplayName: "c4f3", + }, + }, + wantCodespace: "monalisa-123", + wantStdout: "", + wantErr: false, + }, + } + 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, stderr := iostreams.Test() + ios.SetStdinTTY(true) + ios.SetStdoutTTY(true) + a := NewApp(ios, nil, apiMock, nil) + + if err := a.Edit(context.Background(), opts); (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 out := stderr.String(); out != tt.wantStderr { + t.Errorf("stderr = %q, want %q", out, tt.wantStderr) + } + }) + } +}