Skip to content

Commit 4bf611a

Browse files
committed
refac(io) set IO once in main to allow overriding with in memory io in tests
`print.Printer` had a reference to a `cobra.Command` for using its IO streams. Each Command also used a Printer, resulting in an awkward circular dependency. Refactored Printer to use IO streams directly. When using the application these are set in `main`, when used in tests these can be set to `bytes.Buffer`s. Also replaced usages of `os.Args` with just a string slice. Also set in `main`. `cobra.Command`s `Args` and IO-streams are set in `NewRootCmd` with `traverseCommands`. `CmdParams` also has an `fs.FS`, currently unused but will allow using the real FS during regular use and an in-memory-FS during tests. This change prepares the application for integrative testing while keeping good isolation. Generally speaking the goal is to move all things with side effects into main (compare with https://grafana.com/blog/how-i-write-http-services-in-go-after-13-years/#func-main-only-calls-run)
1 parent 83cf194 commit 4bf611a

File tree

380 files changed

+1659
-2413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+1659
-2413
lines changed

internal/cmd/affinity-groups/create/create_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/google/go-cmp/cmp"
108
"github.com/google/go-cmp/cmp/cmpopts"
119
"github.com/google/uuid"
1210
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1311

12+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
13+
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1615
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1817
)
@@ -181,11 +180,10 @@ func TestOutputResult(t *testing.T) {
181180
isValid: true,
182181
},
183182
}
184-
p := print.NewPrinter()
185-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
183+
params := testparams.NewTestParams()
186184
for _, tt := range tests {
187185
t.Run(tt.description, func(t *testing.T) {
188-
err := outputResult(p, tt.model, tt.response)
186+
err := outputResult(params.Printer, tt.model, tt.response)
189187
if err != nil {
190188
if !tt.isValid {
191189
return

internal/cmd/affinity-groups/delete/delete_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/google/go-cmp/cmp"
108
"github.com/google/go-cmp/cmp/cmpopts"
119
"github.com/google/uuid"
1210
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1311

12+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
13+
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1615
)
1716

1817
const (
@@ -103,8 +102,8 @@ func TestParseInput(t *testing.T) {
103102
}
104103
for _, tt := range tests {
105104
t.Run(tt.description, func(t *testing.T) {
106-
p := print.NewPrinter()
107-
cmd := NewCmd(&types.CmdParams{Printer: p})
105+
params := testparams.NewTestParams()
106+
cmd := NewCmd(params.CmdParams)
108107
err := globalflags.Configure(cmd.Flags())
109108
if err != nil {
110109
t.Fatalf("configure global flags: %v", err)
@@ -136,7 +135,7 @@ func TestParseInput(t *testing.T) {
136135
t.Fatalf("error validating flags: %v", err)
137136
}
138137

139-
model, err := parseInput(p, cmd, tt.argValues)
138+
model, err := parseInput(params.Printer, cmd, tt.argValues)
140139
if err != nil {
141140
if !tt.isValid {
142141
return

internal/cmd/affinity-groups/describe/describe_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/google/go-cmp/cmp"
108
"github.com/google/go-cmp/cmp/cmpopts"
119
"github.com/google/uuid"
1210
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1311

12+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
13+
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1615
)
1716

1817
const (
@@ -104,8 +103,8 @@ func TestParseInput(t *testing.T) {
104103
}
105104
for _, tt := range tests {
106105
t.Run(tt.description, func(t *testing.T) {
107-
p := print.NewPrinter()
108-
cmd := NewCmd(&types.CmdParams{Printer: p})
106+
params := testparams.NewTestParams()
107+
cmd := NewCmd(params.CmdParams)
109108
err := globalflags.Configure(cmd.Flags())
110109
if err != nil {
111110
t.Fatalf("configure global flags: %v", err)
@@ -137,7 +136,7 @@ func TestParseInput(t *testing.T) {
137136
t.Fatalf("error validating flags: %v", err)
138137
}
139138

140-
model, err := parseInput(p, cmd, tt.argValues)
139+
model, err := parseInput(params.Printer, cmd, tt.argValues)
141140
if err != nil {
142141
if !tt.isValid {
143142
return
@@ -197,12 +196,11 @@ func TestOutputResult(t *testing.T) {
197196
response: iaas.AffinityGroup{},
198197
},
199198
}
200-
p := print.NewPrinter()
201-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
199+
params := testparams.NewTestParams()
202200

203201
for _, tt := range tests {
204202
t.Run(tt.description, func(t *testing.T) {
205-
err := outputResult(p, tt.model, tt.response)
203+
err := outputResult(params.Printer, tt.model, tt.response)
206204
if err != nil {
207205
if !tt.isValid {
208206
return

internal/cmd/affinity-groups/list/list_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"strconv"
66
"testing"
77

8-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
9-
108
"github.com/google/go-cmp/cmp"
119
"github.com/google/go-cmp/cmp/cmpopts"
1210
"github.com/google/uuid"
1311
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1412

13+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
14+
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
16-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1817
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1918
)
@@ -155,11 +154,10 @@ func TestOutputResult(t *testing.T) {
155154
isValid: true,
156155
},
157156
}
158-
p := print.NewPrinter()
159-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
157+
params := testparams.NewTestParams()
160158
for _, tt := range tests {
161159
t.Run(tt.description, func(t *testing.T) {
162-
err := outputResult(p, tt.model, tt.response)
160+
err := outputResult(params.Printer, tt.model, tt.response)
163161
if err != nil {
164162
if !tt.isValid {
165163
return

internal/cmd/beta/alb/create/create_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"log"
88
"testing"
99

10-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
11-
1210
"github.com/google/go-cmp/cmp"
1311
"github.com/google/go-cmp/cmp/cmpopts"
1412
"github.com/google/uuid"
1513
"github.com/stackitcloud/stackit-sdk-go/services/alb"
1614

15+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
16+
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
18-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1918
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
2019
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2120
)
@@ -213,11 +212,10 @@ func TestOutputResult(t *testing.T) {
213212
wantErr: false,
214213
},
215214
}
216-
p := print.NewPrinter()
217-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
215+
params := testparams.NewTestParams()
218216
for _, tt := range tests {
219217
t.Run(tt.name, func(t *testing.T) {
220-
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
218+
if err := outputResult(params.Printer, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
221219
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
222220
}
223221
})

internal/cmd/beta/alb/describe/describe_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1210

1311
"github.com/google/go-cmp/cmp"
@@ -168,11 +166,10 @@ func Test_outputResult(t *testing.T) {
168166
wantErr: false,
169167
},
170168
}
171-
p := print.NewPrinter()
172-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
169+
params := testparams.NewTestParams()
173170
for _, tt := range tests {
174171
t.Run(tt.name, func(t *testing.T) {
175-
if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr {
172+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr {
176173
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
177174
}
178175
})

internal/cmd/beta/alb/list/list_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"strconv"
66
"testing"
77

8-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
9-
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1110

1211
"github.com/google/go-cmp/cmp/cmpopts"
@@ -168,11 +167,11 @@ func Test_outputResult(t *testing.T) {
168167
wantErr: false,
169168
},
170169
}
171-
p := print.NewPrinter()
172-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
170+
params := testparams.NewTestParams()
171+
173172
for _, tt := range tests {
174173
t.Run(tt.name, func(t *testing.T) {
175-
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
174+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
176175
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
177176
}
178177
})

internal/cmd/beta/alb/observability-credentials/add/add_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1210

1311
"github.com/google/go-cmp/cmp"
@@ -166,11 +164,10 @@ func Test_outputResult(t *testing.T) {
166164
},
167165
}
168166

169-
p := print.NewPrinter()
170-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
167+
params := testparams.NewTestParams()
171168
for _, tt := range tests {
172169
t.Run(tt.name, func(t *testing.T) {
173-
if err := outputResult(p, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr {
170+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr {
174171
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
175172
}
176173
})

internal/cmd/beta/alb/observability-credentials/delete/delete_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/stackitcloud/stackit-sdk-go/services/alb"
108

11-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
12-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1310

1411
"github.com/google/go-cmp/cmp"
1512
"github.com/google/go-cmp/cmp/cmpopts"
1613
"github.com/google/uuid"
14+
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1716
)
1817

1918
type testCtxKey struct{}
@@ -114,8 +113,8 @@ func TestParseInput(t *testing.T) {
114113

115114
for _, tt := range tests {
116115
t.Run(tt.description, func(t *testing.T) {
117-
p := print.NewPrinter()
118-
cmd := NewCmd(&types.CmdParams{Printer: p})
116+
params := testparams.NewTestParams()
117+
cmd := NewCmd(params.CmdParams)
119118
err := globalflags.Configure(cmd.Flags())
120119
if err != nil {
121120
t.Fatalf("configure global flags: %v", err)
@@ -147,7 +146,7 @@ func TestParseInput(t *testing.T) {
147146
t.Fatalf("error validating flags: %v", err)
148147
}
149148

150-
model, err := parseInput(p, cmd, tt.argValues)
149+
model, err := parseInput(params.Printer, cmd, tt.argValues)
151150
if err != nil {
152151
if !tt.isValid {
153152
return

internal/cmd/beta/alb/observability-credentials/describe/describe_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1210

1311
"github.com/google/go-cmp/cmp"
@@ -168,11 +166,11 @@ func Test_outputResult(t *testing.T) {
168166
wantErr: false,
169167
},
170168
}
171-
p := print.NewPrinter()
172-
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
169+
params := testparams.NewTestParams()
170+
173171
for _, tt := range tests {
174172
t.Run(tt.name, func(t *testing.T) {
175-
if err := outputResult(p, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr {
173+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.response); (err != nil) != tt.wantErr {
176174
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
177175
}
178176
})

0 commit comments

Comments
 (0)