-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Env: Download WordPress PHPUnit Into Container (#41780)
* Added WordPress PHPUnit Library To `wp-env` This commit adds support for downloading the WordPress PHPUnit source and mounting it at /phpunit in the container. It uses the version of WordPress installed in the container to decide what version to download. * Improved `WordPress/wp-develop` Git Performance This commit minimizes the amount of data that needs to be downloaded when we pull in the test framework. It does this by not cloning the entire repository and only checking out once we've set up the sparse checkout. * Moved test library to `/wordpress-phpunit` This commit moves the test library from `/phpunit' to `/wordpress-phpunit`. This change is to avoid potential confusion. `/phpunit` sounds like a tool while `/wordpress-phpunit` is not so ambiguous. * Removed `wp-phpunit/wp-phpunit` Native Support The entire point of including the test files is that they are better to use than a package like this. We should have an opinionated default and allow them to override it if they really want to. * Fixed Unit Tests Since we've made it so that the default is no longer to support `wp-phpunit/wp-phpunit`, we need to explicitly override the default `wp-env` behavior. This will be cleaned up in a future pull request that switches over entirely to the test files in the environment. * Documented PHPUnit Test File Inclusion * Added `wp-phpunit/wp-phpunit` Notice to Changelog
- Loading branch information
1 parent
a7181f2
commit 9b13f84
Showing
11 changed files
with
289 additions
and
34 deletions.
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
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.