|
| 1 | +import { |
| 2 | + chmodSync, |
| 3 | + mkdtempSync, |
| 4 | + readFileSync, |
| 5 | + rmSync, |
| 6 | + statSync, |
| 7 | + utimesSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { expect, test } from 'rstack/test'; |
| 13 | +import { runFmtFiles } from '../../src/fmt/runner.ts'; |
| 14 | +import type { FmtFileRequest, FmtMode } from '../../src/fmt/types.ts'; |
| 15 | + |
| 16 | +const withProject = async (callback: (rootPath: string) => Promise<void>): Promise<void> => { |
| 17 | + const rootPath = mkdtempSync(path.join(tmpdir(), 'rstack fmt runner ')); |
| 18 | + |
| 19 | + try { |
| 20 | + await callback(rootPath); |
| 21 | + } finally { |
| 22 | + rmSync(rootPath, { force: true, recursive: true }); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const createRequest = (filePath: string): FmtFileRequest => ({ |
| 27 | + path: filePath, |
| 28 | + options: { |
| 29 | + filepath: filePath, |
| 30 | + parser: 'typescript', |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +const run = (files: FmtFileRequest[], mode: FmtMode = 'write') => |
| 35 | + runFmtFiles({ |
| 36 | + files, |
| 37 | + mode, |
| 38 | + cache: false, |
| 39 | + parallel: false, |
| 40 | + }); |
| 41 | + |
| 42 | +test('does not rewrite unchanged files', async () => { |
| 43 | + await withProject(async (rootPath) => { |
| 44 | + const filePath = path.join(rootPath, 'unchanged.ts'); |
| 45 | + const timestamp = new Date('2020-01-01T00:00:00.000Z'); |
| 46 | + writeFileSync(filePath, 'const value = 1;\n'); |
| 47 | + utimesSync(filePath, timestamp, timestamp); |
| 48 | + const mtimeMs = statSync(filePath).mtimeMs; |
| 49 | + |
| 50 | + const result = await run([createRequest(filePath)]); |
| 51 | + |
| 52 | + expect(result).toMatchObject({ |
| 53 | + exitCode: 0, |
| 54 | + files: [{ path: filePath, status: 'unchanged' }], |
| 55 | + }); |
| 56 | + expect(statSync(filePath).mtimeMs).toBe(mtimeMs); |
| 57 | + expect(result.durationMs).toBeGreaterThanOrEqual(0); |
| 58 | + expect(result.files[0].durationMs).toBeGreaterThanOrEqual(0); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +test('writes changed files', async () => { |
| 63 | + await withProject(async (rootPath) => { |
| 64 | + const filePath = path.join(rootPath, 'changed.ts'); |
| 65 | + writeFileSync(filePath, 'const value=1'); |
| 66 | + |
| 67 | + const result = await run([createRequest(filePath)]); |
| 68 | + |
| 69 | + expect(result).toMatchObject({ |
| 70 | + exitCode: 0, |
| 71 | + files: [{ path: filePath, status: 'written' }], |
| 72 | + }); |
| 73 | + expect(readFileSync(filePath, 'utf8')).toBe('const value = 1;\n'); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test.runIf(process.platform !== 'win32')('preserves file mode when writing', async () => { |
| 78 | + await withProject(async (rootPath) => { |
| 79 | + const filePath = path.join(rootPath, 'executable.ts'); |
| 80 | + writeFileSync(filePath, 'const value=1'); |
| 81 | + chmodSync(filePath, 0o744); |
| 82 | + |
| 83 | + await run([createRequest(filePath)]); |
| 84 | + |
| 85 | + expect(statSync(filePath).mode & 0o777).toBe(0o744); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +for (const mode of ['check', 'list-different'] as const) { |
| 90 | + test(`${mode} reports differences without writing`, async () => { |
| 91 | + await withProject(async (rootPath) => { |
| 92 | + const filePath = path.join(rootPath, 'different.ts'); |
| 93 | + const source = 'const value=1'; |
| 94 | + writeFileSync(filePath, source); |
| 95 | + |
| 96 | + const result = await run([createRequest(filePath)], mode); |
| 97 | + |
| 98 | + expect(result).toMatchObject({ |
| 99 | + exitCode: 1, |
| 100 | + files: [{ path: filePath, status: 'different' }], |
| 101 | + }); |
| 102 | + expect(readFileSync(filePath, 'utf8')).toBe(source); |
| 103 | + }); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +test('continues after a file fails and gives errors exit-code precedence', async () => { |
| 108 | + await withProject(async (rootPath) => { |
| 109 | + const invalidPath = path.join(rootPath, 'invalid.ts'); |
| 110 | + const validPath = path.join(rootPath, 'valid.ts'); |
| 111 | + writeFileSync(invalidPath, 'const value = ;'); |
| 112 | + writeFileSync(validPath, 'const value=1'); |
| 113 | + |
| 114 | + const result = await run([createRequest(invalidPath), createRequest(validPath)], 'check'); |
| 115 | + |
| 116 | + expect(result).toMatchObject({ |
| 117 | + exitCode: 2, |
| 118 | + files: [ |
| 119 | + { path: invalidPath, status: 'error' }, |
| 120 | + { path: validPath, status: 'different' }, |
| 121 | + ], |
| 122 | + }); |
| 123 | + expect(readFileSync(validPath, 'utf8')).toBe('const value=1'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments