-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsqip-cli.test.ts
More file actions
274 lines (216 loc) · 7.9 KB
/
sqip-cli.test.ts
File metadata and controls
274 lines (216 loc) · 7.9 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { vi, type MockedFunction } from 'vitest'
import { fstatSync } from 'fs'
import { sqip, resolvePlugins } from 'sqip'
import sqipCLI from '../../src/sqip-cli'
import semver from 'semver'
vi.mock('fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('fs')>()
return { ...actual, fstatSync: vi.fn(actual.fstatSync) }
})
const mockedFstatSync = fstatSync as MockedFunction<typeof fstatSync>
vi.mock('sqip', () => ({
sqip: vi.fn(async () => []),
resolvePlugins: vi.fn(() => [
{
name: 'mocked',
Plugin: {
cliOptions: [
{
name: 'mocked-plugin-option',
type: Boolean,
description: 'This is mocked'
}
]
}
}
])
}))
const mockedSqip = sqip as MockedFunction<typeof sqip>
const mockedResolvePlugins = resolvePlugins as MockedFunction<
typeof resolvePlugins
>
const logSpy = vi.spyOn(global.console, 'log').mockImplementation(() => null)
const errorSpy = vi
.spyOn(global.console, 'error')
.mockImplementation(() => null)
const proccessExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never)
const originalArgv = process.argv
describe('sqip-plugin-cli', () => {
afterEach(() => {
logSpy.mockClear()
errorSpy.mockClear()
proccessExitSpy.mockClear()
mockedSqip.mockClear()
mockedResolvePlugins.mockClear()
mockedFstatSync.mockRestore()
process.argv = originalArgv
})
afterAll(() => {
logSpy.mockReset()
errorSpy.mockReset()
proccessExitSpy.mockReset()
})
it('no args: show help and require input option', async () => {
await sqipCLI()
expect(mockedSqip).not.toHaveBeenCalled()
expect(mockedResolvePlugins.mock.calls).toMatchSnapshot('resolve')
expect(logSpy.mock.calls[0][0]).toMatch(/-h.*Show help/)
expect(logSpy.mock.calls[0][0]).toMatch(/-p.*One or more plugins/)
expect(logSpy.mock.calls[0][0]).toMatch(
/--output.*Define the path of the resulting file/
)
expect(logSpy.mock.calls[0][0]).toMatch(/Examples/)
expect(errorSpy).toHaveBeenCalledWith(
'\nPlease provide the following arguments: input'
)
expect(global.process.exit).toHaveBeenCalledWith(1)
})
it('--version: show version', async () => {
process.argv = ['', '', '--version']
await sqipCLI()
expect(mockedSqip).not.toHaveBeenCalled()
expect(mockedResolvePlugins.mock.calls).toMatchSnapshot('resolve')
expect(semver.valid(logSpy.mock.calls[0][0])).toBe(logSpy.mock.calls[0][0])
expect(errorSpy.mock.calls).toMatchSnapshot('error')
expect(global.process.exit).toHaveBeenCalledWith(0)
})
it('--help: show help', async () => {
process.argv = ['', '', '--help']
await sqipCLI()
expect(mockedSqip).not.toHaveBeenCalled()
expect(mockedResolvePlugins.mock.calls).toMatchSnapshot('resolve')
expect(logSpy.mock.calls[0][0]).toMatch(/-h.*Show help/)
expect(logSpy.mock.calls[0][0]).toMatch(/-p.*One or more plugins/)
expect(logSpy.mock.calls[0][0]).toMatch(
/--output.*Define the path of the resulting file/
)
expect(logSpy.mock.calls[0][0]).toMatch(/Examples/)
expect(errorSpy.mock.calls).toMatchSnapshot('error')
expect(global.process.exit).toHaveBeenCalledWith(0)
})
it('throws on missing input', async () => {
process.argv = ['', '']
await sqipCLI()
expect(errorSpy.mock.calls).toMatchSnapshot('error')
expect(mockedSqip).not.toHaveBeenCalled()
})
it('passes all given args to library', async () => {
process.argv = [
'',
'',
'-p',
'primitive',
'blur',
'svgo',
'-i',
'mocked-image.jpg',
'-o',
'/tmp/mocked-image.svg'
]
await sqipCLI()
expect(errorSpy).not.toHaveBeenCalled()
expect(mockedSqip).toHaveBeenCalled()
expect(mockedSqip.mock.calls).toMatchSnapshot()
})
it('parses plugin-specific options from CLI args', async () => {
process.argv = [
'',
'',
'-i',
'mocked-image.jpg',
'--mocked-mocked-plugin-option'
]
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
const mockedPlugin = callArgs.plugins?.find(
(p: { name: string }) => p.name === 'mocked'
) as { name: string; options: Record<string, unknown> } | undefined
expect(mockedPlugin).toBeDefined()
expect(mockedPlugin?.options).toEqual({ 'mocked-plugin-option': true })
})
it('handles resolvePlugins errors gracefully', async () => {
mockedResolvePlugins.mockRejectedValueOnce(new Error('plugin not found'))
process.argv = ['', '', '-i', 'mocked-image.jpg']
// process.exit is mocked so execution continues past the catch block,
// causing a secondary TypeError. We only care that the error was reported.
await sqipCLI().catch(() => {})
expect(errorSpy).toHaveBeenCalled()
expect(proccessExitSpy).toHaveBeenCalledWith(1)
})
it('handles sqip errors gracefully', async () => {
mockedSqip.mockRejectedValueOnce(new Error('test error'))
process.argv = ['', '', '-i', 'mocked-image.jpg']
await sqipCLI()
expect(errorSpy).toHaveBeenCalled()
expect(proccessExitSpy).toHaveBeenCalledWith(1)
})
describe('stdin support', () => {
const fakeImageBuffer = Buffer.from('fake-image-data')
const originalToArray = process.stdin.toArray
afterEach(() => {
process.stdin.toArray = originalToArray
})
function mockStdin(data: Buffer) {
mockedFstatSync.mockReturnValue({ isFIFO: () => true } as ReturnType<typeof fstatSync>)
process.stdin.toArray = (() => Promise.resolve([data])) as typeof process.stdin.toArray
}
it('reads input from stdin when no --input flag', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(Buffer.isBuffer(callArgs.input)).toBe(true)
expect(callArgs.input).toEqual(fakeImageBuffer)
})
it('--input flag takes precedence over stdin', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-i', 'mocked-image.jpg', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.input).toBe('mocked-image.jpg')
})
it('defaults print to true for stdin input', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.print).toBe(true)
})
it('does not set output when input is from stdin', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.output).toBeUndefined()
})
it('sets outputFileName to stdin for stdin input', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.outputFileName).toBe('stdin')
})
it('respects -o flag with stdin input', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked', '-o', '/tmp/out.svg']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.output).toBe('/tmp/out.svg')
})
it('defaults silent to true for stdin input', async () => {
mockStdin(fakeImageBuffer)
process.argv = ['', '', '-p', 'mocked']
await sqipCLI()
expect(mockedSqip).toHaveBeenCalled()
const callArgs = mockedSqip.mock.calls[0][0]
expect(callArgs.silent).toBe(true)
})
})
})