From be5fa79a4770fea27fb1d79888dde23f953f845e Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 3 Nov 2020 13:08:16 -0600 Subject: [PATCH] wp-env: Add support for custom WP_HOME port (#26507) This checks for whether a custom port has been set for `WP_HOME` in the .wp-env.json file before appending the environment's to support local environments where the front end needs to be pointed at another port (e.g., a headless app). Resolves #26481 --- packages/env/lib/config/config.js | 7 ++++- packages/env/lib/config/test/config.js | 36 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/env/lib/config/config.js b/packages/env/lib/config/config.js index 487d82f83d9bf..eac10983632fb 100644 --- a/packages/env/lib/config/config.js +++ b/packages/env/lib/config/config.js @@ -259,7 +259,12 @@ function withOverrides( config ) { const baseUrl = new URL( config.env[ envKey ].config[ configKey ] ); - baseUrl.port = config.env[ envKey ].port; + + // Don't overwrite the port of WP_HOME when set. + if ( ! ( configKey === 'WP_HOME' && !! baseUrl.port ) ) { + baseUrl.port = config.env[ envKey ].port; + } + config.env[ envKey ].config[ configKey ] = baseUrl.toString(); } catch ( error ) { throw new ValidationError( diff --git a/packages/env/lib/config/test/config.js b/packages/env/lib/config/test/config.js index 3fd20c7010822..7c784af07f8a5 100644 --- a/packages/env/lib/config/test/config.js +++ b/packages/env/lib/config/test/config.js @@ -731,6 +731,42 @@ describe( 'readConfig', () => { } ); } ); + it( 'should not overwrite port number for WP_HOME if set', async () => { + readFile.mockImplementation( () => + Promise.resolve( + JSON.stringify( { + port: 1000, + testsPort: 2000, + config: { + WP_HOME: 'http://localhost:3000/', + }, + } ) + ) + ); + const config = await readConfig( '.wp-env.json' ); + // Custom port is overriden while testsPort gets the deault value. + expect( config ).toMatchObject( { + env: { + development: { + port: 1000, + config: { + WP_TESTS_DOMAIN: 'http://localhost:1000/', + WP_SITEURL: 'http://localhost:1000/', + WP_HOME: 'http://localhost:3000/', + }, + }, + tests: { + port: 2000, + config: { + WP_TESTS_DOMAIN: 'http://localhost:2000/', + WP_SITEURL: 'http://localhost:2000/', + WP_HOME: 'http://localhost:3000/', + }, + }, + }, + } ); + } ); + it( 'should throw an error if the port number environment variable is invalid', async () => { readFile.mockImplementation( () => Promise.resolve( JSON.stringify( {} ) )