Skip to content

Commit

Permalink
fixes #1357, #1348 ensure we JSON stringify nested object properties …
Browse files Browse the repository at this point in the history
…as command line arguments

- this avoids needing our own serializer for nested objects such as
reporterOptions, config.hosts or config.blacklistHosts
- prevent adding additional underscored properties when propagating
arguments to electron
  • Loading branch information
brian-mann committed Jul 29, 2018
1 parent c0328de commit 62508ca
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 188 deletions.
16 changes: 0 additions & 16 deletions cli/__snapshots__/cypress_spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
exports['cypress .run normalizes config object 1'] = {
"config": "pageLoadTime=10000,watchForFileChanges=false"
}

exports['cypress .run normalizes env option if passed an object 1'] = {
"env": "foo=bar"
}

exports['cypress .run normalizes env option if passed an object with multiple properties 1'] = {
"env": "foo=bar,another=one"
}

exports['cypress .run resolves with contents of tmp file 1'] = {
"code": 0,
"failingTests": []
}

exports['cypress .open normalizes config object 1'] = {
"config": "pageLoadTime=10000,watchForFileChanges=false"
}
8 changes: 4 additions & 4 deletions cli/__snapshots__/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ exports['env_as_string 1'] = {
}

exports['env_as_object 1'] = {
"env": "foo=bar,magicNumber=1234,host=kevin.dev.local"
"env": "{\"foo\":\"bar\",\"magicNumber\":1234,\"host\":\"kevin.dev.local\"}"
}

exports['config_as_object 1'] = {
"config": "baseUrl=http://localhost:2000,watchForFileChanges=false"
"config": "{\"baseUrl\":\"http://localhost:2000\",\"watchForFileChanges\":false}"
}

exports['reporter_options_as_object 1'] = {
"reporterOptions": "mochaFile=results/my-test-output.xml,toConsole=true"
"reporterOptions": "{\"mochaFile\":\"results/my-test-output.xml\",\"toConsole\":true}"
}

exports['others_unchanged 1'] = {
"foo": "bar"
}

exports['spec_as_array 1'] = {
"spec": "a,b,c"
"spec": "[\"a\",\"b\",\"c\"]"
}

exports['spec_as_string 1'] = {
Expand Down
21 changes: 4 additions & 17 deletions cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,13 @@ const logger = require('./logger')
const debug = require('debug')('cypress:cli')

const getosAsync = Promise.promisify(getos)
const joinWithEq = (x, y) => `${x}=${y}`

// converts an object (single level) into
// key1=value1,key2=value2,...
const objectToString = (obj) =>
R.zipWith(joinWithEq, R.keys(obj), R.values(obj)).join(',')

const normalizeObject = (env) =>
_.isPlainObject(env) ? objectToString(env) : env

const normalizeArray = (arr) =>
_.isArray(arr) ? arr.join(',') : arr
const stringify = (val) => {
return _.isObject(val) ? JSON.stringify(val) : val
}

function normalizeModuleOptions (options = {}) {
return R.evolve({
env: normalizeObject,
config: normalizeObject,
reporterOptions: normalizeObject,
spec: normalizeArray,
})(options)
return _.mapValues(options, stringify)
}

function stdoutLineMatches (expectedLine, stdout) {
Expand Down
44 changes: 24 additions & 20 deletions cli/test/lib/cypress_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('cypress', function () {
}

it('calls open#start, passing in options', function () {
cypress.open({ foo: 'foo' })
return cypress.open({ foo: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.foo).to.equal('foo')
Expand All @@ -37,9 +37,12 @@ describe('cypress', function () {
pageLoadTime: 10000,
watchForFileChanges: false,
}
cypress.open({ config })

return cypress.open({ config })
.then(getStartArgs)
.then(snapshot)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})
})

Expand All @@ -66,44 +69,45 @@ describe('cypress', function () {
return normalizeCallArgs(getCallArgs(run.start))
}

it('calls run#start, passing in options', () =>
cypress.run({ foo: 'foo' })
it('calls run#start, passing in options', () => {
return cypress.run({ spec: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.foo).to.equal('foo')
expect(args.spec).to.equal('foo')
})
)
})

it('normalizes config object', () => {
const config = {
pageLoadTime: 10000,
watchForFileChanges: false,
}

return cypress.run({ config })
.then(getStartArgs)
.then(snapshot)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})

it('normalizes env option if passed an object', () =>
cypress.run({ env: { foo: 'bar' } })
.then(getStartArgs)
.then(snapshot)
)
it('normalizes env option if passed an object', () => {
const env = { foo: 'bar', another: 'one' }

it('normalizes env option if passed an object with multiple properties', () =>
cypress.run({ env: { foo: 'bar', another: 'one' } })
return cypress.run({ env })
.then(getStartArgs)
.then(snapshot)
)
.then((args) => {
expect(args).to.deep.eq({ env: JSON.stringify(env) })
})
})

it('gets random tmp file and passes it to run#start', function () {
return cypress.run().then(() => {
expect(run.start.lastCall.args[0].outputPath).to.equal(outputPath)
})
})

it('resolves with contents of tmp file', () =>
cypress.run().then(snapshot)
)
it('resolves with contents of tmp file', () => {
return cypress.run().then(snapshot)
})
})
})
27 changes: 0 additions & 27 deletions packages/server/lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,6 @@ validationRules = {
watchForFileChanges: v.isBoolean
}

toArrayFromPipes = (str) ->
if _.isArray(str)
return str

[].concat(str.split('|'))

toObjectFromPipes = (str) ->
if _.isObject(str)
return str

## convert foo=bar|version=1.2.3 to
## {foo: 'bar', version: '1.2.3'}
_
.chain(str)
.split("|")
.map (pair) ->
pair.split("=")
.fromPairs()
.mapValues(coerce)
.value()

convertRelativeToAbsolutePaths = (projectRoot, obj, defaults = {}) ->
_.reduce folders, (memo, folder) ->
val = obj[folder]
Expand Down Expand Up @@ -262,12 +241,6 @@ module.exports = {
config.cypressEnv = process.env["CYPRESS_ENV"]
delete config.envFile

if hosts = config.hosts
config.hosts = toObjectFromPipes(hosts)

if blacklistHosts = config.blacklistHosts
config.blacklistHosts = toArrayFromPipes(blacklistHosts)

## when headless
if config.isTextTerminal
## dont ever watch for file changes
Expand Down
Loading

0 comments on commit 62508ca

Please sign in to comment.