Skip to content

Commit

Permalink
fix: Prevent Cypress from crashing when argument parsing "spec: {}" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmunechika authored and mjhenkes committed Jul 28, 2022
1 parent f3bb2ba commit bf4f8ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
8 changes: 8 additions & 0 deletions packages/server/__snapshots__/args_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
33 changes: 20 additions & 13 deletions packages/server/lib/util/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,29 @@ module.exports = {
}

if (spec) {
const resolvePath = (p) => {
return path.resolve(options.cwd, p)
}
try {
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)
// 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)

options.spec = strToArray(spec).map(resolvePath)
} else {
options.spec = spec.map(resolvePath)
return errors.throw('COULD_NOT_PARSE_ARGUMENTS', 'spec', spec, 'spec must be a string or comma-separated list')
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/server/test/unit/args_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

1 comment on commit bf4f8ec

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bf4f8ec Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.6.0/circle-matth/fix/hang-investigation-bf4f8ec004b7584823f45841e74961fb8eb86591/cypress.tgz

Please sign in to comment.