|
6 | 6 | // Pure utility functions |
7 | 7 | formatSize, |
8 | 8 | normalizeLineEndings, |
| 9 | + stripTrailingWhitespace, |
9 | 10 | createUnifiedDiff, |
10 | 11 | // Security & validation functions |
11 | 12 | validatePath, |
@@ -377,6 +378,57 @@ describe('Lib Functions', () => { |
377 | 378 | await expect(writeFileContent('/test/script.sh', 'new content')).resolves.toBeUndefined(); |
378 | 379 | expect(mockFs.unlink).not.toHaveBeenCalled(); |
379 | 380 | }); |
| 381 | + |
| 382 | + it('strips trailing whitespace from each line before writing', async () => { |
| 383 | + mockFs.writeFile.mockResolvedValueOnce(undefined); |
| 384 | + |
| 385 | + await writeFileContent('/test/file.txt', 'line1 \nline2\t\nline3'); |
| 386 | + |
| 387 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 388 | + '/test/file.txt', |
| 389 | + 'line1\nline2\nline3', |
| 390 | + { encoding: "utf-8", flag: 'wx' } |
| 391 | + ); |
| 392 | + }); |
| 393 | + |
| 394 | + it('strips trailing whitespace on the EEXIST/atomic-rename fallback path too', async () => { |
| 395 | + const eexistError = Object.assign(new Error('exists'), { code: 'EEXIST' }); |
| 396 | + mockFs.writeFile.mockRejectedValueOnce(eexistError); |
| 397 | + mockFs.stat.mockResolvedValueOnce({ mode: 0o100644 }); |
| 398 | + mockFs.writeFile.mockResolvedValueOnce(undefined); |
| 399 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 400 | + mockFs.chmod.mockResolvedValueOnce(undefined); |
| 401 | + |
| 402 | + await writeFileContent('/test/file.txt', 'line1 \nline2 '); |
| 403 | + |
| 404 | + expect(mockFs.writeFile).toHaveBeenLastCalledWith( |
| 405 | + expect.stringContaining('/test/file.txt.'), |
| 406 | + 'line1\nline2', |
| 407 | + 'utf-8' |
| 408 | + ); |
| 409 | + }); |
| 410 | + }); |
| 411 | + |
| 412 | + describe('stripTrailingWhitespace', () => { |
| 413 | + it('removes trailing spaces and tabs from each line', () => { |
| 414 | + const input = 'line1 \nline2\t\t\nline3'; |
| 415 | + expect(stripTrailingWhitespace(input)).toBe('line1\nline2\nline3'); |
| 416 | + }); |
| 417 | + |
| 418 | + it('leaves leading whitespace untouched', () => { |
| 419 | + const input = ' indented line \n\tmixed indent\t'; |
| 420 | + expect(stripTrailingWhitespace(input)).toBe(' indented line\n\tmixed indent'); |
| 421 | + }); |
| 422 | + |
| 423 | + it('does not change content with no trailing whitespace', () => { |
| 424 | + const input = 'line1\nline2\nline3'; |
| 425 | + expect(stripTrailingWhitespace(input)).toBe(input); |
| 426 | + }); |
| 427 | + |
| 428 | + it('preserves blank lines and trailing newline', () => { |
| 429 | + const input = 'line1 \n\nline3 \n'; |
| 430 | + expect(stripTrailingWhitespace(input)).toBe('line1\n\nline3\n'); |
| 431 | + }); |
380 | 432 | }); |
381 | 433 |
|
382 | 434 | describe('moveFile', () => { |
@@ -604,6 +656,60 @@ describe('Lib Functions', () => { |
604 | 656 | ); |
605 | 657 | }); |
606 | 658 |
|
| 659 | + it('strips trailing whitespace from the new text of an edit', async () => { |
| 660 | + const edits = [ |
| 661 | + { oldText: 'line2', newText: 'modified line2 \t' } |
| 662 | + ]; |
| 663 | + |
| 664 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 665 | + |
| 666 | + await applyFileEdits('/test/file.txt', edits, false); |
| 667 | + |
| 668 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 669 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 670 | + 'line1\nmodified line2\nline3\n', |
| 671 | + 'utf-8' |
| 672 | + ); |
| 673 | + }); |
| 674 | + |
| 675 | + it('does not touch trailing whitespace on lines outside the edit', async () => { |
| 676 | + // line3 has trailing spaces but is never targeted by an edit |
| 677 | + mockFs.readFile.mockResolvedValue('line1\nline2\nline3 \n'); |
| 678 | + |
| 679 | + const edits = [ |
| 680 | + { oldText: 'line1', newText: 'first line' } |
| 681 | + ]; |
| 682 | + |
| 683 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 684 | + |
| 685 | + await applyFileEdits('/test/file.txt', edits, false); |
| 686 | + |
| 687 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 688 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 689 | + 'first line\nline2\nline3 \n', |
| 690 | + 'utf-8' |
| 691 | + ); |
| 692 | + }); |
| 693 | + |
| 694 | + it('strips trailing whitespace from multi-line replacement text', async () => { |
| 695 | + const edits = [ |
| 696 | + { |
| 697 | + oldText: 'line1\nline2', |
| 698 | + newText: 'first line \nsecond line\t' |
| 699 | + } |
| 700 | + ]; |
| 701 | + |
| 702 | + mockFs.rename.mockResolvedValueOnce(undefined); |
| 703 | + |
| 704 | + await applyFileEdits('/test/file.txt', edits, false); |
| 705 | + |
| 706 | + expect(mockFs.writeFile).toHaveBeenCalledWith( |
| 707 | + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), |
| 708 | + 'first line\nsecond line\nline3\n', |
| 709 | + 'utf-8' |
| 710 | + ); |
| 711 | + }); |
| 712 | + |
607 | 713 | it('preserves file permissions after applying edits', async () => { |
608 | 714 | mockFs.stat.mockResolvedValue({ mode: 0o100755 }); |
609 | 715 | const edits = [ |
|
0 commit comments