Skip to content

Commit

Permalink
Change npm bin field to object type to fix merging deserialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed Mar 6, 2024
1 parent 243230c commit f27327d
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.commonjava.indy.pkg.npm.content;

import org.apache.commons.io.IOUtils;
import org.commonjava.cdi.util.weft.PoolWeftExecutorService;
import org.commonjava.cdi.util.weft.WeftExecutorService;
import org.commonjava.indy.audit.ChangeSummary;
Expand Down Expand Up @@ -154,18 +155,18 @@ public void testSingleBinFieldWhenGenerateFromTarball() throws Exception

final KeyedLocation location = LocationUtils.toLocation( hostedRepository );

storeFile( location, "jquery/-/jquery-9.0.5.tgz", "tarball/version-bin-1.tgz");
storeFile( location, "jquery/-/jquery-9.0.6.tgz", "tarball/version-bin-2.tgz");
storeFile( location, "jquery/9.0.5", "metadata/version-bin-1.json" );
storeFile( location, "jquery/9.0.6", "metadata/version-bin-2.json" );
storeFile( location, "json/-/json-9.0.5.tgz", "tarball/version-bin-1.tgz");
storeFile( location, "json/-/json-9.0.6.tgz", "tarball/version-bin-2.tgz");
storeFile( location, "json/9.0.5", "metadata/version-bin-1.json" );
storeFile( location, "json/9.0.6", "metadata/version-bin-2.json" );

final String jqueryMetadataPath = "jquery/package.json";
final String jsonMetadataPath = "json/package.json";

// Check the package metadata before generation.
Transfer before = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
Transfer before = fileManager.retrieve( hostedRepository, jsonMetadataPath );
assertNull(before);

Transfer metadataFile = generator.generateFileContent( hostedRepository, jqueryMetadataPath, new EventMetadata( ) );
Transfer metadataFile = generator.generateFileContent( hostedRepository, jsonMetadataPath, new EventMetadata( ) );
assertNotNull(metadataFile);

final IndyObjectMapper mapper = new IndyObjectMapper( true );
Expand All @@ -175,14 +176,14 @@ public void testSingleBinFieldWhenGenerateFromTarball() throws Exception

assertNotNull( packageMetadata );
assertEquals( 2, packageMetadata.getVersions().size() );
assertEquals( 1, packageMetadata.getVersions().get( "9.0.5" ).getBin().size() );
assertEquals( 1, packageMetadata.getVersions().get( "9.0.6" ).getBin().size() );
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.5").getBin().get( "json" ));
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.6").getBin().get( "json" ));
assertNotNull( packageMetadata.getVersions().get( "9.0.5" ).getBin() );
assertNotNull( packageMetadata.getVersions().get( "9.0.6" ).getBin() );
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.5").getBin().toString() );
assertEquals("{json=./lib/json.js}", packageMetadata.getVersions().get("9.0.6").getBin().toString() );
}

// Check the package metadata after generation.
Transfer after = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
Transfer after = fileManager.retrieve( hostedRepository, jsonMetadataPath );
assertNotNull(after);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public void mergeTwoSimplePackageMetadataFiles() throws Exception
assertThat( keywords, notNullValue() );
assertThat( keywords.size(), equalTo( 4 ) );
assertThat( keywords.contains( "javascript" ), equalTo( true ) );

assertThat( merged.getVersions().get( "1.5.1" ).getBin().toString(), equalTo( "./lib/jquery.js" ) );
assertThat( merged.getVersions().get( "1.6.2" ).getBin().toString(), equalTo( "{jquery=./lib/jquery.js}" ) );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lib": "lib",
"main": "./dist/node-jquery.js",
"version": "1.5.1",
"bin": "./lib/jquery.js",
"_id": "[email protected]",
"engines": {
"node": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lib": "lib",
"main": "./dist/node-jquery.js",
"version": "1.5.1",
"bin": "./lib/jquery.js",
"_id": "[email protected]",
"engines": {
"node": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"contributors": false,
"serverjs": false
},
"bin": { "jquery": "./lib/jquery.js" },
"_id": "[email protected]",
"devDependencies": {},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "Trent Mick <[email protected]> (http://trentm.com)",
"main": "./lib/json.js",
"man": ["./man/man1/json.1"],
"bin": "./lib/json.js",
"bin": { "json": "./lib/json.js" },
"scripts": {
"test": "make test"
},
Expand Down
Binary file modified addons/pkg-npm/common/src/test/resources/tarball/version-bin-2.tgz
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void test()
// This public group version metadata in package metadata will follow npmjs remote
VersionMetadata publicVersion = mergedPublic.getVersions().get( "0.2.2" );
assertThat( publicVersion, notNullValue() );
assertThat( publicVersion.getBin().get( "locktt" ), equalTo( "dist/index.js" ) );
assertThat( publicVersion.getBin().toString(), equalTo( "{locktt=dist/index.js}" ) );
assertThat( publicVersion.getMaintainers().size(), equalTo( 4 ) );
assertThat(
publicVersion.getDist().getTarball().contains( "lock-treatment-tool/-/lock-treatment-tool-0.2.2.tgz" ),
Expand Down Expand Up @@ -168,7 +168,7 @@ public void test()
mapper.readValue( IOUtils.toString( sharedGroupInput2 ), PackageMetadata.class );
VersionMetadata mergedVersion2 = mergedShared2.getVersions().get( "0.2.2" );
assertThat( mergedVersion2, notNullValue() );
assertThat( mergedVersion2.getBin().get( "locktt" ), equalTo( "dist/index.js" ) );
assertThat( mergedVersion2.getBin().toString(), equalTo( "{locktt=dist/index.js}" ) );
assertThat( mergedVersion2.getMaintainers().size(), equalTo( 4 ) );
assertThat(
mergedVersion2.getDist().getTarball().contains( "lock-treatment-tool/-/lock-treatment-tool-0.2.2.tgz" ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public class VersionMetadata

private Map<String, Object> devDependencies;

@JsonDeserialize( converter = ObjectToBinConverter.class )
private Map<String, String> bin;
private Object bin;

private Map<String, String> jsdomVersions;

Expand Down Expand Up @@ -320,23 +319,12 @@ public void setDevDependencies( Map<String, Object> devDependencies )
this.devDependencies = devDependencies;
}

public Map<String, String> getBin()
public Object getBin()
{
if ( null == bin )
{
return null;
}
String value = bin.get( SINGLE_BIN );
if ( null != value )
{
bin.remove( SINGLE_BIN );
// ref https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin
bin.put( name, value );
}
return bin;
}

public void setBin( Map<String, String> bin )
public void setBin( Object bin )
{
this.bin = bin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public VersionMetadata deserialize( JsonParser jsonParser, DeserializationContex
vm.setLicense( parseObject( mapper, vNode.get( "license" ), License.class ) );
vm.setDependencies( parseObject( mapper, vNode.get( "dependencies" ), Map.class ) );
vm.setDevDependencies( parseObject( mapper, vNode.get( "devDependencies" ), Map.class ) );
vm.setBin( parseObject( mapper, vNode.get( "bin" ), Map.class ) );
vm.setBin( parseObject( mapper, vNode.get( "bin" ), Object.class ) );
vm.setJsdomVersions( parseObject( mapper, vNode.get( "jsdomVersions" ), Map.class ) );
vm.setScripts( parseObject( mapper, vNode.get( "scripts" ), Map.class ) );
vm.setPri( parseObject( mapper, vNode.get( "private" ), Boolean.class ) );
Expand Down

0 comments on commit f27327d

Please sign in to comment.