Skip to content

Commit

Permalink
Pass projectRoot and configFile to plugins file through config (#6317)
Browse files Browse the repository at this point in the history
* decaffeinate: Rename index.coffee from .coffee to .js

* decaffeinate: Convert index.coffee to JS

* decaffeinate: Run post-processing cleanups on index.coffee

* refactor decaffeinated plugins/index.js

* decaffeinate: Rename 3_plugins_spec.coffee from .coffee to .js

* decaffeinate: Convert 3_plugins_spec.coffee to JS

* decaffeinate: Run post-processing cleanups on 3_plugins_spec.coffee

* fix wrongly removed return

* refactor e2e plugins spec, update snapshot

* pass env argument to plugins file

* decaffeinate: Rename index_spec.coffee from .coffee to .js

* decaffeinate: Convert index_spec.coffee to JS

* decaffeinate: Run post-processing cleanups on index_spec.coffee

* update plugins tests

* update scaffold snapshot

* add back server test script and document running individual tests

* add projectRoot and configFile directly to config

* normalize browsers in snapshot

* add types for configFile and projectRoot

* fix linting issues

* return return

* Merge

* remove file

* remove unnecessary returns
  • Loading branch information
chrisbreiding committed Feb 28, 2020
1 parent e2ea5bf commit 7783217
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 495 deletions.
13 changes: 12 additions & 1 deletion cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ declare namespace Cypress {
type RequestBody = string | object
type ViewportOrientation = "portrait" | "landscape"
type PrevSubject = "optional" | "element" | "document" | "window"
type PluginConfig = (on: PluginEvents, config: ConfigOptions) => void | Partial<ConfigOptions> | Promise<Partial<ConfigOptions>>
type PluginConfig = (on: PluginEvents, config: PluginConfigOptions) => void | Partial<ConfigOptions> | Promise<Partial<ConfigOptions>>

interface CommandOptions {
prevSubject: boolean | PrevSubject | PrevSubject[]
Expand Down Expand Up @@ -2282,6 +2282,17 @@ declare namespace Cypress {
firefoxGcInterval: Nullable<number | { runMode: Nullable<number>, openMode: Nullable<number> }>
}

interface PluginConfigOptions extends ConfigOptions {
/**
* Absolute path to the config file (default: <projectRoot>/cypress.json) or false
*/
configFile: string | false
/**
* Absolute path to the root of the project
*/
projectRoot: string
}

interface DebugOptions {
verbose: boolean
}
Expand Down
4 changes: 3 additions & 1 deletion cli/types/tests/plugins-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const pluginConfig: Cypress.PluginConfig = (on, config) => {}

// allows synchronous returns
const pluginConfig2: Cypress.PluginConfig = (on, config) => {
config // $ExpectType ConfigOptions
config // $ExpectType PluginConfigOptions
config.baseUrl // $ExpectType: string
config.configFile // $ExpectType: string | false
config.projectRoot // $ExpectType: string

on('before:browser:launch', (browser, options) => {
browser.displayName // $ExpectType string
Expand Down
404 changes: 0 additions & 404 deletions packages/server/__snapshots__/3_plugins_spec.coffee.js

This file was deleted.

18 changes: 9 additions & 9 deletions packages/server/__snapshots__/3_plugins_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports['e2e plugins passes 1'] = `
exports['e2e plugins passes with working preprocessor 1'] = `
====================================================================================================
Expand Down Expand Up @@ -58,7 +58,7 @@ exports['e2e plugins passes 1'] = `
`

exports['e2e plugins fails 1'] = `
exports['e2e plugins fails with async error 1'] = `
====================================================================================================
Expand Down Expand Up @@ -175,13 +175,6 @@ exports['e2e plugins can modify config from plugins 1'] = `
✔ All specs passed! XX:XX 2 2 - - -
`

exports['e2e plugins catches invalid viewportWidth returned from plugins 1'] = `
An invalid configuration value returned from the plugins file: \`cypress/plugins/index.coffee\`
Expected \`viewportWidth\` to be a number. Instead the value was: \`"foo"\`
`

exports['e2e plugins catches invalid browsers list returned from plugins 1'] = `
Expand Down Expand Up @@ -402,3 +395,10 @@ exports['e2e plugins calls after:screenshot for cy.screenshot() and failure scre
`

exports['e2e plugins catches invalid viewportWidth returned from plugins 1'] = `
An invalid configuration value returned from the plugins file: \`cypress/plugins/index.coffee\`
Expected \`viewportWidth\` to be a number. Instead the value was: \`"foo"\`
`
17 changes: 10 additions & 7 deletions packages/server/lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ const init = (config, options) => {
handler(ipc)
}

_.extend(config, {
projectRoot: options.projectRoot,
configFile: options.configFile,
})

ipc.send('load', config)

ipc.on('loaded', (newCfg, registrations) => {
_.omit(config, 'projectRoot', 'configFile')

_.each(registrations, (registration) => {
debug('register plugins process event', registration.event, 'with id', registration.eventId)

Expand Down Expand Up @@ -105,9 +112,7 @@ const init = (config, options) => {

const handleError = (err) => {
debug('plugins process error:', err.stack)
if (!pluginsProcess) {
return // prevent repeating this in case of multiple errors
}
if (!pluginsProcess) return // prevent repeating this in case of multiple errors

killPluginsProcess()
err = errors.get('PLUGINS_ERROR', err.annotated || err.stack || err.message)
Expand All @@ -116,11 +121,9 @@ const init = (config, options) => {
return options.onError(err)
}

const handleWarning = function (warningErr) {
const handleWarning = (warningErr) => {
debug('plugins process warning:', warningErr.stack)
if (!pluginsProcess) {
return // prevent repeating this in case of multiple warnings
}
if (!pluginsProcess) return // prevent repeating this in case of multiple warnings

return options.onWarning(warningErr)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/server/lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class Project extends EE {
cfg = config.whitelist(cfg)

return plugins.init(cfg, {
projectRoot: this.projectRoot,
configFile: settings.pathToConfigFile(this.projectRoot, options),
onError (err) {
debug('got plugins error', err.stack)

Expand Down
47 changes: 44 additions & 3 deletions packages/server/test/e2e/3_plugins_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const e2e = require('../support/helpers/e2e')
const Fixtures = require('../support/helpers/fixtures')

const e2eProject = Fixtures.projectPath('e2e')
const pluginExtension = Fixtures.projectPath('plugin-extension')
const pluginConfig = Fixtures.projectPath('plugin-config')
const pluginFilterBrowsers = Fixtures.projectPath('plugin-filter-browsers')
Expand All @@ -14,10 +15,10 @@ const pluginReturnsBadConfig = Fixtures.projectPath('plugin-returns-bad-config')
const pluginReturnsEmptyBrowsersList = Fixtures.projectPath('plugin-returns-empty-browsers-list')
const pluginReturnsInvalidBrowser = Fixtures.projectPath('plugin-returns-invalid-browser')

describe('e2e plugins', () => {
describe('e2e plugins', function () {
e2e.setup()

it('passes', function () {
it('passes with working preprocessor', function () {
return e2e.exec(this, {
spec: 'app_spec.coffee',
project: workingPreprocessor,
Expand All @@ -26,7 +27,7 @@ describe('e2e plugins', () => {
})
})

it('fails', function () {
it('fails with async error', function () {
return e2e.exec(this, {
spec: 'app_spec.coffee',
project: pluginsAsyncError,
Expand Down Expand Up @@ -121,4 +122,44 @@ describe('e2e plugins', () => {
expectedExitCode: 1,
})
})

describe('projectRoot and configFile', function () {
it('passes projectRoot and default configFile to plugins function', function () {
return e2e.exec(this, {
spec: 'plugins_config_extras_spec.js',
config: {
env: {
projectRoot: e2eProject,
configFile: path.join(e2eProject, 'cypress.json'),
},
},
})
})

it('passes custom configFile to plugins function', function () {
return e2e.exec(this, {
spec: 'plugins_config_extras_spec.js',
configFile: 'cypress-alt.json',
config: {
env: {
projectRoot: e2eProject,
configFile: path.join(e2eProject, 'cypress-alt.json'),
},
},
})
})

it('passes false configFile to plugins function', function () {
return e2e.exec(this, {
spec: 'plugins_config_extras_spec.js',
configFile: 'false',
config: {
env: {
projectRoot: e2eProject,
configFile: false,
},
},
})
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('plugins config extras', () => {
it('has correct projectRoot', () => {
cy.task('get:config:value', 'projectRoot')
.should('not.be.undefined')
.and('equal', Cypress.env('projectRoot'))
})

it('has correct configFile', () => {
cy.task('get:config:value', 'configFile')
.should('not.be.undefined')
.and('equal', Cypress.env('configFile'))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,9 @@ module.exports = (on, config) => {
'get:browser:args' () {
return browserArgs
},

'get:config:value' (key) {
return config[key]
},
})
}
4 changes: 4 additions & 0 deletions packages/server/test/support/helpers/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ const e2e = {
args.push(`--tag=${options.tag}`)
}

if (options.configFile) {
args.push(`--config-file=${options.configFile}`)
}

return args
},

Expand Down
5 changes: 4 additions & 1 deletion packages/server/test/unit/browsers/browsers_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ browsers = require("#{root}../lib/browsers")
utils = require("#{root}../lib/browsers/utils")
snapshot = require('snap-shot-it')

normalizeBrowsers = (message) ->
message.replace(/(found are: ).*/, "$1chrome, firefox, electron")

describe "lib/browsers/index", ->
context ".getBrowserInstance", ->
it "returns instance", ->
Expand Down Expand Up @@ -40,7 +43,7 @@ describe "lib/browsers/index", ->
expect(browsers.ensureAndGetByNameOrPath("browserNotGonnaBeFound"))
.to.be.rejectedWith({ type: 'BROWSER_NOT_FOUND_BY_NAME' })
.then (err) ->
snapshot(err.message)
snapshot(normalizeBrowsers(err.message))

it "throws a special error when canary is passed", ->
sinon.stub(utils, "getBrowsers").resolves([
Expand Down
Loading

4 comments on commit 7783217

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7783217 Feb 28, 2020

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.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/linux-x64/circle-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-269109/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/circle-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-269094/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7783217 Feb 28, 2020

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.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/darwin-x64/circle-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-269122/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/circle-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-269119/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7783217 Feb 28, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-ia32/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.1.0/win32-ia32/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-ia32/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-ia32/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7783217 Feb 28, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-x64/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.1.0/win32-x64/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-x64/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.1.0/win32-x64/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.1.0/appveyor-develop-778321786f7a19d63ddabc74806b7c2852bbfe69-31135334/cypress.tgz

Please sign in to comment.