testmark-go

avoid creeping extra linebreaks at the end of a patched document.

commit 7cb404c1bad32191968bf90af92e906af124f6f3 · parent 62acfdb766 · browse this tree
Eric Myhre, 2021-09-19 17:47:55 UTC

Similar story to the previous commit.
It's best to trim this on sight because it's roughly never desired.
diff --git a/patch.go b/patch.go
index b505249..6ca50f2 100644
--- a/patch.go
+++ b/patch.go
@@ -92,6 +92,13 @@ func Patch(oldDoc *Document, hunks ...Hunk) (newDoc *Document) {
 		newDoc.Lines = append(newDoc.Lines, []byte{})
 	}
 
+	// If we've ended up with multiple trailing blank lines, that's usually considered undesirable,
+	//  so let's nip those back down.
+	l := len(newDoc.Lines)
+	if l > 1 && len(newDoc.Lines[l-1]) == 0 {
+		newDoc.Lines = newDoc.Lines[0 : l-1]
+	}
+
 	return
 }