Skip to content

Commit ab4bc63

Browse files
committed
mock isExecutable
1 parent 4ae15bc commit ab4bc63

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

tests/crypto/gpg.test.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ vi.mock('node:child_process');
99
vi.mock('node:fs/promises');
1010
vi.mock('node:fs');
1111

12+
vi.mock('is-executable', () => ({
13+
isExecutableSync: vi.fn(() => true)
14+
}));
15+
16+
1217
const okAccess = () => Promise.resolve(undefined);
1318
const failAccess = () => Promise.reject(new Error('EACCES'));
1419

@@ -24,23 +29,37 @@ function mockSpawnSyncOnce(result: Partial<child.SpawnSyncReturns<string>>) {
2429
(child.spawnSync as unknown as Mock).mockReturnValueOnce(r);
2530
}
2631

27-
describe('GpgWrapper.encryptFile', () => {
32+
describe('crypto::gpg', () => {
2833
beforeEach(() => {
2934
vi.restoreAllMocks();
3035
vi.resetAllMocks();
3136

3237
vi.mocked(fs.existsSync).mockReturnValue(true);
3338
vi.mocked(fsPromises.access).mockImplementation(okAccess);
34-
vi.mocked(child.spawnSync).mockImplementation(() => ({
35-
pid: 123,
36-
output: ['', '', ''],
37-
stdout: '',
38-
stderr: '',
39-
status: 0,
40-
signal: null,
41-
}));
39+
40+
// if querying PATH ("which"/"where"), return a valid path.
41+
vi.mocked(child.spawnSync).mockImplementation((cmd: any, args: any) => {
42+
const isWhich =
43+
(cmd === 'which' || cmd === 'where') &&
44+
Array.isArray(args) &&
45+
args[0] === 'gpg';
46+
47+
if (isWhich) {
48+
return {
49+
pid: 123,
50+
output: ['', '/usr/bin/gpg\n', ''],
51+
stdout: '/usr/bin/gpg\n',
52+
stderr: '',
53+
status: 0,
54+
signal: null,
55+
} as any;
56+
}
57+
58+
return { pid: 123, output: ['', '', ''], stdout: '', stderr: '', status: 0, signal: null } as any;
59+
});
4260
});
4361

62+
4463
it('fails when input file is not readable', async () => {
4564
(fsPromises.access as unknown as Mock).mockImplementationOnce(failAccess);
4665

0 commit comments

Comments
 (0)