A lightweight test helper package
0

Configure Feed

Select the types of activity you want to include in your feed.

Auto fix missing newlines in `Diff` and `DiffBytes` (#80)

authored by

Tom Fleet and committed by
GitHub
(Aug 25, 2025, 9:27 AM +0100) a85e9aa4 f84e2c2b

+64 -13
+8 -8
README.md
··· 59 59 60 60 ```plaintext 61 61 --- FAIL: TestDemo (0.00s) 62 - test_test.go:501: 62 + test_test.go:501: 63 63 Fruit scramble! 64 64 --------------- 65 - 65 + 66 66 Got: apples 67 67 Wanted: oranges 68 - 68 + 69 69 (Apples are not oranges!) 70 - 70 + 71 71 FAIL 72 72 ``` 73 73 ··· 122 122 wantErr: true, 123 123 }, 124 124 } 125 - 125 + 126 126 for _, tt := range tests { 127 127 t.Run(tt.name, func(t *testing.T) { 128 128 got, err := SomeFunction() 129 - 129 + 130 130 test.WantErr(t, err, tt.wantErr) 131 131 test.Equal(t, got, tt.want) 132 132 }) ··· 154 154 wantErr: true, 155 155 }, 156 156 } 157 - 157 + 158 158 for _, tt := range tests { 159 159 t.Run(tt.name, func(t *testing.T) { 160 160 got, err := SomeFunction() 161 - 161 + 162 162 if tt.wantErr { 163 163 test.Err(t, err) 164 164 } else {
+23 -5
test.go
··· 367 367 368 368 // Diff fails if the two strings got and want are not equal and provides a rich 369 369 // 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. 370 373 func Diff(tb testing.TB, got, want string) { 371 374 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)) 377 376 } 378 377 379 378 // DiffBytes fails if the two []byte got and want are not equal and provides a rich 380 379 // 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. 381 383 func DiffBytes(tb testing.TB, got, want []byte) { 382 384 tb.Helper() 385 + 386 + got = fixNL(got) 387 + want = fixNL(want) 383 388 384 389 if diff := diff.Diff("want", want, "got", got); diff != nil { 385 390 tb.Fatalf("\nDiff\n----\n%s\n", prettyDiff(string(diff))) ··· 507 512 } 508 513 509 514 return strings.Join(lines, "\n") 515 + } 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 510 528 }
+19
test_test.go
··· 405 405 wantFail: false, 406 406 }, 407 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 + }, 417 + { 408 418 name: "Diff/fail", 409 419 fn: func(tb testing.TB) { 410 420 got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n" 411 421 want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n" 422 + test.Diff(tb, got, want) 423 + }, 424 + wantFail: true, 425 + }, 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" 412 431 test.Diff(tb, got, want) 413 432 }, 414 433 wantFail: true,
+14
testdata/snapshots/TestTest/Diff/fail_no_trailing_newline.snap.txt
··· 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 +