|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +const path = require( 'path' ); |
| 5 | +const { spawnSync } = require( 'child_process' ); |
| 6 | + |
| 7 | +/** |
| 8 | + * Internal dependencies |
| 9 | + */ |
| 10 | +const { plugins } = require( '../../plugins.json' ); |
| 11 | +const { formats } = require( '../plugin/lib/logger' ); |
| 12 | + |
| 13 | +const args = process.argv.slice( 2 ); |
| 14 | +const { WPP_PLUGIN, WPP_MULTISITE } = process.env; |
| 15 | +const pluginBasePath = path.resolve( __dirname, '../../' ); |
| 16 | +const pluginBaseName = path.basename( pluginBasePath ); |
| 17 | +const pluginsDir = path.resolve( pluginBasePath, 'plugins' ); |
| 18 | +const phpunitBin = path.resolve( pluginBasePath, 'vendor/bin/phpunit' ); |
| 19 | +const wpEnvBin = path.resolve( pluginBasePath, 'node_modules/.bin/wp-env' ); |
| 20 | +const wpPluginsDir = `/var/www/html/wp-content/plugins/${ pluginBaseName }`; |
| 21 | + |
| 22 | +const _plugins = []; // Store absolute paths to plugins. |
| 23 | + |
| 24 | +if ( WPP_PLUGIN ) { |
| 25 | + if ( |
| 26 | + 'performance-lab' !== WPP_PLUGIN && |
| 27 | + ! plugins.includes( WPP_PLUGIN ) |
| 28 | + ) { |
| 29 | + // eslint-disable-next-line no-console |
| 30 | + console.error( |
| 31 | + formats.error( |
| 32 | + `The plugin ${ WPP_PLUGIN } is not a valid plugin managed as part of this project.` |
| 33 | + ) |
| 34 | + ); |
| 35 | + |
| 36 | + process.exit( 1 ); |
| 37 | + } |
| 38 | + |
| 39 | + if ( 'performance-lab' === WPP_PLUGIN ) { |
| 40 | + _plugins.push( pluginBasePath ); |
| 41 | + } else { |
| 42 | + _plugins.push( path.resolve( pluginsDir, WPP_PLUGIN ) ); |
| 43 | + } |
| 44 | +} else { |
| 45 | + plugins.forEach( ( plugin ) => { |
| 46 | + _plugins.push( path.resolve( pluginsDir, plugin ) ); |
| 47 | + } ); |
| 48 | +} |
| 49 | + |
| 50 | +const wpEnvRunArgs = {}; // Store plugin name with args provided to wp-env run command. |
| 51 | + |
| 52 | +_plugins.forEach( ( plugin ) => { |
| 53 | + const command = [ |
| 54 | + 'run', |
| 55 | + 'tests-cli', |
| 56 | + `--env-cwd=${ plugin.replace( pluginBasePath, wpPluginsDir ) }`, |
| 57 | + ]; |
| 58 | + |
| 59 | + if ( WPP_MULTISITE ) { |
| 60 | + command.push( 'env', 'WP_MULTISITE=1' ); |
| 61 | + } |
| 62 | + |
| 63 | + command.push( phpunitBin.replace( pluginBasePath, wpPluginsDir ) ); |
| 64 | + |
| 65 | + if ( WPP_MULTISITE ) { |
| 66 | + command.push( '--exclude-group', 'ms-excluded' ); |
| 67 | + } |
| 68 | + |
| 69 | + if ( args.length ) { |
| 70 | + command.push( ...args ); |
| 71 | + } |
| 72 | + |
| 73 | + wpEnvRunArgs[ path.basename( plugin ) ] = command; |
| 74 | +} ); |
| 75 | + |
| 76 | +for ( const [ plugin, command ] of Object.entries( wpEnvRunArgs ) ) { |
| 77 | + if ( Object.keys( wpEnvRunArgs ).length > 1 ) { |
| 78 | + // eslint-disable-next-line no-console |
| 79 | + console.log( formats.info( `\n> Running tests for ${ plugin }` ) ); |
| 80 | + } |
| 81 | + |
| 82 | + // Execute tests synchronously for the sake of async fs.stat in loop. |
| 83 | + // fs.stat is used by wp-env to determine if `/snap` is available on user's system. |
| 84 | + const _process = spawnSync( wpEnvBin, command, { stdio: 'inherit' } ); |
| 85 | + |
| 86 | + if ( _process.signal ) { |
| 87 | + process.exit( 1 ); |
| 88 | + } |
| 89 | +} |
0 commit comments