Skip to content

Commit 93bf3a3

Browse files
authored
Testify, clean up, upgrade (#22)
Use testify for assertions, clean up go.mod, and upgrade everything.
2 parents be1a764 + 40b90f1 commit 93bf3a3

File tree

21 files changed

+253
-779
lines changed

21 files changed

+253
-779
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup Go
1717
uses: actions/setup-go@v1
1818
with:
19-
go-version: 1.16.x
19+
go-version: 1.17.x
2020

2121
- name: Checkout code
2222
uses: actions/checkout@v2
@@ -55,7 +55,7 @@ jobs:
5555
- name: Setup Go
5656
uses: actions/setup-go@v1
5757
with:
58-
go-version: 1.16.x
58+
go-version: 1.17.x
5959

6060
- name: Checkout code
6161
uses: actions/checkout@v2

cmd/restack/main_test.go

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package main
22

33
import (
44
"bytes"
5-
"errors"
6-
"strings"
75
"testing"
86

97
"github.com/abhinav/restack/internal/testwriter"
8+
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestRun_Version(t *testing.T) {
@@ -15,14 +14,9 @@ func TestRun_Version(t *testing.T) {
1514
Stdout: &stdout,
1615
Stderr: testwriter.New(t),
1716
}
18-
err := run(&opts, []string{"-version"})
19-
if err != nil {
20-
t.Errorf("unexpected error: %v", err)
21-
}
22-
23-
if stdout.Len() == 0 {
24-
t.Errorf("stdout should contain version information")
25-
}
17+
assert.NoError(t, run(&opts, []string{"-version"}))
18+
assert.NotEmpty(t, stdout.String(),
19+
"stdout should contain version information")
2620
}
2721

2822
func TestRun_CommandErrors(t *testing.T) {
@@ -50,13 +44,9 @@ func TestRun_CommandErrors(t *testing.T) {
5044
Stderr: &stderr,
5145
}
5246

53-
if err := run(opts, tt.give); !errors.Is(err, tt.want) {
54-
t.Errorf("unexpected error: got %v, want %v", err, tt.want)
55-
}
56-
57-
if stderr.Len() == 0 {
58-
t.Errorf("stderr should contain usage")
59-
}
47+
assert.ErrorIs(t, run(opts, tt.give), tt.want)
48+
assert.NotEmpty(t, stderr.String(),
49+
"stderr should contain usage")
6050
})
6151
}
6252
}
@@ -85,10 +75,7 @@ func TestNewSetup_Errors(t *testing.T) {
8575
Stdout: new(bytes.Buffer),
8676
Stderr: new(bytes.Buffer),
8777
}, tt.give)
88-
89-
if !strings.Contains(err.Error(), tt.want) {
90-
t.Errorf("unexpected error: got %v, should contain %q", err, tt.want)
91-
}
78+
assert.Contains(t, err.Error(), tt.want)
9279
})
9380
}
9481
}
@@ -100,17 +87,9 @@ func TestNewSetup(t *testing.T) {
10087
Stdout: &stdout,
10188
Stderr: &stderr,
10289
}, nil)
103-
if err != nil {
104-
t.Errorf("unexpected error: %v", err)
105-
}
106-
107-
if stdout.Len() > 0 {
108-
t.Errorf("stdout should be empty, got:\n%s", stdout.String())
109-
}
110-
111-
if stderr.Len() > 0 {
112-
t.Errorf("stderr should be empty, got:\n%s", stderr.String())
113-
}
90+
assert.NoError(t, err)
91+
assert.Empty(t, stdout.String(), "stdout should be empty")
92+
assert.Empty(t, stderr.String(), "stderr should be empty")
11493
}
11594

11695
func TestNewEdit_EditorParsing(t *testing.T) {
@@ -178,21 +157,10 @@ func TestNewEdit_EditorParsing(t *testing.T) {
178157
Stderr: &stderr,
179158
Getenv: func(k string) string { return env[k] },
180159
}, args)
181-
if err != nil {
182-
t.Errorf("unexpected error: %v", err)
183-
}
184-
185-
if got.Editor != tt.want {
186-
t.Errorf("unexpected editor %q, want %q", got.Editor, tt.want)
187-
}
188-
189-
if stdout.Len() > 0 {
190-
t.Errorf("stdout should be empty, got:\n%s", stdout.String())
191-
}
192-
193-
if stderr.Len() > 0 {
194-
t.Errorf("stderr should be empty, got:\n%s", stderr.String())
195-
}
160+
assert.NoError(t, err)
161+
assert.Equal(t, tt.want, got.Editor, "unexpected editor")
162+
assert.Empty(t, stdout.String(), "stdout should be empty")
163+
assert.Empty(t, stderr.String(), "stderr should be empty")
196164
})
197165
}
198166
}
@@ -231,19 +199,12 @@ func TestNewEdit_FileParsing(t *testing.T) {
231199
}, tt.args)
232200

