2021-12-08 23:08:29 +00:00
|
|
|
/**
|
|
|
|
* External dependencies.
|
|
|
|
*/
|
|
|
|
const fs = require( 'fs' );
|
2021-12-08 23:38:45 +00:00
|
|
|
const path = require( 'path' );
|
2021-12-08 23:08:29 +00:00
|
|
|
const readlineSync = require( 'readline-sync' );
|
2021-12-08 23:38:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies.
|
|
|
|
*/
|
2021-12-08 23:08:29 +00:00
|
|
|
const { resolveLocalE2ePath, resolvePackagePath } = require( './test-config' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a path relative to the local `tests/e2e` folder.
|
|
|
|
* @param relativePath
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const createLocalE2ePath = ( relativePath ) => {
|
|
|
|
let specFolderPath = '';
|
2021-12-08 23:38:45 +00:00
|
|
|
const folders = [ `..${path.sep}..${path.sep}tests`, `..${path.sep}e2e`, relativePath ];
|
2021-12-08 23:08:29 +00:00
|
|
|
folders.forEach( ( folder ) => {
|
|
|
|
specFolderPath = resolveLocalE2ePath( folder );
|
|
|
|
if ( ! fs.existsSync( specFolderPath ) ) {
|
|
|
|
console.log( `Creating folder ${specFolderPath}` );
|
|
|
|
fs.mkdirSync( specFolderPath );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return specFolderPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the console for confirmation.
|
|
|
|
*
|
|
|
|
* @param {string} prompt Prompt for the user.
|
|
|
|
* @param {string} choices valid responses.
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const confirm = ( prompt, choices ) => {
|
|
|
|
const answer = readlineSync.keyIn( prompt, choices );
|
|
|
|
return answer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} localE2ePath Destination path
|
|
|
|
* @param {string} packageE2ePath Source path
|
|
|
|
* @param {string} packageName Source package. Default @woocommerce/e2e-environment package.
|
2022-01-26 16:27:25 +00:00
|
|
|
* @param {boolean} force Whether to override files if they exist without confirmation.
|
2021-12-08 23:08:29 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2022-01-26 16:27:25 +00:00
|
|
|
const confirmLocalCopy = ( localE2ePath, packageE2ePath, packageName = '', force = false ) => {
|
2021-12-08 23:08:29 +00:00
|
|
|
const localPath = resolveLocalE2ePath( localE2ePath );
|
|
|
|
const packagePath = resolvePackagePath( packageE2ePath, packageName );
|
|
|
|
const confirmPrompt = `${localE2ePath} already exists. Overwrite? [Y]es/[n]o: `;
|
|
|
|
|
|
|
|
let overwriteFiles;
|
2022-01-26 16:35:03 +00:00
|
|
|
if ( ! force && fs.existsSync( localPath ) ) {
|
2021-12-08 23:08:29 +00:00
|
|
|
overwriteFiles = confirm( confirmPrompt, 'ny' );
|
|
|
|
overwriteFiles = overwriteFiles.toLowerCase();
|
2022-01-26 16:35:03 +00:00
|
|
|
} else {
|
|
|
|
overwriteFiles = 'y';
|
2021-12-08 23:08:29 +00:00
|
|
|
}
|
|
|
|
if ( overwriteFiles == 'y' ) {
|
|
|
|
fs.copyFileSync( packagePath, localPath );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt for confirmation before deleting a local E2E file.
|
|
|
|
*
|
|
|
|
* @param {string} localE2ePath Relative path to local E2E file.
|
|
|
|
*/
|
|
|
|
const confirmLocalDelete = ( localE2ePath ) => {
|
|
|
|
const localPath = resolveLocalE2ePath( localE2ePath );
|
|
|
|
if ( ! fs.existsSync( localPath ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-08 23:38:45 +00:00
|
|
|
|
|
|
|
const confirmPrompt = `${localE2ePath} exists. Delete? [y]es/[n]o: `;
|
2021-12-08 23:08:29 +00:00
|
|
|
const deleteFile = confirm( confirmPrompt, 'ny' );
|
|
|
|
if ( deleteFile == 'y' ) {
|
|
|
|
fs.unlinkSync( localPath );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the install data for a tests package.
|
|
|
|
*
|
|
|
|
* @param {string} packageName npm package name
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const getPackageData = ( packageName ) => {
|
|
|
|
const packageSlug = packageName.replace( '@', '' ).replace( /\//g, '.' );
|
2021-12-08 23:38:45 +00:00
|
|
|
const installFiles = require( `${packageName}${path.sep}installFiles` );
|
2021-12-08 23:08:29 +00:00
|
|
|
|
|
|
|
return { packageSlug, ...installFiles };
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install test runner and test container defaults
|
|
|
|
*/
|
2022-02-04 15:21:57 +00:00
|
|
|
const installDefaults = ( force ) => {
|
2021-12-08 23:08:29 +00:00
|
|
|
createLocalE2ePath( 'docker' );
|
|
|
|
console.log( 'Writing tests/e2e/docker/initialize.sh' );
|
2022-01-26 16:27:25 +00:00
|
|
|
confirmLocalCopy( `docker${path.sep}initialize.sh`, `installFiles${path.sep}initialize.sh`, '', force );
|
2021-12-08 23:08:29 +00:00
|
|
|
|
|
|
|
createLocalE2ePath( 'config' );
|
|
|
|
console.log( 'Writing tests/e2e/config/jest.config.js' );
|
2022-01-26 16:27:25 +00:00
|
|
|
confirmLocalCopy( `config${path.sep}jest.config.js`, `installFiles${path.sep}jest.config.js.default`, '', force );
|
2021-12-08 23:08:29 +00:00
|
|
|
console.log( 'Writing tests/e2e/config/jest.setup.js' );
|
2022-01-26 16:27:25 +00:00
|
|
|
confirmLocalCopy( `config${path.sep}jest.setup.js`, `installFiles${path.sep}jest.setup.js.default`, '', force );
|
2021-12-08 23:08:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createLocalE2ePath,
|
|
|
|
confirm,
|
|
|
|
confirmLocalCopy,
|
|
|
|
confirmLocalDelete,
|
|
|
|
getPackageData,
|
|
|
|
installDefaults,
|
|
|
|
};
|