X Tutup
Skip to content

Commit 3d017b2

Browse files
committed
Fix stderr output on delete errors
1 parent 75c1dfd commit 3d017b2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

cmd/ghcs/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func delete(ctx context.Context, log logger, opts deleteOptions) error {
151151
codespaceName := c.Name
152152
g.Go(func() error {
153153
if err := opts.apiClient.DeleteCodespace(ctx, user.Login, codespaceName); err != nil {
154-
_, _ = log.Errorf("error deleting codespace %q: %v", codespaceName, err)
154+
_, _ = log.Errorf("error deleting codespace %q: %v\n", codespaceName, err)
155155
return err
156156
}
157157
return nil

cmd/ghcs/delete_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ghcs
22

33
import (
4+
"bytes"
45
"context"
6+
"errors"
57
"fmt"
68
"sort"
79
"testing"
810
"time"
911

12+
"github.com/github/ghcs/cmd/ghcs/output"
1013
"github.com/github/ghcs/internal/api"
1114
)
1215

@@ -22,8 +25,11 @@ func TestDelete(t *testing.T) {
2225
opts deleteOptions
2326
codespaces []*api.Codespace
2427
confirms map[string]bool
28+
deleteErr error
2529
wantErr bool
2630
wantDeleted []string
31+
wantStdout string
32+
wantStderr string
2733
}{
2834
{
2935
name: "by name",
@@ -80,6 +86,24 @@ func TestDelete(t *testing.T) {
8086
},
8187
wantDeleted: []string{"hubot-robawt-abc", "monalisa-spoonknife-c4f3"},
8288
},
89+
{
90+
name: "deletion failed",
91+
opts: deleteOptions{
92+
deleteAll: true,
93+
},
94+
codespaces: []*api.Codespace{
95+
{
96+
Name: "monalisa-spoonknife-123",
97+
},
98+
{
99+
Name: "hubot-robawt-abc",
100+
},
101+
},
102+
deleteErr: errors.New("aborted by test"),
103+
wantErr: true,
104+
wantDeleted: []string{"hubot-robawt-abc", "monalisa-spoonknife-123"},
105+
wantStderr: "error deleting codespace \"hubot-robawt-abc\": aborted by test\nerror deleting codespace \"monalisa-spoonknife-123\": aborted by test\n",
106+
},
83107
{
84108
name: "with confirm",
85109
opts: deleteOptions{
@@ -131,6 +155,9 @@ func TestDelete(t *testing.T) {
131155
if userLogin != user.Login {
132156
return fmt.Errorf("unexpected user %q", userLogin)
133157
}
158+
if tt.deleteErr != nil {
159+
return tt.deleteErr
160+
}
134161
return nil
135162
},
136163
}
@@ -171,7 +198,10 @@ func TestDelete(t *testing.T) {
171198
},
172199
}
173200

174-
err := delete(context.Background(), nil, opts)
201+
stdout := &bytes.Buffer{}
202+
stderr := &bytes.Buffer{}
203+
log := output.NewLogger(stdout, stderr, false)
204+
err := delete(context.Background(), log, opts)
175205
if (err != nil) != tt.wantErr {
176206
t.Errorf("delete() error = %v, wantErr %v", err, tt.wantErr)
177207
}
@@ -186,6 +216,12 @@ func TestDelete(t *testing.T) {
186216
if !sliceEquals(gotDeleted, tt.wantDeleted) {
187217
t.Errorf("deleted %q, want %q", gotDeleted, tt.wantDeleted)
188218
}
219+
if out := stdout.String(); out != tt.wantStdout {
220+
t.Errorf("stdout = %q, want %q", out, tt.wantStdout)
221+
}
222+
if out := stderr.String(); out != tt.wantStderr {
223+
t.Errorf("stderr = %q, want %q", out, tt.wantStderr)
224+
}
189225
})
190226
}
191227
}

0 commit comments

Comments
 (0)
X Tutup