|
6 | 6 | // Pure utility functions |
7 | 7 | formatSize, |
8 | 8 | normalizeLineEndings, |
| 9 | + stripTrailingWhitespace, |
9 | 10 | createUnifiedDiff, |
10 | 11 | // Security & validation functions |
11 | 12 | validatePath, |
@@ -308,6 +309,55 @@ describe('Lib Functions', () => { |
308 | 309 |
|
309 | 310 | expect(mockFs.writeFile).toHaveBeenCalledWith('/test/file.txt', 'new content', { encoding: "utf-8", flag: 'wx' }); |
310 | 311 | }); |
| 312 | + |
| 313 | + it('strips trailing whitespace from each line before writing', async () => { |
| 314 | + mockFs.writeFile.mockResolvedValueOnce(undefined); |
| 315 | + |
| 316 | + await writeFileContent('/test/file.txt', 'line1 \nline2\t\nline3'); |
| 317 | + |
| 318 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 319 | + '/test/file.txt', |
| 320 | + 'line1\nline2\nline3', |
| 321 | + { encoding: "utf-8", flag: 'wx' } |
| 322 | + ); |
| 323 | + }); |
| 324 | + |
| 325 | + it('strips trailing whitespace on the EEXIST/atomic-rename fallback path too', async () => { |
| 326 | + const eexistError = Object.assign(new Error('exists'), { code: 'EEXIST' }); |
| 327 | + mockFs.writeFile.mockRejectedValueOnce(eexistError); |
| 328 | + mockFs.writeFile.mockResolvedValueOnce(undefined); |
| 329 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 330 | + |
| 331 | + await writeFileContent('/test/file.txt', 'line1 \nline2 '); |
| 332 | + |
| 333 | + expect(mockFs.writeFile).toHaveBeenLastCalledWith( |
| 334 | + expect.stringContaining('/test/file.txt.'), |
| 335 | + 'line1\nline2', |
| 336 | + 'utf-8' |
| 337 | + ); |
| 338 | + }); |
| 339 | + }); |
| 340 | + |
| 341 | + describe('stripTrailingWhitespace', () => { |
| 342 | + it('removes trailing spaces and tabs from each line', () => { |
| 343 | + const input = 'line1 \nline2\t\t\nline3'; |
| 344 | + expect(stripTrailingWhitespace(input)).toBe('line1\nline2\nline3'); |
| 345 | + }); |
| 346 | + |
| 347 | + it('leaves leading whitespace untouched', () => { |
| 348 | + const input = ' indented line \n\tmixed indent\t'; |
| 349 | + expect(stripTrailingWhitespace(input)).toBe(' indented line\n\tmixed indent'); |
| 350 | + }); |
| 351 | + |
| 352 | + it('does not change content with no trailing whitespace', () => { |
| 353 | + const input = 'line1\nline2\nline3'; |
| 354 | + expect(stripTrailingWhitespace(input)).toBe(input); |
| 355 | + }); |
| 356 | + |
| 357 | + it('preserves blank lines and trailing newline', () => { |
| 358 | + const input = 'line1 \n\nline3 \n'; |
| 359 | + expect(stripTrailingWhitespace(input)).toBe('line1\n\nline3\n'); |
| 360 | + }); |
311 | 361 | }); |
312 | 362 |
|
313 | 363 | }); |
@@ -510,6 +560,60 @@ describe('Lib Functions', () => { |
510 | 560 | ); |
511 | 561 | }); |
512 | 562 |
|
| 563 | + it('strips trailing whitespace from the new text of an edit', async () => { |
| 564 | + const edits = [ |
| 565 | + { oldText: 'line2', newText: 'modified line2 \t' } |
| 566 | + ]; |
| 567 | + |
| 568 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 569 | + |
| 570 | + await applyFileEdits('/test/file.txt', edits, false); |
| 571 | + |
| 572 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 573 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 574 | + 'line1\nmodified line2\nline3\n', |
| 575 | + 'utf-8' |
| 576 | + ); |
| 577 | + }); |
| 578 | + |
| 579 | + it('does not touch trailing whitespace on lines outside the edit', async () => { |
| 580 | + // line3 has trailing spaces but is never targeted by an edit |
| 581 | + mockFs.readFile.mockResolvedValue('line1\nline2\nline3 \n'); |
| 582 | + |
| 583 | + const edits = [ |
| 584 | + { oldText: 'line1', newText: 'first line' } |
| 585 | + ]; |
| 586 | + |
| 587 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 588 | + |
| 589 | + await applyFileEdits('/test/file.txt', edits, false); |
| 590 | + |
| 591 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 592 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 593 | + 'first line\nline2\nline3 \n', |
| 594 | + 'utf-8' |
| 595 | + ); |
| 596 | + }); |
| 597 | + |
| 598 | + it('strips trailing whitespace from multi-line replacement text', async () => { |
| 599 | + const edits = [ |
| 600 | + { |
| 601 | + oldText: 'line1\nline2', |
| 602 | + newText: 'first line \nsecond line\t' |
| 603 | + } |
| 604 | + ]; |
| 605 | + |
| 606 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 607 | + |
| 608 | + await applyFileEdits('/test/file.txt', edits, false); |
| 609 | + |
| 610 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 611 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 612 | + 'first line\nsecond line\nline3\n', |
| 613 | + 'utf-8' |
| 614 | + ); |
| 615 | + }); |
| 616 | + |
513 | 617 | it('throws error for non-matching edits', async () => { |
514 | 618 | const edits = [ |
515 | 619 | { oldText: 'nonexistent line', newText: 'replacement' } |
|
0 commit comments