File tree Expand file tree Collapse file tree 3 files changed +44
-6
lines changed Expand file tree Collapse file tree 3 files changed +44
-6
lines changed Original file line number Diff line number Diff line change 5
5
"io/ioutil"
6
6
"path/filepath"
7
7
"reflect"
8
+ "strings"
8
9
"testing"
9
10
)
10
11
@@ -158,3 +159,33 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
158
159
}
159
160
}
160
161
}
162
+
163
+ func TestNoNewlineAtEnd (t * testing.T ) {
164
+ orig := `@@ -1,1 +1,1 @@
165
+ -b
166
+ +b
167
+ \ No newline at end of file
168
+ `
169
+
170
+ hunks , err := ParseHunks ([]byte (orig ))
171
+ if err != nil {
172
+ t .Fatal ("ParseHunks: %s" , err )
173
+ }
174
+
175
+ for _ , hunk := range hunks {
176
+ if body := string (hunk .Body ); strings .Contains (body , "No newline" ) {
177
+ t .Errorf ("after parse, hunk body contains 'No newline...' string\n \n body is:\n %q" , body )
178
+ }
179
+ if bytes .HasSuffix (hunk .Body , []byte {'\n' }) {
180
+ t .Errorf ("after parse, hunk body ends with newline\n \n body is:\n %q" , hunk .Body )
181
+ }
182
+
183
+ printed , err := PrintHunks (hunks )
184
+ if err != nil {
185
+ t .Fatal ("PrintHunks: %s" , err )
186
+ }
187
+ if printed := string (printed ); printed != orig {
188
+ t .Errorf ("printed diff hunks != original diff hunks\n \n # PrintHunks output:\n %q\n \n # Original:\n %q" , printed , orig )
189
+ }
190
+ }
191
+ }
Original file line number Diff line number Diff line change @@ -391,7 +391,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
391
391
// handle that case.
392
392
return r .hunk , & ParseError {r .line , r .offset , & ErrBadHunkLine {Line : line }}
393
393
}
394
- if string (line ) == noNewlineMessage {
394
+ if bytes . Equal (line , [] byte ( noNewlineMessage )) {
395
395
// Remove previous line's newline.
396
396
if len (r .hunk .Body ) != 0 {
397
397
r .hunk .Body = r .hunk .Body [:len (r .hunk .Body )- 1 ]
@@ -414,7 +414,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
414
414
return nil , io .EOF
415
415
}
416
416
417
- const noNewlineMessage = " \\ No newline at end of file\n "
417
+ const noNewlineMessage = `\ No newline at end of file`
418
418
419
419
// linePrefixes is the set of all characters a valid line in a diff
420
420
// hunk can start with. '\' can appear in diffs when no newline is
Original file line number Diff line number Diff line change @@ -86,13 +86,20 @@ func PrintHunks(hunks []*Hunk) ([]byte, error) {
86
86
if _ , err := fmt .Fprintln (& buf ); err != nil {
87
87
return nil , err
88
88
}
89
- if len (hunk .Body ) != 0 && hunk .Body [len (hunk .Body )- 1 ] != '\n' {
90
- // Append message if hunk.Body doesn't end with a newline.
91
- hunk .Body = append (hunk .Body , noNewlineMessage ... )
92
- }
93
89
if _ , err := buf .Write (hunk .Body ); err != nil {
94
90
return nil , err
95
91
}
92
+ if ! bytes .HasSuffix (hunk .Body , []byte {'\n' }) {
93
+ if _ , err := fmt .Fprintln (& buf ); err != nil {
94
+ return nil , err
95
+ }
96
+ if _ , err := buf .Write ([]byte (noNewlineMessage )); err != nil {
97
+ return nil , err
98
+ }
99
+ if _ , err := fmt .Fprintln (& buf ); err != nil {
100
+ return nil , err
101
+ }
102
+ }
96
103
}
97
104
return buf .Bytes (), nil
98
105
}
You can’t perform that action at this time.
0 commit comments