Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tools/release/commands/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,21 +723,24 @@ function isNpmPackageVersionMissing( error ) {
}

/**
* Parses npm JSON command output.
* Parses npm JSON command output for a single package.
*
* @param {string} output Command stdout.
* @param {string} description Output description for error messages.
*
* @return {*} Parsed JSON output.
*/
function parseNpmJsonOutput( output, description ) {
let parsed;
try {
return JSON.parse( output );
parsed = JSON.parse( output );
} catch {
throw new Error(
`Unable to parse npm registry ${ description }: ${ output }`
);
}
/* npm v12 always wraps `view --json` in an array; older versions return the object. */
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
return Array.isArray( parsed ) ? parsed[ 0 ] : parsed;
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
}

/**
Expand Down
56 changes: 33 additions & 23 deletions tools/release/commands/test/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,30 +549,40 @@ describe( 'runNpmPublishPreflight', () => {
expect( console ).toHaveLogged();
} );

it( 'accepts a published version from the prepared commit with the expected dist-tag', async () => {
const commandFn = vi
.fn()
.mockResolvedValueOnce( WHOAMI )
.mockResolvedValueOnce( {
stdout: '{"version":"4.50.0","gitHead":"publish-sha","dist-tags":{"latest":"4.50.0"}}',
} );
it.each( [
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
[
'an object, as npm v11 returns',
'{"version":"4.50.0","gitHead":"publish-sha","dist-tags":{"latest":"4.50.0"}}',
],
[
'an array, as npm v12 returns',
'[{"version":"4.50.0","gitHead":"publish-sha","dist-tags":{"latest":"4.50.0"}}]',
],
] )(
'accepts a published version from the prepared commit with the expected dist-tag when npm view returns %s',
async ( _description, stdout ) => {
const commandFn = vi
.fn()
.mockResolvedValueOnce( WHOAMI )
.mockResolvedValueOnce( { stdout } );

await expect(
runNpmPublishPreflight(
{
distTag: 'latest',
gitWorkingDirectoryPath: '/repo',
publishCommit: 'publish-sha',
releasePackages: [
{ name: '@wordpress/a11y', version: '4.50.0' },
],
},
{ commandFn }
)
).resolves.toEqual( [ '@wordpress/a11y' ] );
expect( commandFn ).toHaveBeenCalledTimes( 2 );
expect( console ).toHaveLogged();
} );
await expect(
runNpmPublishPreflight(
{
distTag: 'latest',
gitWorkingDirectoryPath: '/repo',
publishCommit: 'publish-sha',
releasePackages: [
{ name: '@wordpress/a11y', version: '4.50.0' },
],
},
{ commandFn }
)
).resolves.toEqual( [ '@wordpress/a11y' ] );
expect( commandFn ).toHaveBeenCalledTimes( 2 );
expect( console ).toHaveLogged();
}
);

it( 'fails when a published version came from another commit', async () => {
const commandFn = vi
Expand Down
6 changes: 5 additions & 1 deletion tools/validation/test-vitest-consumers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ function pack( name ) {
directory
)
);
return `file:${ path.join( directory, output[ 0 ].filename ) }`;
/* npm v12 keys `pack --json` output by package name; older versions return an array. */
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
const [ packed ] = Array.isArray( output )
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
? output
: Object.values( output );
return `file:${ path.join( directory, packed.filename ) }`;
}
const scripts = values.scripts ?? pack( 'scripts' );
const eslintPlugin = values[ 'eslint-plugin' ] ?? pack( 'eslint-plugin' );
Expand Down
Loading