Skip to content

Commit

Permalink
Issue 1159 (#1259)
Browse files Browse the repository at this point in the history
* server: pass --cwd from CLI to use when resolving relative paths for various options

- remove unnecessary cwd manipulation in scripts/start

* server: fixes #1159, specs are normalized into an array resolved against cwd

- projectPath is now normalized against cwd as well

* server: move hosts out of CLI args, keep as config only

* server: convert spec array to string on module API

* cli: must ref root package directly

* server: fixes busted specs due to cherry pick

* server: temporary fix for specs being normalized into an array

* server: move around spec flattening earlier

* server: pass absolute path for specs

* server: revert flattening hosts into config temporarily

* server: add correct relative + absolute path to spec

* driver: normalize spec path against project

* driver: skip flaky test for now [skip ci]
  • Loading branch information
brian-mann authored Feb 7, 2018
1 parent 8e20541 commit 001a310
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 57 deletions.
8 changes: 8 additions & 0 deletions cli/__snapshots__/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ exports['reporter_options_as_object 1'] = {
exports['others_unchanged 1'] = {
"foo": "bar"
}

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

exports['spec_as_string 1'] = {
"spec": "x,y,z"
}
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
3 changes: 2 additions & 1 deletion cli/lib/exec/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module.exports = {

debug('needs XVFB?', needsXvfb)

args = [].concat(args)
// always push cwd into the args
args = [].concat(args, '--cwd', process.cwd())

_.defaults(options, {
detached: false,
Expand Down
4 changes: 4 additions & 0 deletions cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ const objectToString = (obj) =>
const normalizeObject = (env) =>
_.isPlainObject(env) ? objectToString(env) : env

const normalizeArray = (arr) =>
_.isArray(arr) ? arr.join(',') : arr

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

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 () {
require('mocha-banner').register()
Expand Down Expand Up @@ -182,10 +181,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 @@ -205,10 +202,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
19 changes: 17 additions & 2 deletions cli/test/lib/exec/spawn_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const xvfb = require(`${lib}/exec/xvfb`)
const spawn = require(`${lib}/exec/spawn`)
const util = require(`${lib}/util.js`)

const cwd = process.cwd()

describe('exec spawn', function () {
beforeEach(function () {
this.sandbox.stub(process, 'exit')
Expand Down Expand Up @@ -40,7 +42,13 @@ describe('exec spawn', function () {

return spawn.start('--foo', { foo: 'bar' })
.then(() => {
expect(cp.spawn).to.be.calledWithMatch('/path/to/cypress', ['--foo'], { foo: 'bar' })
expect(cp.spawn).to.be.calledWithMatch('/path/to/cypress', [
'--foo',
'--cwd',
cwd,
], {
foo: 'bar',
})
})
})

Expand All @@ -51,7 +59,14 @@ describe('exec spawn', function () {

return spawn.start('--foo', { dev: true, foo: 'bar' })
.then(() => {
expect(cp.spawn).to.be.calledWithMatch('node', [p, '--foo'], { foo: 'bar' })
expect(cp.spawn).to.be.calledWithMatch('node', [
p,
'--foo',
'--cwd',
cwd,
], {
foo: 'bar',
})
})
})

Expand Down
16 changes: 16 additions & 0 deletions cli/test/lib/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe('util', function () {
}
snapshot('reporter_options_as_object', normalizeModuleOptions(options))
})

it('converts specs array', () => {
const options = {
spec: [
'a', 'b', 'c',
],
}
snapshot('spec_as_array', normalizeModuleOptions(options))
})

it('does not convert spec when string', () => {
const options = {
spec: 'x,y,z',
}
snapshot('spec_as_string', normalizeModuleOptions(options))
})
})

context('.supportsColor', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ describe "src/cy/commands/querying", ->
cy.get("#list").then ($list) ->
expect($list.get(0)).to.eq list.get(0)

it "FLAKY retries finding elements until something is found", ->
it.skip "FLAKY retries finding elements until something is found", ->
missingEl = $("<div />", id: "missing-el")

## wait until we're ALMOST about to time out before
Expand Down
5 changes: 5 additions & 0 deletions packages/server/lib/cypress.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ module.exports = {
mode = options.mode
options = _.omit(options, "mode")

## TODO: temporary hack to get this commit in
## before spec parallelization lands
if _.isArray(options.spec)
options.spec = options.spec[0]

@startInMode(mode, options)

startInMode: (mode, options) ->
Expand Down
26 changes: 21 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,14 @@ 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 +146,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
14 changes: 8 additions & 6 deletions packages/server/test/integration/cypress_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ TYPICAL_BROWSERS = [

describe "lib/cypress", ->
require("mocha-banner").register()

beforeEach ->
@timeout(5000)

Expand Down Expand Up @@ -278,14 +278,16 @@ describe "lib/cypress", ->
## still not projects
expect(projects.length).to.eq(0)

it "runs project by specific spec and exits with status 0", ->
cypress.start(["--run-project=#{@todosPath}", "--spec=tests/test2.coffee"])
it "runs project by relative spec and exits with status 0", ->
relativePath = path.relative(cwd(), @todosPath)

cypress.start(["--run-project=#{@todosPath}", "--spec=#{relativePath}/tests/test2.coffee"])
.then =>
expect(browsers.open).to.be.calledWithMatch("electron", {url: "http://localhost:8888/__/#/tests/integration/test2.coffee"})
@expectExitWith(0)

it "runs project by specific spec with default configuration", ->
cypress.start(["--run-project=#{@idsPath}", "--spec=cypress/integration/bar.js", "--config", "port=2020"])
cypress.start(["--run-project=#{@idsPath}", "--spec=#{@idsPath}/cypress/integration/bar.js", "--config", "port=2020"])
.then =>
expect(browsers.open).to.be.calledWithMatch("electron", {url: "http://localhost:2020/__/#/tests/integration/bar.js"})
@expectExitWith(0)
Expand Down Expand Up @@ -521,7 +523,7 @@ describe "lib/cypress", ->
it "logs error and exits when spec file was specified and does not exist", ->
cypress.start(["--run-project=#{@todosPath}", "--spec=path/to/spec"])
.then =>
@expectExitWithErr("SPEC_FILE_NOT_FOUND", "#{@todosPath}/path/to/spec")
@expectExitWithErr("SPEC_FILE_NOT_FOUND", "#{cwd()}/path/to/spec")

it "logs error and exits when spec absolute file was specified and does not exist", ->
cypress.start(["--run-project=#{@todosPath}", "--spec=#{@todosPath}/tests/path/to/spec"])
Expand Down Expand Up @@ -598,7 +600,7 @@ describe "lib/cypress", ->
fs.unlink(statePath)

it "saves project state", ->
cypress.start(["--run-project=#{@todosPath}", "--spec=tests/test2.coffee"])
cypress.start(["--run-project=#{@todosPath}", "--spec=#{@todosPath}/tests/test2.coffee"])
.then =>
@expectExitWith(0)
.then ->
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(options.project, "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
Loading

0 comments on commit 001a310

Please sign in to comment.