Skip to content

Commit 78600be

Browse files
BrianHenryIEBrianHenryIEObliviousHarmonydesrosjalanef
authored
Add WP_ENV_TESTS_MYSQL_PORT / .wp-env.json .env.tests.mysqlPort option etc (#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 <[email protected]> Co-authored-by: ObliviousHarmony <[email protected]> Co-authored-by: desrosj <[email protected]> Co-authored-by: alanef <[email protected]>
1 parent 05ae110 commit 78600be

File tree

7 files changed

+81
-4
lines changed

7 files changed

+81
-4
lines changed

packages/env/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ You can customize the WordPress installation, plugins and themes that the develo
480480
`.wp-env.json` supports fields for options applicable to both the tests and development instances.
481481

482482
| Field | Type | Default | Description |
483-
| -------------- | -------------- | -------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
483+
|----------------|----------------|----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
484484
| `"core"` | `string\|null` | `null` | The WordPress installation to use. If `null` is specified, `wp-env` will use the latest production release of WordPress. |
485485
| `"phpVersion"` | `string\|null` | `null` | The PHP version to use. If `null` is specified, `wp-env` will use the default version used with production release of WordPress. |
486486
| `"plugins"` | `string[]` | `[]` | A list of plugins to install and activate in the environment. |
@@ -489,6 +489,7 @@ You can customize the WordPress installation, plugins and themes that the develo
489489
| `"testsPort"` | `integer` | `8889` | The port number for the test site. You'll access the instance through the port: 'http://localhost:8889'. |
490490
| `"config"` | `Object` | See below. | Mapping of wp-config.php constants to their desired values. |
491491
| `"mappings"` | `Object` | `"{}"` | Mapping of WordPress directories to local directories to be mounted in the WordPress instance. |
492+
| `"mysqlPort"` | `integer` | `null` (randomly assigned) | The MySQL port number to expose. The setting is only available in the `env.development` and `env.tests` objects. |
492493

493494
_Note: the port number environment variables (`WP_ENV_PORT` and `WP_ENV_TESTS_PORT`) take precedent over the .wp-env.json values._
494495

@@ -521,7 +522,8 @@ Additionally, the key `env` is available to override any of the above options on
521522
"config": {
522523
"KEY_1": false
523524
},
524-
"port": 3000
525+
"port": 3000,
526+
"mysqlPort": 13306
525527
}
526528
}
527529
}
@@ -686,6 +688,8 @@ You can tell `wp-env` to use a custom port number so that your instance does not
686688
}
687689
```
688690

691+
These can also be set via the environment variables `WP_ENV_PORT`, `WP_ENV_TESTS_PORT`, `WP_ENV_MYSQL_PORT` and `WP_ENV_TESTS_MYSQL_PORT`.
692+
689693
### Specific PHP Version
690694

691695
You can tell `wp-env` to use a specific PHP version for compatibility and testing. This can also be set via the environment variable `WP_ENV_PHP_VERSION`.

packages/env/lib/build-docker-compose-config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,20 @@ module.exports = function buildDockerComposeConfig( config ) {
166166

167167
// Set the default ports based on the config values.
168168
const developmentPorts = `\${WP_ENV_PORT:-${ config.env.development.port }}:80`;
169+
const developmentMysqlPorts = `\${WP_ENV_MYSQL_PORT:-${
170+
config.env.development.mysqlPort ?? ''
171+
}}:3306`;
169172
const testsPorts = `\${WP_ENV_TESTS_PORT:-${ config.env.tests.port }}:80`;
173+
const testsMysqlPorts = `\${WP_ENV_TESTS_MYSQL_PORT:-${
174+
config.env.tests.mysqlPort ?? ''
175+
}}:3306`;
170176

171177
return {
172178
version: '3.7',
173179
services: {
174180
mysql: {
175181
image: 'mariadb:lts',
176-
ports: [ '3306' ],
182+
ports: [ developmentMysqlPorts ],
177183
environment: {
178184
MYSQL_ROOT_HOST: '%',
179185
MYSQL_ROOT_PASSWORD:
@@ -184,7 +190,7 @@ module.exports = function buildDockerComposeConfig( config ) {
184190
},
185191
'tests-mysql': {
186192
image: 'mariadb:lts',
187-
ports: [ '3306' ],
193+
ports: [ testsMysqlPorts ],
188194
environment: {
189195
MYSQL_ROOT_HOST: '%',
190196
MYSQL_ROOT_PASSWORD:

packages/env/lib/config/get-config-from-environment-vars.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const { checkPort, checkVersion, checkString } = require( './validate-config' );
1717
*
1818
* @typedef WPEnvironmentVariableConfig
1919
* @property {?number} port An override for the development environment's port.
20+
* @property {?number} mysqlPort An override for the development environment's MySQL port.
2021
* @property {?number} testsPort An override for the testing environment's port.
22+
* @property {?number} testsMysqlPort An override for the testing environment's MySQL port.
2123
* @property {?WPSource} coreSource An override for all environment's coreSource.
2224
* @property {?string} phpVersion An override for all environment's PHP version.
2325
* @property {?Object.<string, string>} lifecycleScripts An override for various lifecycle scripts.
@@ -33,7 +35,11 @@ const { checkPort, checkVersion, checkString } = require( './validate-config' );
3335
module.exports = function getConfigFromEnvironmentVars( cacheDirectoryPath ) {
3436
const environmentConfig = {
3537
port: getPortFromEnvironmentVariable( 'WP_ENV_PORT' ),
38+
mysqlPort: getPortFromEnvironmentVariable( 'WP_ENV_MYSQL_PORT' ),
3639
testsPort: getPortFromEnvironmentVariable( 'WP_ENV_TESTS_PORT' ),
40+
testsMysqlPort: getPortFromEnvironmentVariable(
41+
'WP_ENV_TESTS_MYSQL_PORT'
42+
),
3743
lifecycleScripts: getLifecycleScriptOverrides(),
3844
};
3945

packages/env/lib/config/parse-config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const mergeConfigs = require( './merge-configs' );
5050
* @property {WPSource[]} pluginSources Plugins to load in the environment.
5151
* @property {WPSource[]} themeSources Themes to load in the environment.
5252
* @property {number} port The port to use.
53+
* @property {number} mysqlPort The port to use for MySQL. Random if empty.
5354
* @property {Object} config Mapping of wp-config.php constants to their desired values.
5455
* @property {Object.<string, WPSource>} mappings Mapping of WordPress directories to local directories which should be mounted.
5556
* @property {string|null} phpVersion Version of PHP to use in the environments, of the format 0.0.
@@ -85,6 +86,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = {
8586
themes: [],
8687
port: 8888,
8788
testsPort: 8889,
89+
mysqlPort: null,
8890
mappings: {},
8991
config: {
9092
FS_METHOD: 'direct',
@@ -276,11 +278,19 @@ function getEnvironmentVarOverrides( cacheDirectoryPath ) {
276278
overrideConfig.env.development.port = overrides.port;
277279
}
278280

281+
if ( overrides.mysqlPort ) {
282+
overrideConfig.env.development.mysqlPort = overrides.mysqlPort;
283+
}
284+
279285
if ( overrides.testsPort ) {
280286
overrideConfig.testsPort = overrides.testsPort;
281287
overrideConfig.env.tests.port = overrides.testsPort;
282288
}
283289

290+
if ( overrides.testsMysqlPort ) {
291+
overrideConfig.env.tests.mysqlPort = overrides.testsMysqlPort;
292+
}
293+
284294
if ( overrides.coreSource ) {
285295
overrideConfig.coreSource = overrides.coreSource;
286296
overrideConfig.env.development.coreSource = overrides.coreSource;
@@ -436,6 +446,10 @@ async function parseEnvironmentConfig(
436446
parsedConfig.port = config.port;
437447
}
438448

449+
if ( config.mysqlPort !== undefined ) {
450+
parsedConfig.mysqlPort = config.mysqlPort;
451+
}
452+
439453
if ( config.phpVersion !== undefined ) {
440454
// Support null as a valid input.
441455
if ( config.phpVersion !== null ) {

packages/env/lib/config/test/__snapshots__/config-integration.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ exports[`Config Integration should load local and override configuration files 1
2929
"url": "https://github.com/WordPress/WordPress.git",
3030
},
3131
"mappings": {},
32+
"mysqlPort": 23306,
3233
"phpVersion": null,
3334
"pluginSources": [],
3435
"port": 999,
@@ -57,6 +58,7 @@ exports[`Config Integration should load local and override configuration files 1
5758
"url": "https://github.com/WordPress/WordPress.git",
5859
},
5960
"mappings": {},
61+
"mysqlPort": 23307,
6062
"phpVersion": null,
6163
"pluginSources": [],
6264
"port": 456,
@@ -102,6 +104,7 @@ exports[`Config Integration should load local configuration file 1`] = `
102104
"url": "https://github.com/WordPress/WordPress.git",
103105
},
104106
"mappings": {},
107+
"mysqlPort": 13306,
105108
"phpVersion": null,
106109
"pluginSources": [],
107110
"port": 123,
@@ -130,6 +133,7 @@ exports[`Config Integration should load local configuration file 1`] = `
130133
"url": "https://github.com/WordPress/WordPress.git",
131134
},
132135
"mappings": {},
136+
"mysqlPort": 23307,
133137
"phpVersion": null,
134138
"pluginSources": [],
135139
"port": 8889,
@@ -175,6 +179,7 @@ exports[`Config Integration should use default configuration 1`] = `
175179
"url": "https://github.com/WordPress/WordPress.git",
176180
},
177181
"mappings": {},
182+
"mysqlPort": null,
178183
"phpVersion": null,
179184
"pluginSources": [],
180185
"port": 8888,
@@ -203,6 +208,7 @@ exports[`Config Integration should use default configuration 1`] = `
203208
"url": "https://github.com/WordPress/WordPress.git",
204209
},
205210
"mappings": {},
211+
"mysqlPort": null,
206212
"phpVersion": null,
207213
"pluginSources": [],
208214
"port": 8889,
@@ -248,6 +254,7 @@ exports[`Config Integration should use environment variables over local and over
248254
"url": "https://github.com/WordPress/WordPress.git",
249255
},
250256
"mappings": {},
257+
"mysqlPort": 23306,
251258
"phpVersion": null,
252259
"pluginSources": [],
253260
"port": 12345,
@@ -277,6 +284,7 @@ exports[`Config Integration should use environment variables over local and over
277284
"url": "https://github.com/WordPress/WordPress.git",
278285
},
279286
"mappings": {},
287+
"mysqlPort": 23307,
280288
"phpVersion": null,
281289
"pluginSources": [],
282290
"port": 61234,

packages/env/lib/config/test/config-integration.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ describe( 'Config Integration', () => {
4848
afterEach( () => {
4949
delete process.env.WP_ENV_HOME;
5050
delete process.env.WP_ENV_PORT;
51+
delete process.env.WP_ENV_MYSQL_PORT;
5152
delete process.env.WP_ENV_TESTS_PORT;
53+
delete process.env.WP_ENV_TESTS_MYSQL_PORT;
5254
delete process.env.WP_ENV_LIFECYCLE_SCRIPT_AFTER_START;
5355
} );
5456

@@ -61,6 +63,8 @@ describe( 'Config Integration', () => {
6163

6264
expect( config.env.development.port ).toEqual( 8888 );
6365
expect( config.env.tests.port ).toEqual( 8889 );
66+
expect( config.env.development.mysqlPort ).toEqual( null );
67+
expect( config.env.tests.mysqlPort ).toEqual( null );
6468
expect( config ).toMatchSnapshot();
6569
} );
6670

@@ -75,6 +79,14 @@ describe( 'Config Integration', () => {
7579
afterClean: null,
7680
afterDestroy: null,
7781
},
82+
env: {
83+
development: {
84+
mysqlPort: 13306,
85+
},
86+
tests: {
87+
mysqlPort: 23307,
88+
},
89+
},
7890
} );
7991
}
8092

@@ -85,6 +97,8 @@ describe( 'Config Integration', () => {
8597

8698
expect( config.env.development.port ).toEqual( 123 );
8799
expect( config.env.tests.port ).toEqual( 8889 );
100+
expect( config.env.development.mysqlPort ).toEqual( 13306 );
101+
expect( config.env.tests.mysqlPort ).toEqual( 23307 );
88102
expect( config ).toMatchSnapshot();
89103
} );
90104

@@ -100,6 +114,11 @@ describe( 'Config Integration', () => {
100114
afterClean: null,
101115
afterDestroy: null,
102116
},
117+
env: {
118+
tests: {
119+
mysqlPort: 13306,
120+
},
121+
},
103122
} );
104123
}
105124

@@ -111,6 +130,14 @@ describe( 'Config Integration', () => {
111130
afterClean: null,
112131
afterDestroy: 'test',
113132
},
133+
env: {
134+
development: {
135+
mysqlPort: 23306,
136+
},
137+
tests: {
138+
mysqlPort: 23307,
139+
},
140+
},
114141
} );
115142
}
116143

@@ -121,12 +148,16 @@ describe( 'Config Integration', () => {
121148

122149
expect( config.env.development.port ).toEqual( 999 );
123150
expect( config.env.tests.port ).toEqual( 456 );
151+
expect( config.env.development.mysqlPort ).toEqual( 23306 );
152+
expect( config.env.tests.mysqlPort ).toEqual( 23307 );
124153
expect( config ).toMatchSnapshot();
125154
} );
126155

127156
it( 'should use environment variables over local and override configuration files', async () => {
128157
process.env.WP_ENV_PORT = 12345;
158+
process.env.WP_ENV_MYSQL_PORT = 23306;
129159
process.env.WP_ENV_TESTS_PORT = 61234;
160+
process.env.WP_ENV_TESTS_MYSQL_PORT = 23307;
130161
process.env.WP_ENV_LIFECYCLE_SCRIPT_AFTER_START = 'test';
131162

132163
readFile.mockImplementation( async ( fileName ) => {
@@ -140,6 +171,11 @@ describe( 'Config Integration', () => {
140171
afterClean: null,
141172
afterDestroy: null,
142173
},
174+
env: {
175+
tests: {
176+
mysqlPort: 13306,
177+
},
178+
},
143179
} );
144180
}
145181

@@ -156,6 +192,8 @@ describe( 'Config Integration', () => {
156192

157193
expect( config.env.development.port ).toEqual( 12345 );
158194
expect( config.env.tests.port ).toEqual( 61234 );
195+
expect( config.env.development.mysqlPort ).toEqual( 23306 );
196+
expect( config.env.tests.mysqlPort ).toEqual( 23307 );
159197
expect( config.lifecycleScripts ).toHaveProperty(
160198
'afterStart',
161199
'test'

packages/env/lib/config/test/parse-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jest.mock( '../../wordpress', () => ( {
2121
const DEFAULT_CONFIG = {
2222
port: 8888,
2323
testsPort: 8889,
24+
mysqlPort: null,
2425
phpVersion: null,
2526
coreSource: {
2627
type: 'git',

0 commit comments

Comments
 (0)