-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Env: Download WordPress PHPUnit Into Container #41780
Merged
noahtallen
merged 17 commits into
WordPress:trunk
from
ObliviousHarmony:add/phpcs-installation
Jul 15, 2022
Merged
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d26af6c
Fixed WPConfig References
ObliviousHarmony f14dcef
Added WordPress PHPUnit Library To `wp-env`
ObliviousHarmony f8f5dcf
Fixed Test Suite
ObliviousHarmony 0bf2927
Improved `WordPress/wp-develop` Git Performance
ObliviousHarmony 68a5e1e
Fixed Linting Errors
ObliviousHarmony 99a70d2
Moved test library to `/wordpress-phpunit`
ObliviousHarmony caa47fb
Fixed Test Config
ObliviousHarmony 49fdcca
Removed `wp-phpunit/wp-phpunit` Native Support
ObliviousHarmony f0c2721
Fixed Unit Tests
ObliviousHarmony 27cffcb
Merge branch 'trunk' into add/phpcs-installation
ObliviousHarmony 3650a2d
Documented PHPUnit Test File Inclusion
ObliviousHarmony d9dc775
Added Changelog
ObliviousHarmony eb07442
Fixed Coding Standards
ObliviousHarmony cf5608a
Merge branch 'trunk' into add/phpcs-installation
ObliviousHarmony 47d307a
Fixed `WP_TESTS_DIR` Override
ObliviousHarmony efe7b23
Improved documentation and changelog
ObliviousHarmony c9a1a23
Added `wp-phpunit/wp-phpunit` Notice to Changelog
ObliviousHarmony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
'use strict'; | ||
/** | ||
* External dependencies | ||
*/ | ||
const SimpleGit = require( 'simple-git' ); | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* @typedef {import('./config').WPConfig} WPConfig | ||
*/ | ||
|
||
/** | ||
* Downloads the WordPress PHPUnit files for each environment into the appropriate directories. | ||
* | ||
* @param {WPConfig} config The wp-env config object. | ||
* @param {Object} wpVersions The WordPress versions for each environment. | ||
* @param {Object} spinner The spinner object to show progress. | ||
* @param {boolean} debug Indicates whether or not debug mode is active. | ||
* @return {Promise} Returns a promise which resolves when the downloads finish. | ||
*/ | ||
module.exports = function downloadWPPHPUnit( | ||
config, | ||
wpVersions, | ||
spinner, | ||
debug | ||
) { | ||
const progresses = {}; | ||
const getProgressSetter = ( id ) => ( progress ) => { | ||
progresses[ id ] = progress; | ||
spinner.text = | ||
'Downloading WordPress PHPUnit Suite.\n' + | ||
Object.entries( progresses ) | ||
.map( | ||
( [ key, value ] ) => | ||
` - ${ key }: ${ ( value * 100 ).toFixed( 0 ) }/100%` | ||
) | ||
.join( '\n' ); | ||
}; | ||
|
||
const promises = []; | ||
for ( const env in config.env ) { | ||
const wpVersion = wpVersions[ env ] ? wpVersions[ env ] : null; | ||
const directory = path.join( | ||
config.workDirectoryPath, | ||
env === 'development' | ||
? 'WordPress-PHPUnit' | ||
: 'tests-WordPress-PHPUnit' | ||
); | ||
promises.push( | ||
downloadTestSuite( directory, wpVersion, { | ||
onProgress: getProgressSetter, | ||
spinner, | ||
debug, | ||
} ) | ||
); | ||
} | ||
|
||
return Promise.all( promises ); | ||
}; | ||
|
||
/** | ||
* Downloads the PHPUnit tests for a given WordPress version into the appropriate directory. | ||
* | ||
* @param {string} directory The directory to place the PHPUnit tests in. | ||
* @param {string} wpVersion The version of WordPress to install PHPUnit tests for. Trunk when empty. | ||
* @param {Object} options | ||
* @param {Function} options.onProgress A function called with download progress. Will be invoked with one argument: a number that ranges from 0 to 1 which indicates current download progress for this source. | ||
* @param {Object} options.spinner A CLI spinner which indicates progress. | ||
* @param {boolean} options.debug True if debug mode is enabled. | ||
*/ | ||
async function downloadTestSuite( | ||
directory, | ||
wpVersion, | ||
{ onProgress, spinner, debug } | ||
) { | ||
const log = debug | ||
? ( message ) => { | ||
spinner.info( `SimpleGit: ${ message }` ); | ||
spinner.start(); | ||
} | ||
: () => {}; | ||
onProgress( 0 ); | ||
|
||
const progressHandler = ( { progress } ) => { | ||
onProgress( progress / 100 ); | ||
}; | ||
|
||
// Make sure that the version is in X.X.X format. This is required | ||
// because WordPress/wordpress-develop uses X.X.X tags but | ||
// WordPress uses X.X for non-patch releases. | ||
if ( wpVersion && wpVersion.match( /^[0-9]+.[0-9]+$/ ) ) { | ||
wpVersion += '.0'; | ||
} | ||
|
||
log( 'Cloning or getting the PHPUnit suite from GitHub.' ); | ||
const git = SimpleGit( { progress: progressHandler } ); | ||
|
||
const isRepo = | ||
fs.existsSync( directory ) && | ||
( await git.cwd( directory ).checkIsRepo( 'root' ) ); | ||
|
||
if ( isRepo ) { | ||
log( 'Repo already exists, using it.' ); | ||
} else { | ||
await git.clone( | ||
'https://github.com/WordPress/wordpress-develop.git', | ||
directory, | ||
{ | ||
'--depth': '1', | ||
'--no-checkout': null, | ||
} | ||
); | ||
await git.cwd( directory ); | ||
|
||
// We use a sparse checkout to minimize the amount of data we need to download. | ||
log( 'Enabling sparse checkout.' ); | ||
await git.raw( 'sparse-checkout', 'set', '--cone', 'tests/phpunit' ); | ||
} | ||
|
||
// Figure out the ref that we need to checkout to get the correct version of the PHPUnit library. | ||
// Alpha, Beta, and RC versions are bleeding edge and should pull from trunk. | ||
let ref; | ||
const fetchRaw = []; | ||
if ( ! wpVersion || wpVersion.match( /-(?:alpha|beta|rc)/ ) ) { | ||
ref = 'trunk'; | ||
fetchRaw.push( 'fetch', 'origin', ref, '--depth', '1' ); | ||
} else { | ||
ref = `tags/${ wpVersion }`; | ||
fetchRaw.push( 'fetch', 'origin', 'tag', wpVersion, '--depth', '1' ); | ||
} | ||
|
||
log( `Fetching ${ ref }.` ); | ||
await git.raw( fetchRaw ); | ||
|
||
log( `Checking out ${ ref }.` ); | ||
await git.checkout( ref ); | ||
|
||
onProgress( 1 ); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to run this for every
wp-env
install? I wonder if we should instead have a top-level config option for including phpunit. That said, if this isn't causing any meaningful performance impact, we can probably leave it as is.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.
I considered this initially but instead decided to heavily optimize the
develop
checkout. It only downloads thephpunit
files. I've also taken care to parallelize the download, so as far as performance goes, I've found this to be incredibly fast. It only downloads something like 21MB. For what it's worth, I'm pretty sure this is lighter than thephpunit
container we're currently spinning up.For me, this was a tradeoff between standardization and performance. Everyone who is using
wp-env
should be writing unit tests. All of these tests will likely require the PHPUnit files, so, I didn't see any good reasons not to include them by default.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.
That's a good reason. The easier we can make unit tests to use, the better.