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' );
|
2020-07-29 21:57:59 +00:00
|
|
|
|
|
|
|
const appPath = getAppRoot();
|
2021-11-17 13:50:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2022-01-12 23:04:36 +00:00
|
|
|
const localPath = `${ WC_E2E_FOLDER }/tests/e2e/${ filename }`;
|
2021-11-17 13:50:40 +00:00
|
|
|
const resolvedPath = path.resolve(
|
|
|
|
appPath,
|
|
|
|
localPath.indexOf( '/' ) == 0 ? localPath.slice( 1 ) : localPath
|
|
|
|
);
|
|
|
|
|
|
|
|
return resolvedPath;
|
2021-11-26 19:14:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2022-01-12 23:04:36 +00:00
|
|
|
* @return {Object}
|
2021-11-26 19:14:49 +00:00
|
|
|
*/
|
|
|
|
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 );
|
2021-11-26 19:14:49 +00:00
|
|
|
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 19:14:49 +00:00
|
|
|
|
2021-11-26 21:08:27 +00:00
|
|
|
for ( const [ key, value ] of Object.entries( dependencies ) ) {
|
2021-11-26 19:14:49 +00:00
|
|
|
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;
|
2021-11-26 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const resolvedPath = path.resolve(
|
|
|
|
packagePath,
|
|
|
|
filename.indexOf( '/' ) == 0 ? filename.slice( 1 ) : filename
|
|
|
|
);
|
|
|
|
|
|
|
|
return resolvedPath;
|
|
|
|
};
|
2021-11-17 13:50:40 +00:00
|
|
|
|
2022-01-12 19:00:00 +00:00
|
|
|
/**
|
2022-01-13 15:52:15 +00:00
|
|
|
* Resolves the path a single E2E test
|
2022-01-12 19:00:00 +00:00
|
|
|
*
|
|
|
|
* @param {string} filePath Path to a specific test file
|
2022-01-13 15:52:15 +00:00
|
|
|
* @param {Array} exclude An array of directories that won't be removed in the event that duplicates exist.
|
2022-01-12 19:00:00 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2022-01-13 15:52:15 +00:00
|
|
|
const resolveSingleE2EPath = ( filePath, exclude = [ 'woocommerce' ] ) => {
|
2022-01-12 23:04:36 +00:00
|
|
|
const { SMOKE_TEST_URL } = process.env;
|
|
|
|
let prunedPath;
|
|
|
|
|
2022-01-12 23:27:35 +00:00
|
|
|
// Removes 'plugins/woocommerce/' from path only for tests against a smoke test site.
|
2022-01-12 23:04:36 +00:00
|
|
|
if ( SMOKE_TEST_URL ) {
|
|
|
|
prunedPath = filePath.replace( 'plugins/woocommerce/', '' );
|
|
|
|
} else {
|
|
|
|
prunedPath = filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathArray = resolveLocalE2ePath( prunedPath ).split( '/' );
|
2022-01-13 15:52:15 +00:00
|
|
|
|
|
|
|
// removes duplicate directories from the path
|
2022-01-12 19:00:00 +00:00
|
|
|
return pathArray
|
|
|
|
.filter( ( element, index, arr ) => {
|
|
|
|
return (
|
|
|
|
arr.indexOf( element ) === index ||
|
|
|
|
exclude.indexOf( element ) !== -1
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
.join( '/' );
|
|
|
|
};
|
|
|
|
|
2021-11-17 13:50:40 +00:00
|
|
|
// Copy local test configuration file if it exists.
|
|
|
|
const localTestConfigFile = resolveLocalE2ePath( 'config/default.json' );
|
2021-11-26 19:14:49 +00:00
|
|
|
const defaultConfigFile = resolvePackagePath( 'config/default/default.json' );
|
2022-01-12 23:04:36 +00:00
|
|
|
const testConfigFile = resolvePackagePath( 'config/default.json' );
|
2021-10-28 19:32:31 +00:00
|
|
|
|
2020-07-29 21:57:59 +00:00
|
|
|
if ( fs.existsSync( localTestConfigFile ) ) {
|
2021-10-28 19:32:31 +00:00
|
|
|
fs.copyFileSync( localTestConfigFile, testConfigFile );
|
2021-02-08 20:12:10 +00:00
|
|
|
} else {
|
2021-10-28 19:32:31 +00:00
|
|
|
fs.copyFileSync( defaultConfigFile, testConfigFile );
|
2020-07-29 21:57:59 +00:00
|
|
|
}
|
2020-07-24 13:12:07 +00:00
|
|
|
|
2021-04-01 20:17:34 +00:00
|
|
|
/**
|
|
|
|
* Get test container configuration.
|
2021-10-28 19:32:31 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2021-10-28 19:32:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:32:31 +00:00
|
|
|
const testPort = testConfig.url.match( /[0-9]+/ );
|
2020-09-23 19:22:15 +00:00
|
|
|
testConfig.baseUrl = testConfig.url.substr( 0, testConfig.url.length - 1 );
|
2020-08-21 16:25:30 +00:00
|
|
|
if ( Array.isArray( testPort ) ) {
|
2021-10-28 19:32:31 +00:00
|
|
|
testConfig.port = testPort[ 0 ] ? testPort[ 0 ] : '8084';
|
2020-08-19 12:57:14 +00:00
|
|
|
} else {
|
|
|
|
testConfig.port = '';
|
|
|
|
}
|
2020-07-24 13:12:07 +00:00
|
|
|
return testConfig;
|
|
|
|
};
|
|
|
|
|
2021-01-19 15:02:08 +00:00
|
|
|
/**
|
|
|
|
* Get user account settings for Docker configuration.
|
|
|
|
*/
|
|
|
|
const getAdminConfig = () => {
|
|
|
|
const testConfig = getTestConfig();
|
|
|
|
const adminConfig = {
|
2021-10-28 19:32:31 +00:00
|
|
|
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',
|
2021-01-19 15:02:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return adminConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getTestConfig,
|
|
|
|
getAdminConfig,
|
2021-11-17 13:50:40 +00:00
|
|
|
resolveLocalE2ePath,
|
2021-11-26 19:14:49 +00:00
|
|
|
resolvePackage,
|
|
|
|
resolvePackagePath,
|
2022-01-13 15:52:15 +00:00
|
|
|
resolveSingleE2EPath,
|
2021-01-19 15:02:08 +00:00
|
|
|
};
|