Skip to content

Commit

Permalink
wp-env: Correctly parse basename for wporg zip sources (#22093)
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed May 22, 2020
1 parent 2102f7a commit 189da69
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/env/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,19 @@ function parseSourceString( sourceString, { workDirectoryPath } ) {
const zipFields = sourceString.match(
/^https?:\/\/([^\s$.?#].[^\s]*)\.zip$/
);

if ( zipFields ) {
const wpOrgFields = sourceString.match(
/^https?:\/\/downloads\.wordpress\.org\/(?:plugin|theme)\/([^\s\.]*)([^\s]*)?\.zip$/
);
const basename = wpOrgFields
? encodeURIComponent( wpOrgFields[ 1 ] )
: encodeURIComponent( zipFields[ 1 ] );
return {
type: 'zip',
url: sourceString,
path: path.resolve(
workDirectoryPath,
encodeURIComponent( zipFields[ 1 ] )
),
basename: encodeURIComponent( zipFields[ 1 ] ),
path: path.resolve( workDirectoryPath, basename ),
basename,
};
}

Expand Down
47 changes: 47 additions & 0 deletions packages/env/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,53 @@ describe( 'readConfig', () => {
} );
} );

it( 'should parse wordpress.org sources', async () => {
readFile.mockImplementation( () =>
Promise.resolve(
JSON.stringify( {
plugins: [
'https://downloads.wordpress.org/plugin/gutenberg.zip',
'https://downloads.wordpress.org/plugin/gutenberg.8.1.0.zip',
'https://downloads.wordpress.org/theme/twentytwenty.zip',
'https://downloads.wordpress.org/theme/twentytwenty.1.3.zip',
],
} )
)
);
const config = await readConfig( '.wp-env.json' );
expect( config ).toMatchObject( {
pluginSources: [
{
type: 'zip',
url: 'https://downloads.wordpress.org/plugin/gutenberg.zip',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/plugin/gutenberg.8.1.0.zip',
path: expect.stringMatching( /^\/.*gutenberg$/ ),
basename: 'gutenberg',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/theme/twentytwenty.zip',
path: expect.stringMatching( /^\/.*twentytwenty$/ ),
basename: 'twentytwenty',
},
{
type: 'zip',
url:
'https://downloads.wordpress.org/theme/twentytwenty.1.3.zip',
path: expect.stringMatching( /^\/.*twentytwenty$/ ),
basename: 'twentytwenty',
},
],
} );
} );

it( 'should throw a validaton error if there is an unknown source', async () => {
readFile.mockImplementation( () =>
Promise.resolve( JSON.stringify( { plugins: [ 'invalid' ] } ) )
Expand Down

0 comments on commit 189da69

Please sign in to comment.