|
1 | | -import { beforeEach, expect, rs, test } from 'rstack/test'; |
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test } from 'rstack/test'; |
2 | 4 | import { formatFile } from '../../src/fmt/worker.ts'; |
3 | | - |
4 | | -const mocks = rs.hoisted(() => ({ |
5 | | - readFileCalls: [] as string[], |
6 | | - writeFileCalls: [] as [string, string, unknown][], |
7 | | -})); |
8 | | - |
9 | | -rs.mock('atomically', () => ({ |
10 | | - readFile: (path: string) => { |
11 | | - mocks.readFileCalls.push(path); |
12 | | - return Promise.resolve('const value=1'); |
13 | | - }, |
14 | | - writeFile: (path: string, data: string, options: unknown) => { |
15 | | - mocks.writeFileCalls.push([path, data, options]); |
16 | | - return Promise.resolve(); |
17 | | - }, |
18 | | -})); |
19 | | - |
20 | | -beforeEach(() => { |
21 | | - mocks.readFileCalls.length = 0; |
22 | | - mocks.writeFileCalls.length = 0; |
23 | | -}); |
24 | | - |
25 | | -test('disables fsync for atomic writes', async () => { |
26 | | - const filePath = '/virtual/example.ts'; |
27 | | - |
28 | | - await expect( |
29 | | - formatFile( |
30 | | - { |
31 | | - path: filePath, |
32 | | - options: { |
33 | | - filepath: filePath, |
34 | | - parser: 'typescript', |
| 5 | +import { withTempProject, writeProjectFile } from './helpers.ts'; |
| 6 | + |
| 7 | +test('writes formatted files', async () => { |
| 8 | + await withTempProject(async (rootPath) => { |
| 9 | + const filePath = writeProjectFile(rootPath, 'example.ts', 'const value=1'); |
| 10 | + |
| 11 | + await expect( |
| 12 | + formatFile( |
| 13 | + { |
| 14 | + path: filePath, |
| 15 | + options: { |
| 16 | + filepath: filePath, |
| 17 | + parser: 'typescript', |
| 18 | + }, |
35 | 19 | }, |
36 | | - }, |
37 | | - true, |
38 | | - ), |
39 | | - ).resolves.toBe('changed'); |
| 20 | + true, |
| 21 | + ), |
| 22 | + ).resolves.toBe('changed'); |
40 | 23 |
|
41 | | - expect(mocks.writeFileCalls).toEqual([ |
42 | | - [filePath, 'const value = 1;\n', { encoding: 'utf8', fsync: false }], |
43 | | - ]); |
| 24 | + expect(readFileSync(filePath, 'utf8')).toBe('const value = 1;\n'); |
| 25 | + }); |
44 | 26 | }); |
45 | 27 |
|
46 | 28 | test('infers the parser before formatting', async () => { |
47 | | - const filePath = '/virtual/example.ts'; |
48 | | - |
49 | | - await expect( |
50 | | - formatFile( |
51 | | - { |
52 | | - path: filePath, |
53 | | - options: { filepath: filePath }, |
54 | | - }, |
55 | | - false, |
56 | | - ), |
57 | | - ).resolves.toBe('changed'); |
| 29 | + await withTempProject(async (rootPath) => { |
| 30 | + const source = 'const value=1'; |
| 31 | + const filePath = writeProjectFile(rootPath, 'example.ts', source); |
| 32 | + |
| 33 | + await expect( |
| 34 | + formatFile( |
| 35 | + { |
| 36 | + path: filePath, |
| 37 | + options: { filepath: filePath }, |
| 38 | + }, |
| 39 | + false, |
| 40 | + ), |
| 41 | + ).resolves.toBe('changed'); |
58 | 42 |
|
59 | | - expect(mocks.readFileCalls).toEqual([filePath]); |
60 | | - expect(mocks.writeFileCalls).toEqual([]); |
| 43 | + expect(readFileSync(filePath, 'utf8')).toBe(source); |
| 44 | + }); |
61 | 45 | }); |
62 | 46 |
|
63 | 47 | test('skips unsupported files before reading them', async () => { |
64 | | - const filePath = '/virtual/example.unknown'; |
65 | | - |
66 | | - await expect( |
67 | | - formatFile( |
68 | | - { |
69 | | - path: filePath, |
70 | | - options: { filepath: filePath }, |
71 | | - }, |
72 | | - true, |
73 | | - ), |
74 | | - ).resolves.toBe('unsupported'); |
75 | | - |
76 | | - expect(mocks.readFileCalls).toEqual([]); |
77 | | - expect(mocks.writeFileCalls).toEqual([]); |
| 48 | + await withTempProject(async (rootPath) => { |
| 49 | + const filePath = path.join(rootPath, 'missing.unknown'); |
| 50 | + |
| 51 | + await expect( |
| 52 | + formatFile( |
| 53 | + { |
| 54 | + path: filePath, |
| 55 | + options: { filepath: filePath }, |
| 56 | + }, |
| 57 | + true, |
| 58 | + ), |
| 59 | + ).resolves.toBe('unsupported'); |
| 60 | + }); |
78 | 61 | }); |
0 commit comments