Skip to content

Commit 4507ca5

Browse files
authored
test(staged): cover rs fmt integration (#127)
1 parent db33067 commit 4507ca5

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { spawnSync } from 'node:child_process';
2+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3+
import path from 'node:path';
4+
import { afterEach, beforeEach, expect, test } from 'rstack/test';
5+
import { RSTACK_BIN_PATH } from '#test-helpers';
6+
7+
let projectPath: string;
8+
let env: NodeJS.ProcessEnv;
9+
10+
const writeProjectFile = (filePath: string, content: string): void =>
11+
writeFileSync(path.join(projectPath, filePath), content);
12+
13+
const readProjectFile = (filePath: string): string =>
14+
readFileSync(path.join(projectPath, filePath), 'utf8');
15+
16+
const git = (args: string[]): string => {
17+
const result = spawnSync('git', args, {
18+
cwd: projectPath,
19+
encoding: 'utf8',
20+
env,
21+
});
22+
23+
if (result.status !== 0) {
24+
throw new Error(result.stderr || `Git exited with status ${result.status}.`);
25+
}
26+
27+
return result.stdout;
28+
};
29+
30+
const runStaged = () =>
31+
spawnSync(process.execPath, [RSTACK_BIN_PATH, 'staged', '--no-stash'], {
32+
cwd: projectPath,
33+
encoding: 'utf8',
34+
env,
35+
});
36+
37+
beforeEach(() => {
38+
projectPath = mkdtempSync(path.join(import.meta.dirname, 'staged-fmt-project-'));
39+
env = {
40+
...process.env,
41+
GIT_CONFIG_GLOBAL: path.join(projectPath, 'global.gitconfig'),
42+
GIT_CONFIG_NOSYSTEM: '1',
43+
};
44+
45+
git(['init', '--quiet']);
46+
writeProjectFile(
47+
'rstack.config.ts',
48+
`import { define } from 'rstack';
49+
50+
define.fmt({
51+
ignorePatterns: ['ignored-by-fmt.ts'],
52+
});
53+
54+
define.staged({
55+
'*': 'rs fmt',
56+
});
57+
`,
58+
);
59+
});
60+
61+
afterEach(() => {
62+
rmSync(projectPath, { force: true, recursive: true });
63+
});
64+
65+
test('formats staged files with rs fmt and applies ignore rules', () => {
66+
writeProjectFile('.gitignore', 'ignored-by-git.ts\n');
67+
writeProjectFile('file with spaces.ts', 'const spaced="spaced"');
68+
writeProjectFile('ignored-by-git.ts', 'const gitIgnored="git ignored"');
69+
writeProjectFile('ignored-by-fmt.ts', 'const fmtIgnored="fmt ignored"');
70+
71+
git(['add', '--', 'file with spaces.ts', 'ignored-by-fmt.ts']);
72+
git(['add', '--force', '--', 'ignored-by-git.ts']);
73+
74+
const result = runStaged();
75+
76+
expect(result.status).toBe(0);
77+
expect(readProjectFile('file with spaces.ts')).toBe('const spaced = "spaced";\n');
78+
expect(readProjectFile('ignored-by-git.ts')).toBe('const gitIgnored = "git ignored";\n');
79+
expect(readProjectFile('ignored-by-fmt.ts')).toBe('const fmtIgnored="fmt ignored"');
80+
expect(git(['show', ':file with spaces.ts'])).toBe('const spaced = "spaced";\n');
81+
expect(git(['show', ':ignored-by-git.ts'])).toBe('const gitIgnored = "git ignored";\n');
82+
});
83+
84+
test('propagates rs fmt failures', () => {
85+
writeProjectFile('invalid.ts', 'const value = ;');
86+
git(['add', '--', 'invalid.ts']);
87+
88+
const result = runStaged();
89+
90+
expect(result.status).toBe(1);
91+
expect(`${result.stdout}\n${result.stderr}`).toContain('SyntaxError');
92+
expect(readProjectFile('invalid.ts')).toBe('const value = ;');
93+
});

0 commit comments

Comments
 (0)