[codespaces]: add hidden select command

This commit is contained in:
Oleg Solomka 2022-03-23 00:26:50 +00:00 committed by GitHub
parent 65d90aa24d
commit 783f316df1
3 changed files with 103 additions and 0 deletions

View file

@ -20,6 +20,7 @@ func NewRootCmd(app *App) *cobra.Command {
root.AddCommand(newSSHCmd(app))
root.AddCommand(newCpCmd(app))
root.AddCommand(newStopCmd(app))
root.AddCommand(newSelectCmd(app))
return root
}

View file

@ -0,0 +1,33 @@
package codespace
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
func newSelectCmd(app *App) *cobra.Command {
codeCmd := &cobra.Command{
Use: "select",
Short: "Select a codespace",
Hidden: true,
Args: noArgsConstraint,
RunE: func(cmd *cobra.Command, args []string) error {
return app.Select(cmd.Context(), "")
},
}
return codeCmd
}
func (a *App) Select(ctx context.Context, name string) error {
codespace, err := getOrChooseCodespace(ctx, a.apiClient, name)
if err != nil {
return err
}
a.io.Out.Write([]byte(fmt.Sprintf("%s\n", codespace.Name)));
return nil
}

View file

@ -0,0 +1,69 @@
package codespace
import (
"context"
"errors"
"fmt"
"testing"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/pkg/iostreams"
)
const CODESPACE_NAME = "monalisa-cli-cli-abcdef"
func TestApp_Select(t *testing.T) {
tests := []struct {
name string
arg string
wantErr bool
wantStdout string
wantStderr string
}{
{
name: "Select a codespace",
arg: CODESPACE_NAME,
wantErr: false,
wantStdout: fmt.Sprintf("%s\n", CODESPACE_NAME),
},
{
name: "Select a codespace error",
arg: "non-existent-codespace-name",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
io.SetStdinTTY(true)
io.SetStdoutTTY(true)
a := NewApp(io, nil, testSelectApiMock(), nil)
if err := a.Select(context.Background(), tt.arg); (err != nil) != tt.wantErr {
t.Errorf("App.Select() 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 := sortLines(stderr.String()); out != tt.wantStderr {
t.Errorf("stderr = %q, want %q", out, tt.wantStderr)
}
})
}
}
func testSelectApiMock() *apiClientMock {
testingCodespace := &api.Codespace{
Name: CODESPACE_NAME,
}
return &apiClientMock{
GetCodespaceFunc: func(_ context.Context, name string, includeConnection bool) (*api.Codespace, error) {
if name == CODESPACE_NAME {
return testingCodespace, nil
}
return nil, errors.New("Cannot find codespace.")
},
}
}