diff --git a/NOTICE b/NOTICE index 710774660..103a82ab2 100644 --- a/NOTICE +++ b/NOTICE @@ -27,4 +27,8 @@ Licensed under the MIT License scenarios-original.tmpl is taken from https://github.com/WasiqB/multiple-cucumber-html-reporter which is: Copyright (c) 2023 Wasiq Bhamla +Licensed under the MIT License + +Scripts/run.js uses code provided by cross-spawn taken from https://github.com/moxystudio/node-cross-spawn +Copyright (c) 2018 Made With MOXY Lda hello@moxy.studio Licensed under the MIT License \ No newline at end of file diff --git a/Scripts/run.js b/Scripts/run.js new file mode 100644 index 000000000..d2825c339 --- /dev/null +++ b/Scripts/run.js @@ -0,0 +1,104 @@ +const spawn = require('cross-spawn'); +const { v4: uuidv4 } = require('uuid'); + +// Reading first parameter from the scripts to call function +const functionName = process.argv[2]; +const params = process.argv.slice(3).join(' '); + +// Creating UUID +function generateUUID() { + return uuidv4(); +} + +// Function to extract value of params that contain spaces +function modifyParams(params) { + const envSectionMatch = params.match(/--env\s+(.*?)(?=\s+--|$)/); + const envSection = envSectionMatch ? envSectionMatch[1] : ''; + const paramValuePairs = envSection.split(','); + + let modifiedParams = params; + for (const pair of paramValuePairs) { + if (pair.includes(' ')) { + const [key, value] = pair.split('='); + process.env[`CYPRESS_${key}`] = value; + modifiedParams = modifiedParams.replace(pair + ',', '').replace(',' + pair, ''); + } + } + return modifiedParams; +} + +// Function to check if it's a combined test run +function isCombinedTestRun(params) { + const specValueMatch = params.match(/--spec\s+([^ ]*)/); + const specValue = specValueMatch ? specValueMatch[1] : ''; + return specValue === '*' || specValue.includes(','); +} + +const isCombinedTest = isCombinedTestRun(params); +process.env.CYPRESS_isCombinedTestRun = isCombinedTest; + +// Extract jobId from the parameters +let jobId = ''; +const processingEnvArgs = params.includes('--env'); +if (processingEnvArgs) { + const envArgs = params.match(/--env\s+(.*?)(?=\s+--|$)/)[1].split(','); + for (const envArg of envArgs) { + if (envArg.startsWith('jobId=')) { + jobId = envArg.split('=')[1]; + break; + } + } +} + +// If jobId is not found in the parameters, generate a new one +if (!jobId) { + jobId = generateUUID(); +} + +process.env.CYPRESS_jobId = jobId; + +// Function to execute cypress run +function run() { + const args = ['run', '--e2e', ...modifyParams(params).split(' ')]; + console.log(`[Running cypress command: cypress ${args.join(' ')}]`); + + const cypressProcess = spawn('cypress', args, { stdio: 'inherit' }); + + cypressProcess.on('error', (error) => { + console.error(`Error: ${error.message}`); + }); + + cypressProcess.on('close', (code) => { + if (code !== 0) { + console.error(`Cypress process exited with code ${code}`); + } + }); +} + +// Function to open Cypress without report options +function open() { + const command = 'cypress'; + const args = ['open', '--e2e', ...modifyParams(params).split(' ')]; + console.log(`[Running cypress command: ${command} ${args.join(' ')}]`); + + const cypressProcess = spawn(command, args, { stdio: 'inherit' }); + + cypressProcess.on('error', (error) => { + console.error(`Error: ${error.message}`); + }); + + cypressProcess.on('close', (code) => { + if (code !== 0) { + console.error(`Cypress process exited with code ${code}`); + } + }); +} + +// Calling function based on name +if (functionName === 'run') { + run(); +} else if (functionName === 'open') { + open(); +} else { + console.error('Invalid function name'); +} diff --git a/package.json b/package.json index 4a13474ae..8817c6948 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "Firebolt Certification Test Suite", "main": "index.js", "scripts": { - "cy:open": "node ./Scripts/checkDefaultConfig.js && yarn cache clean && sh ./Scripts/run.sh open", - "cy:run": "node ./Scripts/checkDefaultConfig.js && yarn cache clean && bash ./Scripts/run.sh run", + "cy:open": "node ./Scripts/checkDefaultConfig.js && yarn cache clean && node ./Scripts/run.js open", + "cy:run": "node ./Scripts/checkDefaultConfig.js && yarn cache clean && node ./Scripts/run.js run", "softRefresh": "yarn install && yarn upgrade", "hardRefresh": "rm -rf node_modules && yarn cache clean && yarn softRefresh", "copyCukeReportTemplate": "node ./Scripts/copyCukeReportTemplate.js", @@ -32,6 +32,7 @@ "axios": "^1.3.4", "configModule": "./defaultModule", "cucumber-json-report-formatter": "^0.1.4", + "cross-spawn": "^7.0.3", "cypress": "^12.0.0", "esbuild": "^0.17.0", "flatted": "^3.3.1", @@ -67,4 +68,4 @@ "enabled": true } } -} +} \ No newline at end of file