add resolvePackage(), resolvePackagePath() utilities

This commit is contained in:
Ron Rennick 2021-11-26 15:14:49 -04:00
parent 6c197cc743
commit a21af3d3c6
6 changed files with 81 additions and 25 deletions

View File

@ -60,10 +60,10 @@ The E2E environment uses Jest as a test runner. Extending the base config is nec
```js
const path = require( 'path' );
const { useE2EJestConfig } = require( '@woocommerce/e2e-environment' );
const { useE2EJestConfig, resolveLocalE2ePath } = require( '@woocommerce/e2e-environment' );
const jestConfig = useE2EJestConfig( {
roots: [ path.resolve( __dirname, '../specs' ) ],
roots: [ resolveLocalE2ePath( 'specs' ) ],
} );
module.exports = jestConfig;

View File

@ -11,6 +11,7 @@ const {
getAppName,
getTestConfig,
resolveLocalE2ePath,
resolvePackagePath,
} = require( '../utils' );
const dockerArgs = [];
@ -63,7 +64,7 @@ if ( appPath ) {
if ( fs.existsSync( appInitFile ) ) {
fs.copyFileSync(
appInitFile,
path.resolve( __dirname, '../docker/wp-cli/initialize.sh' )
resolvePackagePath( 'docker/wp-cli/initialize.sh' )
);
console.log( 'Initializing ' + appInitFile );
}
@ -90,7 +91,7 @@ if ( ! process.env.WORDPRESS_URL ) {
}
// Ensure that the first Docker compose file loaded is from our local env.
dockerArgs.unshift( '-f', path.resolve( __dirname, '../docker-compose.yaml' ) );
dockerArgs.unshift( '-f', resolvePackagePath( 'docker-compose.yaml' ) );
const dockerProcess = spawnSync( 'docker-compose', dockerArgs, {
stdio: 'inherit',

View File

@ -4,7 +4,11 @@ const { spawnSync } = require( 'child_process' );
const program = require( 'commander' );
const path = require( 'path' );
const fs = require( 'fs' );
const { getAppRoot, resolveLocalE2ePath } = require( '../utils' );
const {
getAppRoot,
resolveLocalE2ePath,
resolvePackagePath,
} = require( '../utils' );
const {
WC_E2E_SCREENSHOTS,
JEST_PUPPETEER_CONFIG,
@ -30,7 +34,7 @@ if ( WC_E2E_SCREENSHOTS ) {
}
}
const nodeConfigDirs = [ path.resolve( __dirname, '../config' ) ];
const nodeConfigDirs = [ resolvePackagePath( 'config' ) ];
if ( appPath ) {
nodeConfigDirs.unshift( resolveLocalE2ePath( 'config' ) );
@ -51,10 +55,7 @@ if ( ! JEST_PUPPETEER_CONFIG ) {
// Use local Puppeteer config if there is one.
// Load test configuration file into an object.
const localJestConfigFile = resolveLocalE2ePath( 'config/jest-puppeteer.config.js' );
const jestConfigFile = path.resolve(
__dirname,
'../config/jest-puppeteer.config.js'
);
const jestConfigFile = resolvePackagePath( 'config/jest-puppeteer.config.js' );
testEnvVars.JEST_PUPPETEER_CONFIG = fs.existsSync( localJestConfigFile )
? localJestConfigFile
@ -88,7 +89,7 @@ if ( program.debug ) {
const envVars = Object.assign( {}, process.env, testEnvVars );
let configPath = path.resolve( __dirname, '../config/jest.config.js' );
let configPath = resolvePackagePath( 'config/jest.config.js' );
// Look for a Jest config in the dependent app's path.
if ( appPath ) {

View File

@ -13,10 +13,7 @@ const StreamZip = require( 'node-stream-zip' );
*/
const getRemotePluginZip = async ( fileUrl ) => {
const appPath = getAppRoot();
const savePath = path.resolve(
appPath,
'plugins/woocommerce/tests/e2e/plugins'
);
const savePath = resolveLocalE2ePath( 'plugins' );
mkdirp.sync( savePath );
// Pull the filename from the end of the URL

View File

@ -1,6 +1,6 @@
const getAppRoot = require( './app-root' );
const { getAppName, getAppBase } = require( './app-name' );
const { getTestConfig, getAdminConfig, resolveLocalE2ePath } = require( './test-config' );
const testConfig = require( './test-config' );
const { getRemotePluginZip, getLatestReleaseZipUrl } = require('./get-plugin-zip');
const takeScreenshotFor = require( './take-screenshot' );
const updateReadyPageStatus = require('./update-ready-page');
@ -10,12 +10,10 @@ module.exports = {
getAppBase,
getAppRoot,
getAppName,
getTestConfig,
getAdminConfig,
resolveLocalE2ePath,
getRemotePluginZip,
getLatestReleaseZipUrl,
takeScreenshotFor,
updateReadyPageStatus,
...testConfig,
...consoleUtils,
};

View File

@ -19,15 +19,72 @@ const resolveLocalE2ePath = ( filename = '' ) => {
);
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 {
resolvedPackage.path = require.resolve( packageName );
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 );
const packageLockData = JSON.parse( packageLockContent );
for ( const [ key, value ] of Object.entries( packageLockData.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 {
const package = resolvePackage( packageName );
packagePath = package.path;
}
const resolvedPath = path.resolve(
packagePath,
filename.indexOf( '/' ) == 0 ? filename.slice( 1 ) : filename
);
return resolvedPath;
};
// Copy local test configuration file if it exists.
const localTestConfigFile = resolveLocalE2ePath( 'config/default.json' );
const defaultConfigFile = path.resolve(
__dirname,
'../config/default/default.json'
);
const testConfigFile = path.resolve( __dirname, '../config/default.json' );
const defaultConfigFile = resolvePackagePath( 'config/default/default.json' );
const testConfigFile = resolvePackagePath( 'config/default.json' );
if ( fs.existsSync( localTestConfigFile ) ) {
fs.copyFileSync( localTestConfigFile, testConfigFile );
@ -94,4 +151,6 @@ module.exports = {
getTestConfig,
getAdminConfig,
resolveLocalE2ePath,
resolvePackage,
resolvePackagePath,
};