Skip to content

Commit bf4f8ec

Browse files
davidmunechikamjhenkes
authored andcommitted
fix: Prevent Cypress from crashing when argument parsing "spec: {}" (#18312)
1 parent f3bb2ba commit bf4f8ec

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

packages/server/__snapshots__/args_spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ You passed: xyz
2121
2222
The error was: Cannot read property 'split' of undefined
2323
`
24+
25+
exports['invalid spec error'] = `
26+
Cypress encountered an error while parsing the argument spec
27+
28+
You passed: [object Object]
29+
30+
The error was: spec must be a string or comma-separated list
31+
`

packages/server/lib/util/args.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,29 @@ module.exports = {
247247
}
248248

249249
if (spec) {
250-
const resolvePath = (p) => {
251-
return path.resolve(options.cwd, p)
252-
}
250+
try {
251+
const resolvePath = (p) => {
252+
return path.resolve(options.cwd, p)
253+
}
253254

254-
// https://github.com/cypress-io/cypress/issues/8818
255-
// Sometimes spec is parsed to array. Because of that, we need check.
256-
if (typeof spec === 'string') {
257-
// clean up single quotes wrapping the spec for Windows users
258-
// https://github.com/cypress-io/cypress/issues/2298
259-
if (spec[0] === '\'' && spec[spec.length - 1] === '\'') {
260-
spec = spec.substring(1, spec.length - 1)
255+
// https://github.com/cypress-io/cypress/issues/8818
256+
// Sometimes spec is parsed to array. Because of that, we need check.
257+
if (typeof spec === 'string') {
258+
// clean up single quotes wrapping the spec for Windows users
259+
// https://github.com/cypress-io/cypress/issues/2298
260+
if (spec[0] === '\'' && spec[spec.length - 1] === '\'') {
261+
spec = spec.substring(1, spec.length - 1)
262+
}
263+
264+
options.spec = strToArray(spec).map(resolvePath)
265+
} else {
266+
options.spec = spec.map(resolvePath)
261267
}
268+
} catch (err) {
269+
debug('could not pass config spec value %s', spec)
270+
debug('error %o', err)
262271

263-
options.spec = strToArray(spec).map(resolvePath)
264-
} else {
265-
options.spec = spec.map(resolvePath)
272+
return errors.throw('COULD_NOT_PARSE_ARGUMENTS', 'spec', spec, 'spec must be a string or comma-separated list')
266273
}
267274
}
268275

packages/server/test/unit/args_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ describe('lib/util/args', () => {
152152

153153
expect(options.spec[0]).to.eq(`${cwd}/cypress/integration/foo_spec.js`)
154154
})
155+
156+
it('throws if argument cannot be parsed', function () {
157+
expect(() => {
158+
return this.setup('--run-project', 'foo', '--spec', {})
159+
}).to.throw
160+
161+
try {
162+
return this.setup('--run-project', 'foo', '--spec', {})
163+
} catch (err) {
164+
return snapshot('invalid spec error', stripAnsi(err.message))
165+
}
166+
})
155167
})
156168

157169
context('--tag', () => {

0 commit comments

Comments
 (0)