Skip to content

Commit 734aaab

Browse files
committed
add test for list workspaces
1 parent 67c748d commit 734aaab

File tree

10 files changed

+276
-30
lines changed

10 files changed

+276
-30
lines changed

.mockery.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ require-template-schema-exists: true
1010
template: testify
1111
template-schema: '{{.Template}}.schema.json'
1212
packages:
13-
github.com/codesphere-cloud/cs-go/pkg/cs:
13+
github.com/codesphere-cloud/cs-go/cmd:
1414
interfaces:
1515
Client:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ format:
99
lint:
1010
golangci-lint run
1111

12-
test: update-mocks
12+
test:
1313
go test ./...
1414

1515
generate:

cmd/client.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
//go:generate mockery
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"net/url"
9+
10+
"github.com/codesphere-cloud/cs-go/api"
11+
"github.com/codesphere-cloud/cs-go/pkg/cs"
12+
)
13+
14+
type Client interface {
15+
ListTeams() ([]api.Team, error)
16+
ListWorkspaces(teamId int) ([]api.Workspace, error)
17+
}
18+
19+
func NewClient(opts GlobalOptions) (Client, error) {
20+
token, err := cs.GetApiToken()
21+
if err != nil {
22+
return nil, fmt.Errorf("failed to get API token: %e", err)
23+
}
24+
apiUrl, err := url.Parse(opts.GetApiUrl())
25+
if err != nil {
26+
return nil, fmt.Errorf("failed to parse URL '%s': %e", opts.GetApiUrl(), err)
27+
}
28+
client := api.NewClient(context.Background(), api.Configuration{
29+
BaseUrl: apiUrl,
30+
Token: token,
31+
})
32+
return client, nil
33+
}

cmd/list-teams.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212

1313
type ListTeamsCmd struct {
1414
cmd *cobra.Command
15-
opts cs.GlobalOptions
15+
opts GlobalOptions
1616
}
1717

