woocommerce/packages/js/e2e-environment/utils/test-config.js

191 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-07-24 13:12:07 +00:00
const path = require( 'path' );
const fs = require( 'fs' );
2020-10-21 17:25:13 +00:00
const getAppRoot = require( './app-root' );
const appPath = getAppRoot();
/**
* Resolve a local E2E file.
*
* @param {string} filename Filename to append to the path.
* @return {string}
*/
const resolveLocalE2ePath = ( filename = '' ) => {
const { WC_E2E_FOLDER } = process.env;
const localPath = `${WC_E2E_FOLDER}/tests/e2e/${filename}`;
const resolvedPath = path.resolve(
appPath,
localPath.indexOf( '/' ) == 0 ? localPath.slice( 1 ) : localPath
);
return resolvedPath;
};
/**
* Resolve a package name installable by npm install.
*
* @param {string} packageName Name of the installed package.
* @param {boolean} allowRecurse Allow a recursive call. Default true.
* @return {object}
*/
const resolvePackage = ( packageName, allowRecurse = true ) => {
const resolvedPackage = {};
try {
2021-12-08 17:47:30 +00:00
const resolvedPath = path.dirname( require.resolve( packageName ) );
const buildPaths = [ 'dist', 'build', 'build-modules' ];
// Remove build paths from the resolved path.
let resolvedParts = resolvedPath.split( path.sep );
for ( let rp = resolvedParts.length - 1; rp >= 0; rp-- ) {
if ( buildPaths.includes( resolvedParts[ rp ] ) ) {
resolvedParts = resolvedParts.slice( 0, -1 );
} else {
break;
}
}
resolvedPackage.path = resolvedParts.join( path.sep );
resolvedPackage.name = packageName;
} catch ( e ) {
// Package name installed is not the package name.
resolvedPackage.path = '';
resolvedPackage.name = '';
}
// Attempt to find the package through the project package lock file.
if ( ! resolvedPackage.path.length && allowRecurse ) {
const packageLockPath = path.resolve( appPath, 'package-lock.json' );
const packageLockContent = fs.readFileSync( packageLockPath );
2021-11-26 21:08:27 +00:00
const { dependencies } = JSON.parse( packageLockContent );
2021-11-26 21:08:27 +00:00
for ( const [ key, value ] of Object.entries( dependencies ) ) {
if ( value.version.indexOf( packageName ) == 0 ) {
resolvedPackage = resolvePackage( key, false );
break;
}
}
}
return resolvedPackage;
};
/**
* Resolve a file in a package.
*
* @param {string} filename Filename to append to the path.
* @param {string} packageName Name of the installed package. Default @woocommerce/e2e-environment.
* @return {string}
*/
const resolvePackagePath = ( filename, packageName = '' ) => {
let packagePath;
if ( ! packageName.length ) {
packagePath = path.resolve( __dirname, '../' );
} else {
2021-11-26 21:08:27 +00:00
const pkg = resolvePackage( packageName );
packagePath = pkg.path;
}
const resolvedPath = path.resolve(
packagePath,
filename.indexOf( '/' ) == 0 ? filename.slice( 1 ) : filename
);
return resolvedPath;
};
/**
* Removes duplicates from a file path in order to allow backwards support for executing tests commands
* that prepend `plugins/woocommerce` and/or `tests/e2e.
*
* @param {string} filePath Path to a specific test file
* @param {Array} exclude An array of strings that won't be removed in the event that duplicates exist
* @return {string}
*/
const removePathDuplicates = ( filePath, exclude = [] ) => {
const pathArray = resolveLocalE2ePath( filePath ).split( '/' );
return pathArray
.filter( ( element, index, arr ) => {
return (
arr.indexOf( element ) === index ||
exclude.indexOf( element ) !== -1
);
} )
.join( '/' );
};
// Copy local test configuration file if it exists.
const localTestConfigFile = resolveLocalE2ePath( 'config/default.json' );
const defaultConfigFile = resolvePackagePath( 'config/default/default.json' );
const testConfigFile = resolvePackagePath( 'config/default.json' );
if ( fs.existsSync( localTestConfigFile ) ) {
fs.copyFileSync( localTestConfigFile, testConfigFile );
} else {
fs.copyFileSync( defaultConfigFile, testConfigFile );
}
2020-07-24 13:12:07 +00:00
2021-04-01 20:17:34 +00:00
/**
* Get test container configuration.
*
* @return {any}
2021-04-01 20:17:34 +00:00
*/
2020-07-24 13:12:07 +00:00
const getTestConfig = () => {
const rawTestConfig = fs.readFileSync( testConfigFile );
2021-04-01 20:17:34 +00:00
const config = require( 'config' );
const url = config.get( 'url' );
const users = config.get( 'users' );
2020-07-24 13:12:07 +00:00
2021-04-01 20:17:34 +00:00
// Support for environment variable overrides.
const testConfig = JSON.parse( rawTestConfig );
2021-04-01 20:17:34 +00:00
if ( url ) {
testConfig.url = url;
}
if ( users ) {
if ( users.admin ) {
testConfig.users.admin = users.admin;
}
if ( users.customer ) {
testConfig.users.customer = users.customer;
}
}
const testPort = testConfig.url.match( /[0-9]+/ );
testConfig.baseUrl = testConfig.url.substr( 0, testConfig.url.length - 1 );
2020-08-21 16:25:30 +00:00
if ( Array.isArray( testPort ) ) {
testConfig.port = testPort[ 0 ] ? testPort[ 0 ] : '8084';
} else {
testConfig.port = '';
}
2020-07-24 13:12:07 +00:00
return testConfig;
};
/**
* Get user account settings for Docker configuration.
*/
const getAdminConfig = () => {
const testConfig = getTestConfig();
const adminConfig = {
WORDPRESS_LOGIN: testConfig.users.admin.username
? testConfig.users.admin.username
: 'admin',
WORDPRESS_PASSWORD: testConfig.users.admin.password
? testConfig.users.admin.password
: 'password',
WORDPRESS_EMAIL: testConfig.users.admin.email
? testConfig.users.admin.email
: 'admin@woocommercecoree2etestsuite.com',
};
return adminConfig;
};
module.exports = {
getTestConfig,
getAdminConfig,
resolveLocalE2ePath,
resolvePackage,
resolvePackagePath,
removePathDuplicates,
};