-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.test.ts
More file actions
117 lines (89 loc) · 3.51 KB
/
cli.test.ts
File metadata and controls
117 lines (89 loc) · 3.51 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { describe, afterEach, test, expect, vi } from 'vitest';
import path from 'path';
import { pathToFileURL } from 'url';
import cli, { shouldRunAsCli } from './src/cli.js';
const originalArgv = process.argv;
function setArgv(args: string[]) {
process.argv = ['node', 'go-git-it', ...args];
}
describe('cli', () => {
afterEach(() => {
process.argv = originalArgv;
vi.restoreAllMocks();
});
test('prints help and exits with code 0', () => {
setArgv([]);
const exitMock = vi
.spyOn(process, 'exit')
.mockImplementation(((code?: number) => {
throw new Error(`process.exit:${code ?? 0}`);
}) as never);
const logMock = vi.spyOn(console, 'log').mockImplementation(() => {});
expect(() => cli(async () => {})).toThrow('process.exit:0');
expect(exitMock).toHaveBeenCalledWith(0);
expect(logMock).toHaveBeenCalled();
});
test('rejects invalid argument counts', () => {
setArgv(['one', 'two', 'three']);
const exitMock = vi
.spyOn(process, 'exit')
.mockImplementation(((code?: number) => {
throw new Error(`process.exit:${code ?? 0}`);
}) as never);
const errorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
expect(() => cli(async () => {})).toThrow('process.exit:1');
expect(exitMock).toHaveBeenCalledWith(1);
expect(errorMock).toHaveBeenCalled();
});
test('invokes goGitIt with provided arguments', async () => {
setArgv(['https://github.com/owner/repo', './out']);
const exitMock = vi.spyOn(process, 'exit');
const goGitIt = vi.fn().mockResolvedValue(undefined);
cli(goGitIt);
await Promise.resolve();
expect(goGitIt).toHaveBeenCalledWith(
'https://github.com/owner/repo',
'./out',
);
expect(exitMock).not.toHaveBeenCalled();
});
test('prints error and exits on failure', async () => {
setArgv(['https://github.com/owner/repo']);
const exitMock = vi
.spyOn(process, 'exit')
.mockImplementation((() => undefined) as never);
const errorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
const goGitIt = vi.fn().mockRejectedValue(new Error('boom'));
cli(goGitIt);
await new Promise<void>((resolve) => {
setImmediate(resolve);
});
expect(exitMock).toHaveBeenCalledWith(1);
expect(errorMock).toHaveBeenCalled();
});
});
describe('shouldRunAsCli', () => {
test('returns true for direct absolute entry path', () => {
const entryPath = path.join(process.cwd(), 'dist', 'index.js');
const importMetaUrl = pathToFileURL(entryPath).href;
expect(shouldRunAsCli(importMetaUrl, entryPath)).toBe(true);
});
test('returns true for relative entry path', () => {
const entryPath = path.join(process.cwd(), 'dist', 'index.js');
const importMetaUrl = pathToFileURL(entryPath).href;
const relativePath = path.relative(process.cwd(), entryPath);
expect(shouldRunAsCli(importMetaUrl, relativePath)).toBe(true);
});
test('returns true for binary name', () => {
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
expect(shouldRunAsCli(importMetaUrl, 'go-git-it')).toBe(true);
});
test('returns false for unrelated argv1', () => {
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
expect(shouldRunAsCli(importMetaUrl, '/usr/bin/node')).toBe(false);
});
test('returns false when argv1 is missing', () => {
const importMetaUrl = pathToFileURL('/tmp/fake/index.js').href;
expect(shouldRunAsCli(importMetaUrl, undefined)).toBe(false);
});
});