-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.unit.test.ts
More file actions
136 lines (118 loc) · 4 KB
/
cli.unit.test.ts
File metadata and controls
136 lines (118 loc) · 4 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { describe, expect, it } from 'vitest';
import {
createCliCommandObject,
createCliCommandString,
objectToCliArgs,
} from './cli.js';
describe('objectToCliArgs', () => {
it('should empty params', () => {
const result = objectToCliArgs();
expect(result).toEqual([]);
});
it('should handle the "_" argument as script', () => {
const params = { _: 'bin.js' };
const result = objectToCliArgs(params);
expect(result).toEqual(['bin.js']);
});
it('should handle the "_" argument with multiple values', () => {
const params = { _: ['bin.js', '--help'] };
const result = objectToCliArgs(params);
expect(result).toEqual(['bin.js', '--help']);
});
it('should handle shorthands arguments', () => {
const params = {
e: `test`,
};
const result = objectToCliArgs(params);
expect(result).toEqual([`-e="${params.e}"`]);
});
it('should handle string arguments', () => {
const params = { name: 'Juanita' };
const result = objectToCliArgs(params);
expect(result).toEqual(['--name="Juanita"']);
});
it('should handle number arguments', () => {
const params = { parallel: 5 };
const result = objectToCliArgs(params);
expect(result).toEqual(['--parallel=5']);
});
it('should handle boolean arguments', () => {
const params = { verbose: true };
const result = objectToCliArgs(params);
expect(result).toEqual(['--verbose']);
});
it('should handle negated boolean arguments', () => {
const params = { verbose: false };
const result = objectToCliArgs(params);
expect(result).toEqual(['--no-verbose']);
});
it('should handle array of string arguments', () => {
const params = { format: ['json', 'md'] };
const result = objectToCliArgs(params);
expect(result).toEqual(['--format="json"', '--format="md"']);
});
it('should handle objects', () => {
const params = { format: { json: 'simple' } };
const result = objectToCliArgs(params);
expect(result).toStrictEqual(['--format.json="simple"']);
});
it('should handle nested objects', () => {
const params = { persist: { format: ['json', 'md'], skipReports: false } };
const result = objectToCliArgs(params);
expect(result).toEqual([
'--persist.format="json"',
'--persist.format="md"',
'--no-persist.skipReports',
]);
});
it('should handle objects with undefined', () => {
const params = { format: undefined };
const result = objectToCliArgs(params);
expect(result).toStrictEqual([]);
});
it('should throw error for unsupported type', () => {
expect(() => objectToCliArgs({ param: Symbol('') })).toThrow(
'Unsupported type',
);
});
});
describe('createCliCommandString', () => {
it('should create command out of object for arguments', () => {
const result = createCliCommandString({ args: { verbose: true } });
expect(result).toBe('npx @code-pushup/cli --verbose');
});
it('should create command out of object for arguments with positional', () => {
const result = createCliCommandString({
args: { _: 'autorun', verbose: true },
});
expect(result).toBe('npx @code-pushup/cli autorun --verbose');
});
});
describe('createCliCommandObject', () => {
it('should create command out of object for arguments', () => {
expect(createCliCommandObject({ args: { verbose: true } })).toStrictEqual({
args: ['@code-pushup/cli', '--verbose'],
command: 'npx',
});
});
it('should create command out of object for arguments with positional', () => {
expect(
createCliCommandObject({
args: { _: 'autorun', verbose: true },
}),
).toStrictEqual({
args: ['@code-pushup/cli', 'autorun', '--verbose'],
command: 'npx',
});
});
it('should create command out of object for arguments with bin', () => {
expect(
createCliCommandObject({
bin: 'node_modules/@code-pushup/cli/src/bin.js',
}),
).toStrictEqual({
args: ['node_modules/@code-pushup/cli/src/bin.js'],
command: 'npx',
});
});
});