forked from patternfly/patternfly-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-user-config.test.ts
More file actions
82 lines (69 loc) · 2.78 KB
/
git-user-config.test.ts
File metadata and controls
82 lines (69 loc) · 2.78 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
jest.mock('inquirer', () => ({
__esModule: true,
default: { prompt: jest.fn() },
}));
jest.mock('execa', () => ({
__esModule: true,
execa: jest.fn(),
}));
import inquirer from 'inquirer';
import { execa } from 'execa';
import { promptAndSetLocalGitUser } from '../git-user-config.js';
const mockPrompt = inquirer.prompt as jest.MockedFunction<typeof inquirer.prompt>;
const mockExeca = execa as jest.MockedFunction<typeof execa>;
describe('promptAndSetLocalGitUser', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
beforeEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
consoleErrorSpy.mockRestore();
consoleLogSpy.mockRestore();
});
it('reads global defaults then sets local user.name and user.email', async () => {
mockExeca
.mockResolvedValueOnce({ stdout: 'Jane Doe\n', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>)
.mockResolvedValueOnce({ stdout: 'jane@example.com\n', stderr: '', exitCode: 0 } as Awaited<
ReturnType<typeof execa>
>)
.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>);
mockPrompt.mockResolvedValue({
userName: ' Local Name ',
userEmail: ' local@example.com ',
});
await promptAndSetLocalGitUser('/tmp/proj');
expect(mockExeca).toHaveBeenNthCalledWith(1, 'git', ['config', '--global', 'user.name'], {
reject: false,
});
expect(mockExeca).toHaveBeenNthCalledWith(2, 'git', ['config', '--global', 'user.email'], {
reject: false,
});
expect(mockExeca).toHaveBeenNthCalledWith(3, 'git', ['config', '--local', 'user.name', 'Local Name'], {
cwd: '/tmp/proj',
stdio: 'inherit',
});
expect(mockExeca).toHaveBeenNthCalledWith(
4,
'git',
['config', '--local', 'user.email', 'local@example.com'],
{ cwd: '/tmp/proj', stdio: 'inherit' },
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('Set local git user.name and user.email'),
);
});
it('skips git config when name or email is empty after trim', async () => {
mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>);
mockPrompt.mockResolvedValue({ userName: '', userEmail: 'a@b.com' });
await promptAndSetLocalGitUser('/tmp/proj');
const localConfigCalls = mockExeca.mock.calls.filter(
([cmd, args]) =>
cmd === 'git' && Array.isArray(args) && (args as string[]).includes('--local'),
);
expect(localConfigCalls).toHaveLength(0);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('Both user.name and user.email are required'),
);
});
});