18-
func addListTeamsCmd(p *cobra.Command, opts cs.GlobalOptions) {
18+
func addListTeamsCmd(p *cobra.Command, opts GlobalOptions) {
1919
l := ListTeamsCmd{
2020
cmd: &cobra.Command{
2121
Use: "teams",
@@ -34,7 +34,7 @@ $ cs list teams
3434
}
3535

3636
func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
37-
client, err := cs.NewClient(l.opts)
37+
client, err := NewClient(l.opts)
3838
if err != nil {
3939
return fmt.Errorf("failed to create Codesphere client: %e", err)
4040
}

cmd/list-workspaces.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import (
44
"fmt"
55

66
"github.com/codesphere-cloud/cs-go/api"
7-
"github.com/codesphere-cloud/cs-go/pkg/cs"
87
"github.com/codesphere-cloud/cs-go/pkg/out"
98
"github.com/jedib0t/go-pretty/v6/table"
109

1110
"github.com/spf13/cobra"
1211
)
1312

1413
type ListWorkspacesCmd struct {
14+
Opts ListWorkspacesOptions
1515
cmd *cobra.Command
16-
opts ListWorkspacesOptions
1716
}
1817

1918
type ListWorkspacesOptions struct {
20-
cs.GlobalOptions
19+
GlobalOptions
2120
TeamId *int
2221
}
2322

24-
func addListWorkspacesCmd(p *cobra.Command, opts cs.GlobalOptions) {
23+
func addListWorkspacesCmd(p *cobra.Command, opts GlobalOptions) {
2524
l := ListWorkspacesCmd{
2625
cmd: &cobra.Command{
2726
Use: "workspaces",
@@ -33,34 +32,26 @@ List all workspaces:
3332
$ cs list workspaces --team-id <team-id>
3433
`,
3534
},
36-
opts: ListWorkspacesOptions{GlobalOptions: opts},
35+
Opts: ListWorkspacesOptions{GlobalOptions: opts},
3736
}
3837
l.cmd.RunE = l.RunE
3938
l.parseLogCmdFlags()
4039
p.AddCommand(l.cmd)
4140
}
4241

4342
func (l *ListWorkspacesCmd) parseLogCmdFlags() {
44-
l.opts.TeamId = l.cmd.Flags().IntP("team-id", "t", -1, "ID of team to query")
43+
l.Opts.TeamId = l.cmd.Flags().IntP("team-id", "t", -1, "ID of team to query")
4544
}
4645

4746
func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
48-
client, err := cs.NewClient(l.opts.GlobalOptions)
47+
client, err := NewClient(l.Opts.GlobalOptions)
4948
if err != nil {
5049
return fmt.Errorf("failed to create Codesphere client: %e", err)
5150
}
5251

53-
teams, err := l.getTeamIds(client)
52+
workspaces, err := l.ListWorkspaces(client)
5453
if err != nil {
55-
return fmt.Errorf("failed to get teams: %e", err)
56-
}
57-
workspaces := []api.Workspace{}
58-
for _, team := range teams {
59-
teamWorkspaces, err := client.ListWorkspaces(team)
60-
if err != nil {
61-
return fmt.Errorf("failed to list workspaces: %e", err)
62-
}
63-
workspaces = append(workspaces, teamWorkspaces...)
54+
return fmt.Errorf("failed to list workspaces: %e", err)
6455
}
6556

6657
t := out.GetTableWriter()
@@ -77,9 +68,25 @@ func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
7768
return nil
7869
}
7970

80-
func (l *ListWorkspacesCmd) getTeamIds(client cs.Client) (teams []int, err error) {
81-
if l.opts.TeamId != nil && *l.opts.TeamId >= 0 {
82-
teams = append(teams, *l.opts.TeamId)
71+
func (l *ListWorkspacesCmd) ListWorkspaces(client Client) ([]api.Workspace, error) {
72+
teams, err := l.getTeamIds(client)
73+
if err != nil {
74+
return nil, fmt.Errorf("failed to get teams: %e", err)
75+
}
76+
workspaces := []api.Workspace{}
77+
for _, team := range teams {
78+
teamWorkspaces, err := client.ListWorkspaces(team)
79+
if err != nil {
80+
return nil, fmt.Errorf("failed to list workspaces: %e", err)
81+
}
82+
workspaces = append(workspaces, teamWorkspaces...)
83+
}
84+
return workspaces, nil
85+
}
86+
87+
func (l *ListWorkspacesCmd) getTeamIds(client Client) (teams []int, err error) {
88+
if l.Opts.TeamId != nil && *l.Opts.TeamId >= 0 {
89+
teams = append(teams, *l.Opts.TeamId)
8390
return
8491
}
8592
var allTeams []api.Team

cmd/list-workspaces_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/codesphere-cloud/cs-go/api"
7+
"github.com/codesphere-cloud/cs-go/cmd"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestListWorkspaces(t *testing.T) {
12+
l := newListWorkspacesCmdWithTeam(0)
13+
client := NewMockClient(t)
14+
client.EXPECT().ListWorkspaces(0).Return([]api.Workspace{}, nil)
15+
16+
w, err := l.ListWorkspaces(client)
17+
assert.Equal(t, w, []api.Workspace{}, "should return empty list of workspaces")
18+
assert.Nil(t, err, "should be nil")
19+
}
20+
21+
func TestListWorkspacesAllTeams(t *testing.T) {
22+
l := newListWorkspacesCmd()
23+
client := NewMockClient(t)
24+
client.EXPECT().ListTeams().Return([]api.Team{{Id: 0}, {Id: 1}}, nil)
25+
26+
expectedWorkspaces := []api.Workspace{
27+
{Id: 0, Name: "fakeForTeam0"},
28+
{Id: 1, Name: "fakeForTeam1"},
29+
}
30+
client.EXPECT().ListWorkspaces(0).Return([]api.Workspace{expectedWorkspaces[0]}, nil)
31+
client.EXPECT().ListWorkspaces(1).Return([]api.Workspace{expectedWorkspaces[1]}, nil)
32+
33+
w, err := l.ListWorkspaces(client)
34+
assert.Equal(t, w, expectedWorkspaces, "should return both workspaces")
35+
assert.Nil(t, err, "should be nil")
36+
}
37+
38+
func newListWorkspacesCmdWithTeam(teamId int) cmd.ListWorkspacesCmd {
39+
return cmd.ListWorkspacesCmd{
40+
Opts: cmd.ListWorkspacesOptions{
41+
TeamId: &teamId,
42+
},
43+
}
44+
}
45+
46+
func newListWorkspacesCmd() cmd.ListWorkspacesCmd {
47+
return cmd.ListWorkspacesCmd{}
48+
}

cmd/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package cmd
22

33
import (
4-
"github.com/codesphere-cloud/cs-go/pkg/cs"
54
"github.com/spf13/cobra"
65
)
76

87
type ListCmd struct {
98
cmd *cobra.Command
109
}
1110

12-
func addListCmd(rootCmd *cobra.Command, opts cs.GlobalOptions) {
11+
func addListCmd(rootCmd *cobra.Command, opts GlobalOptions) {
1312
l := ListCmd{
1413
cmd: &cobra.Command{
1514
Use: "list",

cmd/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
type LogCmd struct {
2222
cmd *cobra.Command
2323
scope LogCmdScope
24-
opts cs.GlobalOptions
24+
opts GlobalOptions
2525
}
2626

2727
type LogCmdScope struct {
@@ -49,7 +49,7 @@ type SSE struct {
4949
data string
5050
}
5151

52-
func addLogCmd(rootCmd *cobra.Command, opts cs.GlobalOptions) {
52+
func addLogCmd(rootCmd *cobra.Command, opts GlobalOptions) {
5353
logCmd := LogCmd{
5454
cmd: &cobra.Command{
5555
Use: "log",

0 commit comments

Comments
 (0)