Skip to content

Commit

Permalink
Find and use full path to percy-healthcheck script (#67)
Browse files Browse the repository at this point in the history
* Find and use full path to percy-healthcheck script.

* Make directory creation Node-8-compatible.
  • Loading branch information
anaulin authored Mar 27, 2019
1 parent 7a269ac commit 8468123
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
27 changes: 24 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {clientInfo, environmentInfo} from './environment'
import PercyAgent from '@percy/agent'
import * as path from 'path'

declare const Cypress: any
declare const cy: any
Expand All @@ -10,13 +11,12 @@ Cypress.Commands.add('percySnapshot', (name: string, options: any = {}) => {
// Use cy.exec(...) to check if percy agent is running. Ideally this would be
// done using something like cy.request(...), but that's not currently possible,
// for details, see: https://github.com/cypress-io/cypress/issues/3161
// We rely on node finding the percy-healtcheck binary and running it correctly,
// which is more cross-platform friendly than running the script directly ourselves.
const healthcheck = `node percy-healthcheck ${percyAgentClient.port}`
const healthcheck = `node ${_healthcheckPath()} ${percyAgentClient.port}`
cy.exec(healthcheck, {failOnNonZeroExit: false}).then((result: any) => {
if (result.code != 0) {
// Percy server not available, or we failed to find the healthcheck.
cy.log('[percy] Percy agent is not running. Skipping snapshots')
cy.log(`[percy] Healthcheck output: ${result.stdout}\n${result.stderr}`)
return
}

Expand All @@ -42,3 +42,24 @@ Cypress.Commands.add('percySnapshot', (name: string, options: any = {}) => {
})
})
})


// An attempt to resiliently find the path to the 'percy-healthcheck' script, and to do so
// in a cross-platform manner.
function _healthcheckPath() {
try {
// Try to resolve with respect to the install local module.
return require.resolve('@percy/cypress/dist/percy-healthcheck')
} catch {
try {
// Try to resolve relative to the current file.
return require.resolve('./percy-healthcheck')
} catch {
// Oh well. Assume it's in the local node_modules.
// It would be nice to use __dirname here, but this code is entirely executed inside of
// Cypress' unusual context, making __dirname always '/dist' for this file, which is
// unhelpful when trying to find a working filesystem path to percy-healthcheck.
return path.join('.', './node_modules/@percy/cypress/dist/percy-healthcheck')
}
}
}
5 changes: 4 additions & 1 deletion percy-healthcheck → lib/percy-healthcheck
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const healtcheck = options => {
const request = http.request(options, res => {
resolve(res.statusCode === 200)
})
request.on('error', err => reject(err))
request.on('error', (err) => {
console.log(`Healthcheck failed: ${err}`)
reject(err)
})
request.end()
})
}
Expand Down
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
"description": "Cypress client library for visual regression testing with Percy (https://percy.io).",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"percy-healthcheck": "percy-healthcheck"
},
"scripts": {
"pretest": "npm run build",
"test": "percy exec -- node run-tests.js",
"test:debug": "LOG_LEVEL=debug npm run test",
"clean": "rm -rf dist/",
"build": "npm run clean && tsc",
"build": "npm run clean && tsc && cp lib/percy-healthcheck dist/percy-healthcheck",
"prepublish": "npm run build",
"lint": "tslint -p . -t stylish --fix",
"cypress:open": "cypress open"
Expand Down
19 changes: 15 additions & 4 deletions run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ const port = process.env.PORT_NUMBER || 8000;
const spawn = require('child_process').spawn;
const platform = require('os').platform();

// Copy our healthcheck binary to the 'node_modules/.bin' directory where it will be
// Helper to make sure directories we need exist.
function ensureDirExists(dir) {
if (! fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}

// Copy our healthcheck script to node_modules, where it will be
// when this package is installed as a dependency.
const src = 'percy-healthcheck'
const src = 'lib/percy-healthcheck'
const copyDst = /^win/.test(platform)
? `${process.cwd()}\\node_modules\\.bin\\percy-healthcheck`
: `${__dirname}/node_modules/.bin/percy-healthcheck`;
? `${process.cwd()}\\node_modules\\@percy\\cypress\\dist\\percy-healthcheck`
: `${__dirname}/node_modules/@percy/cypress/dist/percy-healthcheck`;
console.log(`[run-tests] Copying ${src} to ${copyDst}`);
// The 'recursive' fs.mkdirSync() option is only available in Node >10.12.0;
// create all directories ourselves to accommodate Node 8.
ensureDirExists('./node_modules/@percy/cypress')
ensureDirExists('./node_modules/@percy/cypress/dist')
fs.copyFileSync(src, copyDst);

// We need to change the command path based on the platform they're using
Expand Down

0 comments on commit 8468123

Please sign in to comment.