|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | +import { fixLineEndings } from '../../src/utils/line-endings'; |
| 6 | +import * as git from '../../src/utils/git'; |
| 7 | + |
| 8 | +describe('fixLineEndings', () => { |
| 9 | + let tmpDir: string; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'line-endings-test-')); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + fs.rmSync(tmpDir, { recursive: true }); |
| 17 | + vi.restoreAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('normalizes mixed line endings to CRLF when file has CRLF', () => { |
| 21 | + const filePath = path.join(tmpDir, 'mixed.dart'); |
| 22 | + fs.writeFileSync(filePath, 'line1\r\nline2\nline3\r\n', 'utf8'); |
| 23 | + |
| 24 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 25 | + `- ${filePath}`, |
| 26 | + ]); |
| 27 | + |
| 28 | + fixLineEndings(); |
| 29 | + |
| 30 | + const result = fs.readFileSync(filePath, 'utf8'); |
| 31 | + expect(result).toBe('line1\r\nline2\r\nline3\r\n'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('skips files that are pure LF', () => { |
| 35 | + const filePath = path.join(tmpDir, 'lf.dart'); |
| 36 | + const original = 'line1\nline2\n'; |
| 37 | + fs.writeFileSync(filePath, original, 'utf8'); |
| 38 | + |
| 39 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 40 | + `- ${filePath}`, |
| 41 | + ]); |
| 42 | + |
| 43 | + fixLineEndings(); |
| 44 | + |
| 45 | + const result = fs.readFileSync(filePath, 'utf8'); |
| 46 | + expect(result).toBe(original); |
| 47 | + }); |
| 48 | + |
| 49 | + it('leaves consistent CRLF files unchanged', () => { |
| 50 | + const filePath = path.join(tmpDir, 'crlf.dart'); |
| 51 | + const original = 'line1\r\nline2\r\n'; |
| 52 | + fs.writeFileSync(filePath, original, 'utf8'); |
| 53 | + |
| 54 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 55 | + `- ${filePath}`, |
| 56 | + ]); |
| 57 | + |
| 58 | + fixLineEndings(); |
| 59 | + |
| 60 | + const result = fs.readFileSync(filePath, 'utf8'); |
| 61 | + expect(result).toBe(original); |
| 62 | + }); |
| 63 | + |
| 64 | + it('skips non-text files', () => { |
| 65 | + const filePath = path.join(tmpDir, 'image.png'); |
| 66 | + const original = 'line1\r\nline2\nline3\r\n'; |
| 67 | + fs.writeFileSync(filePath, original, 'utf8'); |
| 68 | + |
| 69 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 70 | + `- ${filePath}`, |
| 71 | + ]); |
| 72 | + |
| 73 | + fixLineEndings(); |
| 74 | + |
| 75 | + const result = fs.readFileSync(filePath, 'utf8'); |
| 76 | + expect(result).toBe(original); |
| 77 | + }); |
| 78 | + |
| 79 | + it('skips directories', () => { |
| 80 | + const dirPath = path.join(tmpDir, 'subdir'); |
| 81 | + fs.mkdirSync(dirPath); |
| 82 | + |
| 83 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 84 | + `- ${dirPath}`, |
| 85 | + ]); |
| 86 | + |
| 87 | + expect(() => fixLineEndings()).not.toThrow(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('skips nonexistent files', () => { |
| 91 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([ |
| 92 | + '- nonexistent.txt', |
| 93 | + ]); |
| 94 | + |
| 95 | + expect(() => fixLineEndings()).not.toThrow(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does nothing when there are no modified files', () => { |
| 99 | + vi.spyOn(git, 'getUncommittedOrUntrackedFiles').mockReturnValue([]); |
| 100 | + |
| 101 | + expect(() => fixLineEndings()).not.toThrow(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments