Skip to content
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

Add WP_ENV_TESTS_MYSQL_PORT / .wp-env.json .env.tests.mysqlPort option etc #61057

Merged

Conversation

BrianHenryIE
Copy link
Contributor

@BrianHenryIE BrianHenryIE commented Apr 24, 2024

Fix: #50677

What?

Currently, the tests container's database port is assigned randomly each time wp-env is started. This PR adds the ability to set the MySQL port number in the .wp-env.json file or via a WP_ENV_TESTS_MYSQL_PORT environmental variable.

Why?

I'd like to be able to use the database in unit tests.

How?

By adding a config option to keep the port number static.

Testing Instructions

Set testsMysqlPort in .wp-env.json

{
	"testsMysqlPort": 52666
}
$ cat .wp-env.json | jq '.testsMySqlPort = 52666' | sponge .wp-env.json`
$ npx wp-env start
...
MySQL for automated testing is listening on port 52666
$ WP_ENV_TESTS_MYSQL_PORT=56678 npx wp-env start
...
MySQL for automated testing is listening on port 56678

Screenshots or screencast

Screenshot 2024-04-24 at 2 59 05 PM Screenshot 2024-04-24 at 2 59 19 PM

Note

I tried to also add it as a setting env.tests.mysqlPort but I got a parsing error:

✖ Invalid /Users/brian.henry/Sites/wpcs-autoload/.wp-env.json: "tests.mysqlPort" is not a configuration option.

Copy link

github-actions bot commented Apr 24, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @shashwatahalder01.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: shashwatahalder01.

Co-authored-by: BrianHenryIE <brianhenryie@git.wordpress.org>
Co-authored-by: ObliviousHarmony <obliviousharmony@git.wordpress.org>
Co-authored-by: desrosj <desrosj@git.wordpress.org>
Co-authored-by: alanef <fullworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Apr 24, 2024
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @BrianHenryIE! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

Copy link
Contributor

@ObliviousHarmony ObliviousHarmony left a comment

Choose a reason for hiding this comment

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

This seems like a reasonable addition to wp-env, thanks!

I've left some comments 😄

@@ -85,6 +87,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = {
themes: [],
port: 8888,
testsPort: 8889,
testsMysqlPort: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

I view the top-level port and testsPort properties are more of a legacy thing. It's more intuitive to set configuration like this only at the environment level. How would you feel about dropping the root testsMysqlPort in favor of just having development.mysqlPort and tests.mysqlPort?

@@ -167,6 +167,7 @@ module.exports = function buildDockerComposeConfig( config ) {
// Set the default ports based on the config values.
const developmentPorts = `\${WP_ENV_PORT:-${ config.env.development.port }}:80`;
const testsPorts = `\${WP_ENV_TESTS_PORT:-${ config.env.tests.port }}:80`;
const testsMysqlPorts = `\${WP_ENV_TESTS_MYSQL_PORT:-${ config.env.tests.mysqlPort ?? '' }}:3306`;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to support customizing the port like this let's go ahead and also support the development environment's MySQL port too.

@BrianHenryIE
Copy link
Contributor Author

BrianHenryIE commented May 14, 2024

To test, checkout this PR and create a project with its package.json referencing it:

{
  "name": "gutenberg-61057-test",
  "devDependencies": {
    "@wordpress/env": "file:/path/to/checked/out/gutenberg/packages/env"
  }
}
npm install;
npx wp-env start;

Observe default behaviour with randomly assigned MySQL ports:

MySQL is listening on port 54916
MySQL for automated testing is listening on port 54922

Add .wp-env.json:

{
  "env": {
    "development": {
      "mysqlPort": 13306
    },
    "tests": {
      "mysqlPort": 13307
    }
  }
}
npx wp-env start;

Observe specified ports used:

MySQL is listening on port 13306
MySQL for automated testing is listening on port 13307

Test Bash env variables:

WP_ENV_MYSQL_PORT=13304 npx wp-env start
MySQL is listening on port 13304
MySQL for automated testing is listening on port 13307
WP_ENV_TESTS_MYSQL_PORT=13305 npx wp-env start
MySQL is listening on port 13306
MySQL for automated testing is listening on port 13305

I'll take another look at the automated tests soon. At first glance the failures don't seem related to these changes but are obviously wp-env config parsing failures. Fixed.

@BrianHenryIE BrianHenryIE changed the title Add WP_ENV_TESTS_MYSQL_PORT / .wp-env.json testsMysqlPort option May 14, 2024
Copy link
Contributor

@desrosj desrosj left a comment

Choose a reason for hiding this comment

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

This seems sensible to me. Thanks for adding tests! @ObliviousHarmony has this been updated to your liking?

@desrosj
Copy link
Contributor

desrosj commented May 15, 2024

Actually on second look I mistook these changes as new tests. It would be great to add a few if you could.

@desrosj
Copy link
Contributor

desrosj commented Jun 5, 2024

It seems like all of the feedback has been reasonably addressed. Since this is only being blocked by the review points to be confirmed as addressed, I'm going to merge this to give it a try.

@desrosj desrosj merged commit 78600be into WordPress:trunk Jun 5, 2024
62 of 63 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.6 milestone Jun 5, 2024
patil-vipul pushed a commit to patil-vipul/gutenberg that referenced this pull request Jun 17, 2024
… option etc (WordPress#61057)

* Add `WP_ENV_TESTS_MYSQL_PORT`

* Remove base `testsMysqlPort` in favour of `.env.x.mysqlPort`

* Remove old root config code

* Update config-integration.js.snap

* Add `mysqlPort` to `DEFAULT_CONFIG`

* Update README.md

* Add test expectations for mysqlPort changes

Unlinked contributors: shashwatahalder01.

Co-authored-by: BrianHenryIE <brianhenryie@git.wordpress.org>
Co-authored-by: ObliviousHarmony <obliviousharmony@git.wordpress.org>
Co-authored-by: desrosj <desrosj@git.wordpress.org>
Co-authored-by: alanef <fullworks@git.wordpress.org>
@bph bph added the [Type] Build Tooling Issues or PRs related to build tooling label Jun 18, 2024
Copy link

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core.
  • Labels found: [Type] Enhancement, [Type] Build Tooling, First-time Contributor.

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Build Tooling Issues or PRs related to build tooling [Type] Enhancement A suggestion for improvement.
4 participants