Skip to content

Commit a85e9aa

Browse files
Auto fix missing newlines in Diff and DiffBytes (#80)
1 parent f84e2c2 commit a85e9aa

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ Will get you an error log in the test that looks like this...
5959

6060
```plaintext
6161
--- FAIL: TestDemo (0.00s)
62-
test_test.go:501:
62+
test_test.go:501:
6363
Fruit scramble!
6464
---------------
65-
65+
6666
Got: apples
6767
Wanted: oranges
68-
68+
6969
(Apples are not oranges!)
70-
70+
7171
FAIL
7272
```
7373

@@ -122,11 +122,11 @@ func TestTableThings(t *testing.T) {
122122
wantErr: true,
123123
},
124124
}
125-
125+
126126
for _, tt := range tests {
127127
t.Run(tt.name, func(t *testing.T) {
128128
got, err := SomeFunction()
129-
129+
130130
test.WantErr(t, err, tt.wantErr)
131131
test.Equal(t, got, tt.want)
132132
})
@@ -154,11 +154,11 @@ func TestTableThings(t *testing.T) {
154154
wantErr: true,
155155
},
156156
}
157-
157+
158158
for _, tt := range tests {
159159
t.Run(tt.name, func(t *testing.T) {
160160
got, err := SomeFunction()
161-
161+
162162
if tt.wantErr {
163163
test.Err(t, err)
164164
} else {

test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,20 +367,25 @@ func False(tb testing.TB, got bool, options ...Option) {
367367

368368
// Diff fails if the two strings got and want are not equal and provides a rich
369369
// unified diff of the two for easy comparison.
370+
//
371+
// If either got or want do not end in a newline, one is added to avoid a
372+
// "No newline at end of file" warning in the diff which is visually distracting.
370373
func Diff(tb testing.TB, got, want string) {
371374
tb.Helper()
372-
373-
// TODO(@FollowTheProcess): If either got or want don't end in a newline, add one
374-
if diff := diff.Diff("want", []byte(want), "got", []byte(got)); diff != nil {
375-
tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff)))
376-
}
375+
DiffBytes(tb, []byte(got), []byte(want))
377376
}
378377

379378
// DiffBytes fails if the two []byte got and want are not equal and provides a rich
380379
// unified diff of the two for easy comparison.
380+
//
381+
// If either got or want do not end in a newline, one is added to avoid a
382+
// "No newline at end of file" warning in the diff which is visually distracting.
381383
func DiffBytes(tb testing.TB, got, want []byte) {
382384
tb.Helper()
383385

386+
got = fixNL(got)
387+
want = fixNL(want)
388+
384389
if diff := diff.Diff("want", want, "got", got); diff != nil {
385390
tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff)))
386391
}
@@ -508,3 +513,16 @@ func prettyDiff(diff string) string {
508513

509514
return strings.Join(lines, "\n")
510515
}
516+
517+
// If data is empty or ends in \n, fixNL returns data.
518+
// Otherwise fixNL returns a new slice consisting of data with a final \n added.
519+
func fixNL(data []byte) []byte {
520+
if len(data) == 0 || data[len(data)-1] == '\n' {
521+
return data
522+
}
523+
d := make([]byte, len(data)+1)
524+
copy(d, data)
525+
d[len(data)] = '\n'
526+
527+
return d
528+
}

test_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ func TestTest(t *testing.T) {
404404
},
405405
wantFail: false,
406406
},
407+
{
408+
name: "Diff/pass no trailing newline",
409+
fn: func(tb testing.TB) {
410+
got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
411+
want := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
412+
413+
test.Diff(tb, got, want)
414+
},
415+
wantFail: false,
416+
},
407417
{
408418
name: "Diff/fail",
409419
fn: func(tb testing.TB) {
@@ -413,6 +423,15 @@ func TestTest(t *testing.T) {
413423
},
414424
wantFail: true,
415425
},
426+
{
427+
name: "Diff/fail no trailing newline",
428+
fn: func(tb testing.TB) {
429+
got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff"
430+
want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff"
431+
test.Diff(tb, got, want)
432+
},
433+
wantFail: true,
434+
},
416435
{
417436
name: "DiffBytes/pass",
418437
fn: func(tb testing.TB) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Diff
3+
----
4+
diff want got
5+
--- want
6+
+++ got
7+
@@ -1,4 +1,4 @@
8+
Some
9+
- different stuff here in this file
10+
- this line is different
11+
+ stuff here in this file
12+
+ lines as well wow
13+
some more stuff
14+

0 commit comments

Comments
 (0)