233201
if tt.wantErr != nil {
234-
if !errors.Is(err, tt.wantErr) {
235-
t.Errorf("unexpected error: %v, want %v", err, tt.wantErr)
236-
}
202+
assert.ErrorIs(t, err, tt.wantErr)
237203
return
238204
}
239205

240-
if err != nil {
241-
t.Errorf("unexpected error: %v", err)
242-
}
243-
244-
if tt.want != got.Path {
245-
t.Errorf("unexpected path %q, want %q", got.Path, tt.want)
246-
}
206+
assert.NoError(t, err)
207+
assert.Equal(t, tt.want, got.Path, "unexpected path")
247208
})
248209
}
249210
}

edit_test.go

Lines changed: 34 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"fmt"
88
"io/ioutil"
99
"path/filepath"
10-
"strings"
1110
"testing"
1211

1312
"github.com/abhinav/restack/internal/editorfake"
1413
"github.com/abhinav/restack/internal/iotest"
14+
"github.com/abhinav/restack/internal/test"
1515
"github.com/abhinav/restack/internal/testwriter"
16-
"github.com/google/go-cmp/cmp"
16+
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
1718
)
1819

1920
var _noop = "noop\n"
@@ -22,9 +23,9 @@ func TestEdit(t *testing.T) {
2223
dir := iotest.TempDir(t, "edit")
2324
file := filepath.Join(dir, "git-rebase-todo")
2425

25-
if err := ioutil.WriteFile(file, []byte(_noop), 0600); err != nil {
26-
t.Fatalf("write temporary file: %v", err)
27-
}
26+
require.NoError(t,
27+
ioutil.WriteFile(file, []byte(_noop), 0600),
28+
"write temporary file")
2829

2930
restackerOutput := "x echo hello world"
3031
restacker := fakeRestacker{
@@ -49,23 +50,17 @@ func TestEdit(t *testing.T) {
4950
Stdout: testwriter.New(t),
5051
Stderr: testwriter.New(t),
5152
}).Run(ctx)
52-
53-
if err != nil {
54-
t.Errorf("edit failed: %v", err)
55-
}
53+
require.NoError(t, err, "edit failed")
5654

5755
gotOutput, err := ioutil.ReadFile(file)
58-
if err != nil {
59-
t.Fatalf("read output: %v", err)
60-
}
56+
require.NoError(t, err, "read output")
6157

62-
if diff := cmp.Diff(editorOutput, string(gotOutput)); len(diff) > 0 {
63-
t.Errorf("output mismatch: (-want, +got):\n%s", diff)
64-
}
58+
assert.Equal(t, editorOutput, string(gotOutput),
59+
"output mismatch")
6560
}
6661

