mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-11 17:50:01 -06:00
fix: Prevent Cypress from crashing when argument parsing "spec: {}" (#18312)
This commit is contained in:
@@ -21,3 +21,11 @@ You passed: xyz
|
||||
|
||||
The error was: Cannot read property 'split' of undefined
|
||||
`
|
||||
|
||||
exports['invalid spec error'] = `
|
||||
Cypress encountered an error while parsing the argument spec
|
||||
|
||||
You passed: [object Object]
|
||||
|
||||
The error was: spec must be a string or comma-separated list
|
||||
`
|
||||
|
||||
@@ -247,22 +247,29 @@ module.exports = {
|
||||
}
|
||||
|
||||
if (spec) {
|
||||
const resolvePath = (p) => {
|
||||
return path.resolve(options.cwd, p)
|
||||
}
|
||||
|
||||
// https://github.com/cypress-io/cypress/issues/8818
|
||||
// Sometimes spec is parsed to array. Because of that, we need check.
|
||||
if (typeof spec === 'string') {
|
||||
// clean up single quotes wrapping the spec for Windows users
|
||||
// https://github.com/cypress-io/cypress/issues/2298
|
||||
if (spec[0] === '\'' && spec[spec.length - 1] === '\'') {
|
||||
spec = spec.substring(1, spec.length - 1)
|
||||
try {
|
||||
const resolvePath = (p) => {
|
||||
return path.resolve(options.cwd, p)
|
||||
}
|
||||
|
||||
options.spec = strToArray(spec).map(resolvePath)
|
||||
} else {
|
||||
options.spec = spec.map(resolvePath)
|
||||
// https://github.com/cypress-io/cypress/issues/8818
|
||||
// Sometimes spec is parsed to array. Because of that, we need check.
|
||||
if (typeof spec === 'string') {
|
||||
// clean up single quotes wrapping the spec for Windows users
|
||||
// https://github.com/cypress-io/cypress/issues/2298
|
||||
if (spec[0] === '\'' && spec[spec.length - 1] === '\'') {
|
||||
spec = spec.substring(1, spec.length - 1)
|
||||
}
|
||||
|
||||
options.spec = strToArray(spec).map(resolvePath)
|
||||
} else {
|
||||
options.spec = spec.map(resolvePath)
|
||||
}
|
||||
} catch (err) {
|
||||
debug('could not pass config spec value %s', spec)
|
||||
debug('error %o', err)
|
||||
|
||||
return errors.throw('COULD_NOT_PARSE_ARGUMENTS', 'spec', spec, 'spec must be a string or comma-separated list')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -152,6 +152,18 @@ describe('lib/util/args', () => {
|
||||
|
||||
expect(options.spec[0]).to.eq(`${cwd}/cypress/integration/foo_spec.js`)
|
||||
})
|
||||
|
||||
it('throws if argument cannot be parsed', function () {
|
||||
expect(() => {
|
||||
return this.setup('--run-project', 'foo', '--spec', {})
|
||||
}).to.throw
|
||||
|
||||
try {
|
||||
return this.setup('--run-project', 'foo', '--spec', {})
|
||||
} catch (err) {
|
||||
return snapshot('invalid spec error', stripAnsi(err.message))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
context('--tag', () => {
|
||||
|
||||
Reference in New Issue
Block a user