-
Notifications
You must be signed in to change notification settings - Fork 138
Update PHPUnit tests infrastructure #1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bfbb5d0
5cfcd3d
ae4391e
f319d67
b7098e8
7326514
4dff72d
ed013da
7ec118b
0bc071f
95477f6
a89b584
23c78c6
2fc36f0
8f2db86
3150c36
92cba14
8b5df2f
88d6e86
8b93bcd
f43b497
0a29cba
2b5a6e3
ba366f2
7bc326e
5273312
19f25d8
45b9687
26b2c77
92bee06
5abb8f7
e341854
b472c00
7d49c26
c91cff8
517286c
01a47d1
a5d95e6
4c5c45f
01a64be
4f53252
0fbf6dc
0533572
18bb970
90e161b
2237ba0
62cdaae
c8fa1fd
5d74c29
ebb7d8f
1b7f334
51b4a9b
8baa7b6
482b822
7b55708
fbd4b9a
d5300d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,6 @@ on: | |
| - '**/package.json' | ||
| - 'package-lock.json' | ||
| - 'phpunit.xml.dist' | ||
| - 'tests/multisite.xml' | ||
| - 'composer.json' | ||
| - 'composer.lock' | ||
| pull_request: | ||
|
|
@@ -25,7 +24,6 @@ on: | |
| - '**/package.json' | ||
| - 'package-lock.json' | ||
| - 'phpunit.xml.dist' | ||
| - 'tests/multisite.xml' | ||
| - 'composer.json' | ||
| - 'composer.lock' | ||
| types: | ||
|
|
@@ -52,10 +50,10 @@ jobs: | |
| WP_ENV_PHP_VERSION: ${{ matrix.php }} | ||
| WP_ENV_CORE: ${{ matrix.wp == 'trunk' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wp ) }} | ||
| steps: | ||
| - uses: styfle/cancel-workflow-action@0.12.1 | ||
| - uses: actions/checkout@v4 | ||
| - uses: styfle/cancel-workflow-action@0.11.0 | ||
| - uses: actions/checkout@v3 | ||
| - name: Setup Node.js (.nvmrc) | ||
| uses: actions/setup-node@v4 | ||
| uses: actions/setup-node@v3 | ||
| with: | ||
| node-version-file: '.nvmrc' | ||
| cache: npm | ||
|
|
@@ -71,6 +69,6 @@ jobs: | |
| - name: Composer update | ||
| run: npm run wp-env run tests-cli -- --env-cwd="wp-content/plugins/$(basename $(pwd))" composer update --no-interaction | ||
| - name: Running single site unit tests for plugins | ||
| run: npm run test-php-plugins | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you removed
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did that in bd66bb9
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean we should remove that file reference from workflow file.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh okay. Done in 61eb092. |
||
| run: npm run test:php:plugins | ||
| - name: Running multisite unit tests for plugins | ||
| run: npm run test-php-multisite-plugins | ||
| run: npm run test:php:plugins:multisite | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * External dependencies | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| const path = require( 'path' ); | ||||||||||||||||||||||||
| const { spawnSync } = require( 'child_process' ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Internal dependencies | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| const { plugins } = require( '../../plugins.json' ); | ||||||||||||||||||||||||
| const { formats } = require( '../plugin/lib/logger' ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const args = process.argv.slice( 2 ); | ||||||||||||||||||||||||
| const { WPP_PLUGIN, WPP_MULTISITE } = process.env; | ||||||||||||||||||||||||
| const pluginBasePath = path.resolve( __dirname, '../../' ); | ||||||||||||||||||||||||
| const pluginBaseName = path.basename( pluginBasePath ); | ||||||||||||||||||||||||
| const pluginsDir = path.resolve( pluginBasePath, 'plugins' ); | ||||||||||||||||||||||||
| const phpunitBin = path.resolve( pluginBasePath, 'vendor/bin/phpunit' ); | ||||||||||||||||||||||||
| const wpEnvBin = path.resolve( pluginBasePath, 'node_modules/.bin/wp-env' ); | ||||||||||||||||||||||||
| const wpPluginsDir = `/var/www/html/wp-content/plugins/${ pluginBaseName }`; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const _plugins = []; // Store absolute paths to plugins. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( WPP_PLUGIN ) { | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thelovekesh Unrelated to the ongoing discussion in #1012: If we implement new JS tooling scripts like this, I think they should be incorporated into the existing foundation in |
||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||
| 'performance-lab' !== WPP_PLUGIN && | ||||||||||||||||||||||||
| ! plugins.includes( WPP_PLUGIN ) | ||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||||||||
| console.error( | ||||||||||||||||||||||||
| formats.error( | ||||||||||||||||||||||||
| `The plugin ${ WPP_PLUGIN } is not a valid plugin managed as part of this project.` | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
Comment on lines
+29
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we utilize
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think better to use Otherwise, this could be added to logger as an alias in the same way that was done for |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| process.exit( 1 ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( 'performance-lab' === WPP_PLUGIN ) { | ||||||||||||||||||||||||
| _plugins.push( pluginBasePath ); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| _plugins.push( path.resolve( pluginsDir, WPP_PLUGIN ) ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| plugins.forEach( ( plugin ) => { | ||||||||||||||||||||||||
| _plugins.push( path.resolve( pluginsDir, plugin ) ); | ||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const wpEnvRunArgs = {}; // Store plugin name with args provided to wp-env run command. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| _plugins.forEach( ( plugin ) => { | ||||||||||||||||||||||||
| const command = [ | ||||||||||||||||||||||||
| 'run', | ||||||||||||||||||||||||
| 'tests-cli', | ||||||||||||||||||||||||
| `--env-cwd=${ plugin.replace( pluginBasePath, wpPluginsDir ) }`, | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( WPP_MULTISITE ) { | ||||||||||||||||||||||||
| command.push( 'env', 'WP_MULTISITE=1' ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| command.push( phpunitBin.replace( pluginBasePath, wpPluginsDir ) ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( WPP_MULTISITE ) { | ||||||||||||||||||||||||
| command.push( '--exclude-group', 'ms-excluded' ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( args.length ) { | ||||||||||||||||||||||||
| command.push( ...args ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| wpEnvRunArgs[ path.basename( plugin ) ] = command; | ||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for ( const [ plugin, command ] of Object.entries( wpEnvRunArgs ) ) { | ||||||||||||||||||||||||
| if ( Object.keys( wpEnvRunArgs ).length > 1 ) { | ||||||||||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||||||||||
| console.log( formats.info( `\n> Running tests for ${ plugin }` ) ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Execute tests synchronously for the sake of async fs.stat in loop. | ||||||||||||||||||||||||
| // fs.stat is used by wp-env to determine if `/snap` is available on user's system. | ||||||||||||||||||||||||
| const _process = spawnSync( wpEnvBin, command, { stdio: 'inherit' } ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if ( _process.signal ) { | ||||||||||||||||||||||||
| process.exit( 1 ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| "post-install-cmd": "if php -r 'exit( version_compare( phpversion(), \"8.1\", \">=\" ) ? 0 : 1 );'; then composer --working-dir=build-cs install --no-interaction; else echo 'Skipping composer install for build-cs since not on PHP 8.1+. You are running: '; php -v; fi", | ||
| "post-update-cmd": "if php -r 'exit( version_compare( phpversion(), \"8.1\", \">=\" ) ? 0 : 1 );'; then composer --working-dir=build-cs update --no-interaction; else echo 'Skipping composer update for build-cs since not on PHP 8.1+. You are running: '; php -v; fi", | ||
| "phpstan": "build-cs/vendor/bin/phpstan analyse --memory-limit=2048M", | ||
| "phpunit": "phpunit --verbose", | ||
| "format": "build-cs/vendor/bin/phpcbf --report-summary --report-source", | ||
| "lint": "build-cs/vendor/bin/phpcs", | ||
| "lint:all": [ | ||
|
|
@@ -41,56 +42,13 @@ | |
| "lint:embed-optimizer": "@lint -- ./plugins/embed-optimizer --standard=./plugins/embed-optimizer/phpcs.xml.dist", | ||
| "lint:optimization-detective": "@lint -- ./plugins/optimization-detective --standard=./plugins/optimization-detective/phpcs.xml.dist", | ||
| "lint:speculation-rules": "@lint -- ./plugins/speculation-rules --standard=./plugins/speculation-rules/phpcs.xml.dist", | ||
| "lint:webp-uploads": "@lint -- ./plugins/webp-uploads --standard=./plugins/webp-uploads/phpcs.xml.dist", | ||
| "test": "phpunit --verbose --testsuite performance-lab", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove all of the test scripts from composer? I think it would be better if scripts that rely on PHP functionality were maintained in the composer.json and provide a node script that referenced composer than moving all of these over to the package.json file.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joemcgill @felixarntz In conjunction with both this and #1065 (comment), I have added a node script to run PHPUnit tests in 0ba78d9 and added the scripts in Now the commands works like: Please LMKWYT.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nice. Previously in order to run just the tests for Optimization Detective, I would do this: npm run wp-env run tests-cli bashAnd then in the container: cd wp-content/plugins/performance/
composer run-script test:optimization-detective # (run as many times as needed)Great to have a command to run just one plugin's tests: npm run test:php:plugins --plugin=optimization-detective
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more advantage is, that you can now pass args to PHPUnit by escaping once only like: is makes it more better IMO. |
||
| "test-multisite": "phpunit -c tests/multisite.xml --verbose --testsuite performance-lab", | ||
| "test:all": [ | ||
| "@test", | ||
| "@test:plugins" | ||
| ], | ||
| "test-multisite:all": [ | ||
| "@test-multisite", | ||
| "@test-multisite:plugins" | ||
| ], | ||
| "test:plugins": [ | ||
| "@test:auto-sizes", | ||
| "@test:dominant-color-images", | ||
| "@test:embed-optimizer", | ||
| "@test:optimization-detective", | ||
| "@test:speculation-rules", | ||
| "@test:webp-uploads" | ||
| ], | ||
| "test-multisite:plugins": [ | ||
| "@test-multisite:auto-sizes", | ||
| "@test-multisite:dominant-color-images", | ||
| "@test-multisite:embed-optimizer", | ||
| "@test-multisite:optimization-detective", | ||
| "@test-multisite:speculation-rules", | ||
| "@test-multisite:webp-uploads" | ||
| ], | ||
| "test:auto-sizes": "phpunit --verbose --testsuite auto-sizes", | ||
| "test-multisite:auto-sizes": "phpunit -c tests/multisite.xml --verbose --testsuite auto-sizes", | ||
| "test:dominant-color-images": "phpunit --verbose --testsuite dominant-color-images", | ||
| "test-multisite:dominant-color-images": "phpunit -c tests/multisite.xml --verbose --testsuite dominant-color-images", | ||
| "test:embed-optimizer": "phpunit --verbose --testsuite embed-optimizer", | ||
| "test-multisite:embed-optimizer": "phpunit -c tests/multisite.xml --verbose --testsuite embed-optimizer", | ||
| "test:optimization-detective": "phpunit --verbose --testsuite optimization-detective", | ||
| "test-multisite:optimization-detective": "phpunit -c tests/multisite.xml --verbose --testsuite optimization-detective", | ||
| "test:speculation-rules": "phpunit --verbose --testsuite speculation-rules", | ||
| "test-multisite:speculation-rules": "phpunit -c tests/multisite.xml --verbose --testsuite speculation-rules", | ||
| "test:webp-uploads": "phpunit --verbose --testsuite webp-uploads", | ||
| "test-multisite:webp-uploads": "phpunit -c tests/multisite.xml --verbose --testsuite webp-uploads" | ||
| "lint:webp-uploads": "@lint -- ./plugins/webp-uploads --standard=./plugins/webp-uploads/phpcs.xml.dist" | ||
| }, | ||
| "config": { | ||
| "allow-plugins": { | ||
| "composer/installers": true, | ||
| "phpstan/extension-installer": true, | ||
| "dealerdirect/phpcodesniffer-composer-installer": true | ||
| } | ||
| }, | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "PerformanceLab\\Tests\\": "tests/utils" | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be reverted as trunk now have latest dependancy.