6762
type fakeRestacker struct {
68-
T *testing.T
63+
T test.T
6964

7065
ran bool
7166
WantInput string
@@ -74,27 +69,22 @@ type fakeRestacker struct {
7469
}
7570

7671
func (r *fakeRestacker) VerifyRan() {
77-
if !r.ran {
78-
r.T.Errorf("restack never executed")
79-
}
72+
assert.True(r.T, r.ran, "restack never executed")
8073
}
8174

8275
func (r *fakeRestacker) Restack(ctx context.Context, req *Request) error {
8376
t := r.T
8477
r.ran = true
8578

8679
gotInput, err := ioutil.ReadAll(req.From)
87-
if err != nil {
88-
t.Errorf("read input: %v", err)
80+
if !assert.NoError(t, err, "read input") {
8981
return err
9082
}
9183

92-
if diff := cmp.Diff(r.WantInput, string(gotInput)); len(diff) > 0 {
93-
t.Errorf("input mismatch: (-want, +got):\n%s", diff)
94-
}
84+
assert.Equal(t, r.WantInput, string(gotInput), "input mismatch")
9585

96-
if _, err := req.To.Write([]byte(r.GiveOutput)); err != nil {
97-
t.Errorf("write output: %v", err)
86+
_, err = req.To.Write([]byte(r.GiveOutput))
87+
if !assert.NoError(t, err, "write output") {
9888
return err
9989
}
10090

@@ -112,20 +102,18 @@ func TestEdit_MissingFile(t *testing.T) {
112102
Stdout: testwriter.New(t),
113103
Stderr: testwriter.New(t),
114104
}).Run(ctx)
115-
if err == nil {
116-
t.Errorf("edit must fail")
117-
}
118-
errorMustContain(t, err, "no such file")
105+
assert.Error(t, err, "edit must fail")
106+
assert.Contains(t, err.Error(), "no such file")
119107
}
120108

121109
// Handle failures in restacking the instructions.
122110
func TestEdit_RestackFailed(t *testing.T) {
123111
dir := iotest.TempDir(t, "edit-restack-fail")
124112
file := filepath.Join(dir, "git-rebase-todo")
125113

126-
if err := ioutil.WriteFile(file, []byte(_noop), 0600); err != nil {
127-
t.Fatalf("write temporary file: %v", err)
128-
}
114+
require.NoError(t,
115+
ioutil.WriteFile(file, []byte(_noop), 0600),
116+
"write temporary file")
129117

130118
ctx := context.Background()
131119
err := (&Edit{
@@ -140,20 +128,18 @@ func TestEdit_RestackFailed(t *testing.T) {
140128
Stdout: testwriter.New(t),
141129
Stderr: testwriter.New(t),
142130
}).Run(ctx)
143-
if err == nil {
144-
t.Errorf("edit must fail")
145-
}
146-
errorMustContain(t, err, "great sadness")
131+
assert.Error(t, err, "edit must fail")
132+
assert.Contains(t, err.Error(), "great sadness")
147133
}
148134

149135
// Handle non-zero codes from editors.
150136
func TestEdit_EditorFailed(t *testing.T) {
151137
dir := iotest.TempDir(t, "edit-editor-fail")
152138
file := filepath.Join(dir, "git-rebase-todo")
153139

154-
if err := ioutil.WriteFile(file, []byte{}, 0600); err != nil {
155-
t.Fatalf("write temporary file: %v", err)
156-
}
140+
require.NoError(t,
141+
ioutil.WriteFile(file, []byte{}, 0600),
142+
"write temporary file")
157143

158144
restacker := fakeRestacker{T: t}
159145
defer restacker.VerifyRan()
@@ -169,11 +155,8 @@ func TestEdit_EditorFailed(t *testing.T) {
169155
Stdout: testwriter.New(t),
170156
Stderr: testwriter.New(t),
171157
}).Run(ctx)
172-
if err == nil {
173-
t.Fatalf("edit must fail")
174-
}
175-
176-
errorMustContain(t, err, "exit status 1")
158+
assert.Error(t, err, "edit must fail")
159+
assert.Contains(t, err.Error(), "exit status 1")
177160
}
178161

179162
// Handle failures in renaming if, for example, the file was deleted by the
@@ -182,9 +165,9 @@ func TestEdit_RenameFailed(t *testing.T) {
182165
dir := iotest.TempDir(t, "edit-rename-fail")
183166
file := filepath.Join(dir, "git-rebase-todo")
184167

185-
if err := ioutil.WriteFile(file, []byte{}, 0600); err != nil {
186-
t.Fatalf("write temporary file: %v", err)
187-
}
168+
require.NoError(t,
169+
ioutil.WriteFile(file, []byte{}, 0600),
170+
"write temporary file")
188171

189172
restacker := fakeRestacker{T: t}
190173
defer restacker.VerifyRan()
@@ -200,18 +183,7 @@ func TestEdit_RenameFailed(t *testing.T) {
200183
Stdout: testwriter.New(t),
201184
Stderr: testwriter.New(t),
202185
}).Run(ctx)
203-
if err == nil {
204-
t.Fatalf("edit must fail")
205-
}
206-
207-
errorMustContain(t, err, fmt.Sprintf("overwrite %q", file))
208-
errorMustContain(t, err, "no such file or directory")
209-
}
210-
211-
func errorMustContain(t *testing.T, err error, needle string) {
212-
t.Helper()
213-
214-
if !strings.Contains(err.Error(), needle) {
215-
t.Errorf("error %v must contain %q", err, needle)
216-
}
186+
assert.Error(t, err, "edit must fail")
187+
assert.Contains(t, err.Error(), fmt.Sprintf("overwrite %q", file))
188+
assert.Contains(t, err.Error(), "no such file or directory")
217189
}

0 commit comments

Comments
 (0)