diff --git a/test/cypress/e2e/api/AccessListHostToggle.cy.js b/test/cypress/e2e/api/AccessListHostToggle.cy.js index ee1bee0dc..46e01fb3a 100644 --- a/test/cypress/e2e/api/AccessListHostToggle.cy.js +++ b/test/cypress/e2e/api/AccessListHostToggle.cy.js @@ -25,7 +25,7 @@ describe('Access Lists across Proxy Host disable/enable', () => { */ const request = (auth) => { const authArg = auth ? `-u '${auth.username}:${auth.password}'` : ''; - return cy.exec(`curl --noproxy '*' -s -o /dev/null -w '%{http_code}' ${authArg} http://${domain}/`) + return cy.task('exec', { command: `curl --noproxy '*' -s -o /dev/null -w '%{http_code}' ${authArg} http://${domain}/` }, { log: false }) .then((result) => { expect(result.exitCode).to.eq(0); return result.stdout.trim(); diff --git a/test/cypress/e2e/api/AccessListLocations.cy.js b/test/cypress/e2e/api/AccessListLocations.cy.js index 2ea2742ef..a30366a0f 100644 --- a/test/cypress/e2e/api/AccessListLocations.cy.js +++ b/test/cypress/e2e/api/AccessListLocations.cy.js @@ -31,7 +31,7 @@ describe('Per-path Access Lists', () => { */ const request = (path, creds) => { const auth = creds ? `-u '${creds.username}:${creds.password}'` : ''; - return cy.exec(`curl --noproxy '*' -s -w '\n%{http_code}' ${auth} http://${domain}${path}`) + return cy.task('exec', { command: `curl --noproxy '*' -s -w '\n%{http_code}' ${auth} http://${domain}${path}` }, { log: false }) .then((result) => { expect(result.exitCode).to.eq(0); const lines = result.stdout.trim().split('\n'); diff --git a/test/cypress/e2e/api/Streams.cy.js b/test/cypress/e2e/api/Streams.cy.js index 95c6dba13..0587629fa 100644 --- a/test/cypress/e2e/api/Streams.cy.js +++ b/test/cypress/e2e/api/Streams.cy.js @@ -31,7 +31,7 @@ describe('Streams', () => { }); }); - cy.exec('rm -f /test/results/testssl.json'); + cy.task('exec', { command: 'rm -f /test/results/testssl.json' }); }); it('Should be able to create TCP Stream', () => { @@ -55,7 +55,7 @@ describe('Streams', () => { expect(data).to.have.property('tcp_forwarding', true); expect(data).to.have.property('udp_forwarding', false); - cy.exec('curl --noproxy -- http://website1.example.com:1500').then((result) => { + cy.task('exec', { command: 'curl --noproxy -- http://website1.example.com:1500' }).then((result) => { expect(result.exitCode).to.eq(0); expect(result.stdout).to.contain('yay it works'); }); @@ -106,7 +106,7 @@ describe('Streams', () => { expect(data).to.have.property('tcp_forwarding', true); expect(data).to.have.property('udp_forwarding', true); - cy.exec('curl --noproxy -- http://website1.example.com:1502').then((result) => { + cy.task('exec', { command: 'curl --noproxy -- http://website1.example.com:1502' }).then((result) => { expect(result.exitCode).to.eq(0); expect(result.stdout).to.contain('yay it works'); }); @@ -166,7 +166,9 @@ describe('Streams', () => { // Check the ssl termination cy.task('log', '[testssl.sh] Running ...'); - cy.exec('/testssl/testssl.sh --quiet --add-ca="$(/bin/mkcert -CAROOT)/rootCA.pem" --jsonfile=/test/results/testssl.json website1.example.com:1503', { + cy.task('exec', { + command: '/testssl/testssl.sh --quiet --add-ca="$(/bin/mkcert -CAROOT)/rootCA.pem" --jsonfile=/test/results/testssl.json website1.example.com:1503', + }, { timeout: 120000, // 2 minutes }).then((result) => { cy.task('log', `[testssl.sh] ${result.stdout}`); diff --git a/test/cypress/plugins/index.mjs b/test/cypress/plugins/index.mjs index 6a2ff9ffb..ba0706886 100644 --- a/test/cypress/plugins/index.mjs +++ b/test/cypress/plugins/index.mjs @@ -1,5 +1,6 @@ import { plugin as cypressGrepPlugin } from "@cypress/grep/plugin"; import { SwaggerValidation } from "@jc21/cypress-swagger-validation"; +import { exec } from "node:child_process"; import chalk from "chalk"; import backendTask from "./backendApi/task.mjs"; @@ -25,6 +26,32 @@ export default (on, config) => { return null; }, }); + on("task", { + /** + * Replacement for cy.exec(), removed in Cypress 16 + * + * @param {object} options + * @param {string} options.command + * @param {bool} [options.failOnNonZeroExit] Defaults to true + * @returns {object} { exitCode, stdout, stderr } + */ + exec({ command, failOnNonZeroExit = true }) { + return new Promise((resolve, reject) => { + exec(command, { maxBuffer: 10 * 1024 * 1024 }, (error, stdout, stderr) => { + const result = { + exitCode: error ? (error.code ?? 1) : 0, + stdout: stdout.trimEnd(), + stderr: stderr.trimEnd(), + }; + if (error && failOnNonZeroExit) { + reject(new Error(`Command failed with exit code ${result.exitCode}: ${command}\n${result.stderr}`)); + return; + } + resolve(result); + }); + }); + }, + }); on("task", { getFixturesFolder() { return config.fixturesFolder; diff --git a/test/cypress/support/commands.mjs b/test/cypress/support/commands.mjs index 03c5bf224..0526cf553 100644 --- a/test/cypress/support/commands.mjs +++ b/test/cypress/support/commands.mjs @@ -33,7 +33,7 @@ Cypress.Commands.add("validateSwaggerFile", (url, savePath) => { return cy .request(url) .then((response) => cy.writeFile(savePath, response.body, { log: false })) - .then(() => cy.exec(`yarn swagger-lint '${savePath}'`, { failOnNonZeroExit: false })) + .then(() => cy.task('exec', { command: `yarn swagger-lint '${savePath}'`, failOnNonZeroExit: false })) .then((result) => cy.task('log', `Swagger Vacuum Results:\n${result.stdout || ''}`) .then(() => expect(result.exitCode).to.eq(0))); }); @@ -167,10 +167,10 @@ Cypress.Commands.add('createCustomCerts', ({domain, certFile, keyFile}) => { const fullKeyFile = `${fixturesFolder}/${keyFile}`; const cmd = `mkcert -cert-file="${fullCertFile}" -key-file="${fullKeyFile}" "${domain}"`; cy.log(`Creating custom certs with command: ${cmd}`); - return cy.exec(cmd).then((result) => { + return cy.task('exec', { command: cmd }).then((result) => { cy.log(`mkcert output:\n${JSON.stringify(result)}`); expect(result.exitCode).to.eq(0); - return cy.exec('mkcert -install').then((result2) => { + return cy.task('exec', { command: 'mkcert -install' }).then((result2) => { cy.log(`mkcert install output:\n${JSON.stringify(result2)}`); expect(result2.exitCode).to.eq(0); }).then(() => {