fix: Prevent Cypress from crashing when argument parsing "spec: {}" (#18312)

This commit is contained in:
David Munechika
2021-10-05 13:31:27 -04:00
committed by GitHub
parent b898d11afe
commit 56928c8ca0
3 changed files with 41 additions and 14 deletions

View File

@@ -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
`

View File

@@ -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')
}
}

View File

@@ -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', () => {