Skip to content

Commit

Permalink
wp-env: Add support for custom WP_HOME port (#26507)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joemcgill committed Nov 3, 2020
1 parent 26806cb commit be5fa79
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/env/lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 36 additions & 0 deletions packages/env/lib/config/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( {} ) )
Expand Down

0 comments on commit be5fa79

Please sign in to comment.