-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
validateOptions.spec.js
129 lines (88 loc) Β· 3.72 KB
/
validateOptions.spec.js
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
import { constants, promises as fs } from 'fs'
import path from 'path'
import makeConsoleMock from 'consolemock'
import { validateOptions } from '../lib/validateOptions'
import { InvalidOptionsError } from '../lib/symbols'
describe('validateOptions', () => {
const mockAccess = jest.spyOn(fs, 'access')
beforeEach(() => {
mockAccess.mockClear()
})
it('should resolve empty and missing config', async () => {
expect.assertions(3)
const logger = makeConsoleMock()
mockAccess.mockImplementationOnce(async () => {})
await expect(validateOptions({}, logger)).resolves.toBeUndefined()
await expect(validateOptions(undefined, logger)).resolves.toBeUndefined()
expect(logger.history()).toHaveLength(0)
})
describe('cwd', () => {
it('should resolve with valid absolute cwd option', async () => {
expect.assertions(4)
const logger = makeConsoleMock()
await expect(validateOptions({ cwd: process.cwd() }, logger)).resolves.toBeUndefined()
expect(mockAccess).toHaveBeenCalledTimes(1)
expect(mockAccess).toHaveBeenCalledWith(process.cwd(), constants.F_OK)
expect(logger.history()).toHaveLength(0)
})
it('should resolve with valid relative cwd option', async () => {
expect.assertions(4)
const logger = makeConsoleMock()
await expect(validateOptions({ cwd: 'test' }, logger)).resolves.toBeUndefined()
expect(mockAccess).toHaveBeenCalledTimes(1)
expect(mockAccess).toHaveBeenCalledWith(path.join(process.cwd(), 'test'), constants.F_OK)
expect(logger.history()).toHaveLength(0)
})
it('should reject with invalid cwd option', async () => {
expect.assertions(4)
const logger = makeConsoleMock()
await expect(validateOptions({ cwd: 'non_existent' }, logger)).rejects.toThrowError(
InvalidOptionsError
)
expect(mockAccess).toHaveBeenCalledTimes(1)
expect(mockAccess).toHaveBeenCalledWith(
path.join(process.cwd(), 'non_existent'),
constants.F_OK
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
ERROR β Validation Error:
Invalid value for option 'cwd': non_existent
ENOENT: no such file or directory, access '${path
.join(process.cwd(), 'non_existent')
// Windows test fix: D:\something -> D:\\something
.replace(/\\/g, '\\\\')}'
See https://github.com/okonet/lint-staged#command-line-flags"
`)
})
})
describe('shell', () => {
it('should resolve with valid string-valued shell option', async () => {
expect.assertions(4)
const logger = makeConsoleMock()
mockAccess.mockImplementationOnce(async () => {})
await expect(validateOptions({ shell: '/bin/sh' }, logger)).resolves.toBeUndefined()
expect(mockAccess).toHaveBeenCalledTimes(1)
expect(mockAccess).toHaveBeenCalledWith('/bin/sh', constants.X_OK)
expect(logger.history()).toHaveLength(0)
})
it('should reject with invalid string-valued shell option', async () => {
expect.assertions(5)
const logger = makeConsoleMock()
mockAccess.mockImplementationOnce(() => Promise.reject(new Error('Failed')))
await expect(validateOptions({ shell: '/bin/sh' }, logger)).rejects.toThrowError(
InvalidOptionsError
)
expect(mockAccess).toHaveBeenCalledTimes(1)
expect(mockAccess).toHaveBeenCalledWith('/bin/sh', constants.X_OK)
expect(logger.history()).toHaveLength(1)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
ERROR β Validation Error:
Invalid value for option 'shell': /bin/sh
Failed
See https://github.com/okonet/lint-staged#command-line-flags"
`)
})
})
})