Skip to content

Commit

Permalink
server: fixes #1159, specs are normalized into an array resolved agai…
Browse files Browse the repository at this point in the history
…nst cwd
  • Loading branch information
brian-mann committed Jan 7, 2018
1 parent 08c8260 commit 25820ad
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 24 deletions.
5 changes: 0 additions & 5 deletions cli/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash')
const path = require('path')
const commander = require('commander')
const { oneLine } = require('common-tags')
const debug = require('debug')('cypress:cli')
Expand All @@ -17,10 +16,6 @@ const parseOpts = (opts) => {
'browser', 'detached', 'headed',
'group', 'groupId', 'global', 'dev')

if (opts.project) {
opts.project = path.resolve(opts.project)
}

debug('parsed cli options', opts)

return opts
Expand Down
1 change: 1 addition & 0 deletions cli/lib/cypress.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cypressModuleApi = {

run (options = {}) {
options = util.normalizeModuleOptions(options)

return tmp.fileAsync()
.then((outputPath) => {
options.outputPath = outputPath
Expand Down
3 changes: 2 additions & 1 deletion cli/lib/exec/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const verify = require('../tasks/verify')
// and forms list of CLI arguments to the server
const processRunOptions = (options = {}) => {
debug('processing run options')

const args = ['--run-project', options.project]

//// if key is set use that - else attempt to find it by env var
Expand All @@ -27,7 +28,7 @@ const processRunOptions = (options = {}) => {
args.push('--port', options.port)
}

//// if we have a specific spec push that into the args
// if we have specific spec(s) push that into the args
if (options.spec) {
args.push('--spec', options.spec)
}
Expand Down
9 changes: 2 additions & 7 deletions cli/test/lib/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const verify = require(`${lib}/tasks/verify`)
const install = require(`${lib}/tasks/install`)
const snapshot = require('snap-shot-it')
const execa = require('execa-wrap')
const path = require('path')

describe('cli', function () {
beforeEach(function () {
Expand Down Expand Up @@ -180,10 +179,8 @@ describe('cli', function () {
})

it('calls run with relative --project folder', function () {
this.sandbox.stub(path, 'resolve')
.withArgs('foo/bar').returns('/mock/absolute/foo/bar')
this.exec('run --project foo/bar')
expect(run.start).to.be.calledWith({ project: '/mock/absolute/foo/bar' })
expect(run.start).to.be.calledWith({ project: 'foo/bar' })
})

it('calls run with absolute --project folder', function () {
Expand All @@ -203,10 +200,8 @@ describe('cli', function () {
})

it('calls open.start with relative --project folder', function () {
this.sandbox.stub(path, 'resolve')
.withArgs('foo/bar').returns('/mock/absolute/foo/bar')
this.exec('open --project foo/bar')
expect(open.start).to.be.calledWith({ project: '/mock/absolute/foo/bar' })
expect(open.start).to.be.calledWith({ project: 'foo/bar' })
})

it('calls open.start with absolute --project folder', function () {
Expand Down
25 changes: 20 additions & 5 deletions packages/server/lib/util/args.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ coerce = require("./coerce")
config = require("../config")
cwd = require("../cwd")

whitelist = "appPath execPath apiKey smokeTest getKey generateKey runProject project spec ci record updating ping key logs clearLogs returnPkg version mode autoOpen removeIds headed config exitWithCode hosts browser headless outputPath group groupId exit".split(" ")
whitelist = "cwd appPath execPath apiKey smokeTest getKey generateKey runProject project spec ci record updating ping key logs clearLogs returnPkg version mode autoOpen removeIds headed config exit exitWithCode hosts browser headless outputPath group groupId parallel parallelId".split(" ")
whitelist = whitelist.concat(config.getConfigKeys())

everythingAfterFirstEqualRe = /=(.+)/
Expand Down Expand Up @@ -39,6 +39,9 @@ normalizeBackslashes = (options) ->

options

parseArrayValues = (vals) ->
[].concat(vals.split(','))

parseNestedValues = (vals) ->
## convert foo=bar,version=1.2.3 to
## {foo: 'bar', version: '1.2.3'}
Expand Down Expand Up @@ -83,11 +86,16 @@ module.exports = {
}
})

whitelisted = _.pick(argv, whitelist...)
whitelisted = _.pick(argv, whitelist)

options = _
.chain(options)
.defaults(whitelisted)
.defaults({
## set in case we
## bypassed the cli
cwd: process.cwd()
})
.mapValues(coerce)
.value()

Expand All @@ -101,6 +109,13 @@ module.exports = {
## and apply them to both appPath + execPath
[options.appPath, options.execPath] = options._.slice(-2)

if spec = options.spec
backup("spec", options)

resolvePath = (p) ->
path.resolve(options.cwd, p)

options.spec = parseArrayValues(spec).map(resolvePath)
if hosts = options.hosts
backup("hosts", options)
options.hosts = parseNestedValues(hosts)
Expand Down Expand Up @@ -130,11 +145,11 @@ module.exports = {

## normalize project to projectPath
if p = options.project or options.runProject
options.projectPath = path.resolve(cwd(), p)
options.projectPath = path.resolve(options.cwd, p)

## normalize output path from current working directory
## normalize output path from previous current working directory
if op = options.outputPath
options.outputPath = path.resolve(cwd(), op)
options.outputPath = path.resolve(options.cwd, op)

if options.runProject
options.run = true
Expand Down
6 changes: 5 additions & 1 deletion packages/server/test/support/helpers/e2e.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ module.exports = {
ctx.timeout(options.timeout)

if spec = options.spec
## normalize into array and then prefix
specs = spec.split(',').map (spec) ->
path.join(e2ePath, "cypress", "integration", spec)

## normalize the path to the spec
options.spec = spec = path.join("cypress", "integration", spec)
options.spec = specs.join(',')

return options

Expand Down
41 changes: 36 additions & 5 deletions packages/server/test/unit/args_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require("../spec_helper")
path = require("path")
argsUtil = require("#{root}lib/util/args")

cwd = process.cwd()

describe "lib/util/args", ->
beforeEach ->
@setup = (args...) ->
Expand All @@ -15,13 +17,13 @@ describe "lib/util/args", ->

context "--project", ->
it "sets projectPath", ->
projectPath = path.resolve(process.cwd(), "./foo/bar")
projectPath = path.resolve(cwd, "./foo/bar")
options = @setup("--project", "./foo/bar")
expect(options.projectPath).to.eq projectPath

context "--run-project", ->
it "sets projectPath", ->
projectPath = path.resolve(process.cwd(), "/baz")
projectPath = path.resolve(cwd, "/baz")
options = @setup("--run-project", "/baz")
expect(options.projectPath).to.eq projectPath

Expand All @@ -35,6 +37,10 @@ describe "lib/util/args", ->
options = @setup("--run-project", '"foo bar"')
expect(options.runProject).to.eq('"foo bar"')

context "--spec", ->
it "converts to array", ->


context "--port", ->
it "converts to Number", ->
options = @setup("--port", "8080")
Expand Down Expand Up @@ -79,15 +85,24 @@ describe "lib/util/args", ->
context ".toObject", ->
beforeEach ->
## make sure it works with both --env=foo=bar and --config foo=bar
@obj = @setup("--get-key", "--hosts=*.foobar.com=127.0.0.1", "--env=foo=bar,baz=quux,bar=foo=quz", "--config", "requestTimeout=1234,responseTimeout=9876")
@obj = @setup(
"--get-key",
"--hosts=*.foobar.com=127.0.0.1",
"--env=foo=bar,baz=quux,bar=foo=quz",
"--config",
"requestTimeout=1234,responseTimeout=9876"
"--reporter-options=foo=bar"
"--spec=foo,bar,baz",
)

it "coerces booleans", ->
expect(@setup("--foo=true").foo).be.true
expect(@setup("--no-record").record).to.be.false
expect(@setup("--record=false").record).to.be.false

it "backs up hosts + env", ->
it "backs up hosts, env, config, reporterOptions, spec", ->
expect(@obj).to.deep.eq({
cwd
_: []
"get-key": true
getKey: true
Expand All @@ -106,16 +121,30 @@ describe "lib/util/args", ->
_config: "requestTimeout=1234,responseTimeout=9876"
requestTimeout: 1234
responseTimeout: 9876
"reporter-options": "foo=bar"
_reporterOptions: "foo=bar"
reporterOptions: {
foo: "bar"
}
_spec: "foo,bar,baz"
spec: [
path.join(cwd, "foo"),
path.join(cwd, "bar"),
path.join(cwd, "baz")
]
})

it "can transpose back to an array", ->
expect(argsUtil.toArray(@obj)).to.deep.eq([
"--cwd=#{cwd}"
"--getKey=true"
"--spec=foo,bar,baz",
"--config=requestTimeout=1234,responseTimeout=9876"
"--hosts=*.foobar.com=127.0.0.1"
"--env=foo=bar,baz=quux,bar=foo=quz"
"--reporterOptions=foo=bar"
"--requestTimeout=1234"
"--responseTimeout=9876"
"--responseTimeout=9876",
])

context "--updating", ->
Expand All @@ -131,6 +160,7 @@ describe "lib/util/args", ->
]

expect(argsUtil.toObject(argv)).to.deep.eq({
cwd
_: [
"/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress"
"/Applications/Cypress.app"
Expand All @@ -152,6 +182,7 @@ describe "lib/util/args", ->
]

expect(argsUtil.toObject(argv)).to.deep.eq({
cwd
_: [
"/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress"
"/Applications/Cypress.app1"
Expand Down

0 comments on commit 25820ad

Please sign in to comment.