-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpost-install.spec.ts
More file actions
96 lines (81 loc) · 3.04 KB
/
post-install.spec.ts
File metadata and controls
96 lines (81 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock child_process before importing
vi.mock('node:child_process', () => ({
execFileSync: vi.fn(),
}));
vi.mock('../utils/git-utils.js', () => ({
getDefaultBranch: vi.fn(() => 'main'),
getUncommittedFiles: vi.fn(() => []),
}));
import { execFileSync } from 'node:child_process';
import { stageAndCommit, detectChanges } from './post-install.js';
const mockExecFileSync = vi.mocked(execFileSync);
describe('post-install', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('stageAndCommit', () => {
it('stages all files and commits with message', () => {
mockExecFileSync.mockReturnValue(Buffer.from(''));
stageAndCommit('feat: add WorkOS AuthKit', '/test/dir');
expect(mockExecFileSync).toHaveBeenCalledWith('git', ['add', '-A'], {
cwd: '/test/dir',
stdio: 'ignore',
});
expect(mockExecFileSync).toHaveBeenCalledWith('git', ['commit', '-m', 'feat: add WorkOS AuthKit'], {
cwd: '/test/dir',
stdio: ['ignore', 'pipe', 'pipe'],
});
});
it('throws with stderr when commit fails from pre-commit hook', () => {
// First call (git add) succeeds
mockExecFileSync.mockReturnValueOnce(Buffer.from(''));
// Second call (git commit) fails with hook output
const error = new Error('Command failed') as Error & {
stderr: Buffer;
stdout: Buffer;
};
error.stderr = Buffer.from('eslint found 3 errors\nTypeError: missing return type');
error.stdout = Buffer.from('');
mockExecFileSync.mockImplementationOnce(() => {
throw error;
});
expect(() => stageAndCommit('feat: add WorkOS', '/test/dir')).toThrow(
/Git commit failed:\neslint found 3 errors/,
);
});
it('includes stdout when stderr is empty', () => {
mockExecFileSync.mockReturnValueOnce(Buffer.from(''));
const error = new Error('Command failed') as Error & {
stderr: Buffer;
stdout: Buffer;
};
error.stderr = Buffer.from('');
error.stdout = Buffer.from('pre-commit hook failed: formatting check');
mockExecFileSync.mockImplementationOnce(() => {
throw error;
});
expect(() => stageAndCommit('feat: add WorkOS', '/test/dir')).toThrow(
/Git commit failed:\npre-commit hook failed/,
);
});
it('falls back to error message when no stdio captured', () => {
mockExecFileSync.mockReturnValueOnce(Buffer.from(''));
const error = new Error('signal SIGTERM') as Error & {
stderr?: Buffer;
stdout?: Buffer;
};
mockExecFileSync.mockImplementationOnce(() => {
throw error;
});
expect(() => stageAndCommit('feat: add WorkOS', '/test/dir')).toThrow(/Git commit failed: signal SIGTERM/);
});
});
describe('detectChanges', () => {
it('returns hasChanges false when no uncommitted files', () => {
const result = detectChanges();
expect(result.hasChanges).toBe(false);
expect(result.files).toEqual([]);
});
});
});