Skip to content

Commit

Permalink
wp-env: Remove PHPUnit and Composer containers (#50408)
Browse files Browse the repository at this point in the history
* Removed Composer & PHPUnit Containers

- Remove these containers because they don't work as expected,
  and are broken in many cases. It's also confusing to have a lot
  of random containers!

- Add composer and phpunit to other containers so that they
  can still be used. (E.g. `wp-env run cli composer` instead of
  `wp-env run composer`)

* Utilized `--env-cwd` Where Useful

---------

Co-authored-by: Noah Allen <noahtallen@gmail.com>
  • Loading branch information
ObliviousHarmony and noahtallen authored May 8, 2023
1 parent ad5f722 commit de94c50
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 198 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
"fixtures:generate": "cross-env GENERATE_MISSING_FIXTURES=y npm run test:unit test/integration/full-content/ && npm run format test/integration/fixtures/blocks/*.json",
"fixtures:regenerate": "npm-run-all fixtures:clean fixtures:generate",
"format": "wp-scripts format",
"format:php": "wp-env run composer run-script format",
"format:php": "wp-env run --env-cwd=\"wp-content/plugins/gutenberg\" cli composer run-script format",
"lint": "concurrently \"npm run lint:lockfile\" \"npm run lint:tsconfig\" \"npm run lint:js\" \"npm run lint:pkg-json\" \"npm run lint:css\"",
"lint:css": "wp-scripts lint-style \"**/*.scss\"",
"lint:css:fix": "npm run lint:css -- --fix",
Expand All @@ -281,8 +281,8 @@
"lint:lockfile": "node ./bin/validate-package-lock.js",
"lint:tsconfig": "node ./bin/validate-tsconfig.mjs",
"lint:md:docs": "wp-scripts lint-md-docs",
"prelint:php": "wp-env run composer \"update --no-interaction\"",
"lint:php": "wp-env run composer run-script lint",
"prelint:php": "wp-env run --env-cwd='wp-content/plugins/gutenberg' cli composer update --no-interaction",
"lint:php": "wp-env run --env-cwd=\"wp-content/plugins/gutenberg\" cli composer run-script lint",
"lint:pkg-json": "wp-scripts lint-pkg-json . 'packages/*/package.json'",
"native": "npm run --prefix packages/react-native-editor",
"other:changelog": "node ./bin/plugin/cli.js changelog",
Expand Down Expand Up @@ -318,9 +318,9 @@
"test:unit:debug": "wp-scripts --inspect-brk test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ",
"test:unit:profile": "wp-scripts --cpu-prof test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ",
"pretest:unit:php": "wp-env start",
"test:unit:php": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist --verbose",
"test:unit:php": "wp-env run --env-cwd=\"wp-content/plugins/gutenberg\" tests-wordpress vendor/bin/phpunit -c phpunit.xml.dist --verbose",
"pretest:unit:php:multisite": "wp-env start",
"test:unit:php:multisite": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit/multisite.xml --verbose",
"test:unit:php:multisite": "wp-env run --env-cwd=\"wp-content/plugins/gutenberg\" tests-wordpress vendor/bin/phpunit -c phpunit/multisite.xml --verbose",
"test:unit:update": "npm run test:unit -- --updateSnapshot",
"test:unit:watch": "npm run test:unit -- --watch",
"wp-env": "wp-env"
Expand Down
8 changes: 5 additions & 3 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

- Docker containers now run as the host user. This should resolve problems with permissions arising from different owners
between the host, web container, and cli container. If you still encounter permissions issues, try running `npx wp-env destroy` so that the environment can be recreated with the correct permissions.
- Remove the `composer` and `phpunit` Docker containers. If you are currently using the `run composer` or `run phpunit` command you can migrate to `run cli composer` or `run tests-cli phpunit` respectively. Note that with `composer`, you will need to use the `--env-cwd` option to navigate to your plugin's directory as it is no longer the default working directory.

### New feature

- Create an `afterSetup` option in `.wp-env.json` files for setting arbitrary commands to run after setting up WordPress when using `wp-env start` and `wp-env clean`.
- Create an `afterSetup` option in `.wp-env.json` files for setting arbitrary commands to run after setting up WordPress when using `npx wp-env start` and `npx wp-env clean`.
- Add a `WP_ENV_AFTER_SETUP` environment variable to override the `afterSetup` option.
- Execute the `afterSetup` command on `wp-env start` after the environment is set up. This can happen when your config changes, WordPress updates, or you pass the `--update` flag.
- Execute the `afterSetup` command on `wp-env clean`.
- Execute the `afterSetup` command on `npx wp-env start` after the environment is set up. This can happen when your config changes, WordPress updates, or you pass the `--update` flag.
- Execute the `afterSetup` command on `npx wp-env clean`.
- Globally install `composer` and the correct version of `phpunit` in all of the Docker containers.

### Bug fix

Expand Down
48 changes: 0 additions & 48 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,6 @@ module.exports = function buildDockerComposeConfig( config ) {
const developmentPorts = `\${WP_ENV_PORT:-${ config.env.development.port }}:80`;
const testsPorts = `\${WP_ENV_TESTS_PORT:-${ config.env.tests.port }}:80`;

// Defaults are to use the most recent version of PHPUnit that provides
// support for the specified version of PHP.
// PHP Unit is assumed to be for Tests so use the testsPhpVersion.
let phpunitTag = 'latest';
const phpunitPhpVersion = '-php-' + config.env.tests.phpVersion + '-fpm';
if ( config.env.tests.phpVersion === '5.6' ) {
phpunitTag = '5' + phpunitPhpVersion;
} else if ( config.env.tests.phpVersion === '7.0' ) {
phpunitTag = '6' + phpunitPhpVersion;
} else if ( config.env.tests.phpVersion === '7.1' ) {
phpunitTag = '7' + phpunitPhpVersion;
} else if ( config.env.tests.phpVersion === '7.2' ) {
phpunitTag = '8' + phpunitPhpVersion;
} else if (
[ '7.3', '7.4', '8.0', '8.1', '8.2' ].indexOf(
config.env.tests.phpVersion
) >= 0
) {
phpunitTag = '9' + phpunitPhpVersion;
}
const phpunitImage = `wordpressdevelop/phpunit:${ phpunitTag }`;

// If the user mounted their own uploads folder, we should not override it in the phpunit service.
const isMappingTestUploads = testsMounts.some( ( mount ) =>
mount.endsWith( ':/var/www/html/wp-content/uploads' )
);

return {
version: '3.7',
services: {
Expand Down Expand Up @@ -284,33 +257,12 @@ module.exports = function buildDockerComposeConfig( config ) {
WP_TESTS_DIR: '/wordpress-phpunit',
},
},
composer: {
image: 'composer',
volumes: [ `${ config.configDirectoryPath }:/app` ],
},
phpunit: {
image: phpunitImage,
depends_on: [ 'tests-wordpress' ],
volumes: [
...testsMounts,
...( ! isMappingTestUploads
? [ 'phpunit-uploads:/var/www/html/wp-content/uploads' ]
: [] ),
],
environment: {
LOCAL_DIR: 'html',
WP_TESTS_DIR: '/wordpress-phpunit',
...dbEnv.credentials,
...dbEnv.tests,
},
},
},
volumes: {
...( ! config.env.development.coreSource && { wordpress: {} } ),
...( ! config.env.tests.coreSource && { 'tests-wordpress': {} } ),
mysql: {},
'mysql-test': {},
'phpunit-uploads': {},
'user-home': {},
'tests-user-home': {},
},
Expand Down
39 changes: 39 additions & 0 deletions packages/env/lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require( 'path' );
*/
const initConfig = require( '../init-config' );
const getHostUser = require( '../get-host-user' );
const { ValidationError } = require( '../config' );

/**
* @typedef {import('../config').WPConfig} WPConfig
Expand All @@ -32,6 +33,8 @@ module.exports = async function run( {
spinner,
debug,
} ) {
validateContainerExistence( container );

const config = await initConfig( { spinner, debug } );

command = command.join( ' ' );
Expand All @@ -44,6 +47,42 @@ module.exports = async function run( {
spinner.text = `Ran \`${ command }\` in '${ container }'.`;
};

/**
* Validates the container option and throws if it is invalid.
*
* @param {string} container The Docker container to run the command on.
*/
function validateContainerExistence( container ) {
// Give better errors for containers that we have removed.
if ( container === 'phpunit' ) {
throw new ValidationError(
"The 'phpunit' container has been removed. Please use 'wp-env run tests-cli --env-cwd=wp-content/path/to/plugin phpunit' instead."
);
}
if ( container === 'composer' ) {
throw new ValidationError(
"The 'composer' container has been removed. Please use 'wp-env run cli --env-cwd=wp-content/path/to/plugin composer' instead."
);
}

// Provide better error output than Docker's "service does not exist" messaging.
const validContainers = [
'mysql',
'tests-mysql',
'wordpress',
'tests-wordpress',
'cli',
'tests-cli',
];
if ( ! validContainers.includes( container ) ) {
throw new ValidationError(
`The '${ container }' container does not exist. Valid selections are: ${ validContainers.join(
', '
) }`
);
}
}

/**
* Runs an arbitrary command on the given Docker container.
*
Expand Down
Loading

1 comment on commit de94c50

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in de94c50.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4919977567
📝 Reported issues:

Please sign in to comment.