From 29fc118f6a0e4fea900045c625e17a0fd2f8dc46 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 14 Jul 2020 20:46:10 -0500 Subject: [PATCH] [cypress.rake] Add cypress.env.json generation Cypress handles environment variables itself, so `process.env` isn't available. As a result, "Option 2" is what is being done here: https://docs.cypress.io/guides/guides/environment-variables.html#Option-2-cypress-env-json which makes sure a default config file exists as part of the `setup` step for running the specs. A `file` task is used here to only run it if the file doesn't exist, but makes it a dependency for the `:setup` task. By default, screenshots are disabledin CI, but enabled in dev. The cypress.env.json file is also ignored by git, so local edits can happen and can be customized for each developer. On CI, it will just be copied in and the defaults from `.cypress.ci.env.json` will be what is conifigured in that environment. --- .cypress.ci.env.json | 3 +++ .cypress.dev.env.json | 3 +++ .gitignore | 1 + cypress/support/index.js | 8 +++----- lib/tasks/manageiq/cypress.rake | 14 +++++++++++++- 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .cypress.ci.env.json create mode 100644 .cypress.dev.env.json diff --git a/.cypress.ci.env.json b/.cypress.ci.env.json new file mode 100644 index 000000000000..1a79ceb11634 --- /dev/null +++ b/.cypress.ci.env.json @@ -0,0 +1,3 @@ +{ + "disable_screenshots": "true" +} diff --git a/.cypress.dev.env.json b/.cypress.dev.env.json new file mode 100644 index 000000000000..18c5362392c4 --- /dev/null +++ b/.cypress.dev.env.json @@ -0,0 +1,3 @@ +{ + "disable_screenshots": "false" +} diff --git a/.gitignore b/.gitignore index bf1f7099e4e1..6c5c1acb42af 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ config/webpack/paths.json *.sw[po] cypress/screenshots cypress/videos +cypress.env.json diff --git a/cypress/support/index.js b/cypress/support/index.js index 94ef7ce3f798..f9d539cd7c84 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -19,8 +19,6 @@ import './commands' // Alternatively you can use CommonJS syntax: // require('./commands') -const { env } = require('process') - -if (env.TRAVIS || env.CI) { - Cypress.Screenshot.defaults({screenshotOnRunFailure: false}) -} +Cypress.Screenshot.defaults({ + screenshotOnRunFailure: !(Cypress.env('disable_screenshots') == 'true') +}) diff --git a/lib/tasks/manageiq/cypress.rake b/lib/tasks/manageiq/cypress.rake index 32c228eee3cc..e639be1afa6a 100644 --- a/lib/tasks/manageiq/cypress.rake +++ b/lib/tasks/manageiq/cypress.rake @@ -1,3 +1,5 @@ +CYPRESS_ENV_FILE = File.expand_path(File.join("..", "..", "..", "cypress.env.json"), __dir__) unless defined?(CYPRESS_ENV_FILE) + namespace :cypress do desc "Run cypress tests" task :ui => 'ui:run' @@ -25,11 +27,21 @@ namespace :cypress do Rake::Task["#{app_prefix}integration:stop_server"].invoke end - task :setup do |rake_task| + task :setup => CYPRESS_ENV_FILE do |rake_task| app_prefix = rake_task.name.chomp('cypress:ui:setup') Rake::Task["#{app_prefix}integration:start_server"].invoke end + file CYPRESS_ENV_FILE do |cypress_env_json_file| + unless File.exist?(CYPRESS_ENV_FILE) + cypress_dir = File.dirname(CYPRESS_ENV_FILE) + cypress_env_example = ".cypress.dev.env.json" + cypress_env_example = ".cypress.ci.env.json" if ENV['CI'] || ENV['TRAVIS'] + + cp File.join(cypress_dir, cypress_env_example), cypress_env_json_file.name + end + end + desc "Run cypress tests in 'development' mode" task :dev => 'dev:run'