@@ -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.
370373func 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 ("\n Diff\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.
381383func 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 ("\n Diff\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+ }
0 commit comments