cli/pkg/cmd/codespace/stop_test.go
Caleb Brose 57c73e8239
Add --repo filter to more gh codespaces commands (#6669)
* Add --repo flag to commands

* Example of using common args

* Add -r to stop

* Add validation to the add helper

* Skip prompt for single option

* Migrate (mostly) everything to addGetOrChooseCodespaceCommandArgs

* Fix typo in logsCmd

* Use R instead of r

* Update a couple -r usages

* Refactor to codespace_selector

* Clean up selector, apply it in a couple examples

* Use apiClient instead in constructor

* Restore addDeprecatedRepoShorthand

* Finish implementing the commands

* Update existing tests to use the selector

* Add tests for selector

* linter

* Catch case where there's no conflict

* Make the flag persistent for ports

* Add support to stop
2023-02-22 17:16:36 -06:00

105 lines
2.9 KiB
Go

package codespace
import (
"context"
"fmt"
"testing"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/stretchr/testify/assert"
)
func TestApp_StopCodespace(t *testing.T) {
type fields struct {
apiClient apiClient
}
tests := []struct {
name string
fields fields
opts *stopOptions
}{
{
name: "Stop a codespace I own",
opts: &stopOptions{
selector: &CodespaceSelector{codespaceName: "test-codespace"},
},
fields: fields{
apiClient: &apiClientMock{
GetCodespaceFunc: func(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error) {
if name != "test-codespace" {
return nil, fmt.Errorf("got codespace name %s, wanted %s", name, "test-codespace")
}
return &api.Codespace{
State: api.CodespaceStateAvailable,
}, nil
},
StopCodespaceFunc: func(ctx context.Context, name string, orgName string, userName string) error {
if name != "test-codespace" {
return fmt.Errorf("got codespace name %s, wanted %s", name, "test-codespace")
}
if orgName != "" {
return fmt.Errorf("got orgName %s, expected none", orgName)
}
return nil
},
},
},
},
{
name: "Stop a codespace as an org admin",
opts: &stopOptions{
selector: &CodespaceSelector{codespaceName: "test-codespace"},
orgName: "test-org",
userName: "test-user",
},
fields: fields{
apiClient: &apiClientMock{
GetOrgMemberCodespaceFunc: func(ctx context.Context, orgName string, userName string, codespaceName string) (*api.Codespace, error) {
if codespaceName != "test-codespace" {
return nil, fmt.Errorf("got codespace name %s, wanted %s", codespaceName, "test-codespace")
}
if orgName != "test-org" {
return nil, fmt.Errorf("got org name %s, wanted %s", orgName, "test-org")
}
if userName != "test-user" {
return nil, fmt.Errorf("got user name %s, wanted %s", userName, "test-user")
}
return &api.Codespace{
State: api.CodespaceStateAvailable,
}, nil
},
StopCodespaceFunc: func(ctx context.Context, codespaceName string, orgName string, userName string) error {
if codespaceName != "test-codespace" {
return fmt.Errorf("got codespace name %s, wanted %s", codespaceName, "test-codespace")
}
if orgName != "test-org" {
return fmt.Errorf("got org name %s, wanted %s", orgName, "test-org")
}
if userName != "test-user" {
return fmt.Errorf("got user name %s, wanted %s", userName, "test-user")
}
return nil
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, _, _ := iostreams.Test()
a := &App{
io: ios,
apiClient: tt.fields.apiClient,
}
err := a.StopCodespace(context.Background(), tt.opts)
assert.NoError(t, err)
})
}
}