Skip to content

Commit

Permalink
Do not break on invalid versions
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Aug 5, 2024
1 parent d4678f4 commit 3c8973e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main/java/org/openrewrite/nodejs/NodeResolutionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.Value;
import org.openrewrite.Validated;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.json.tree.Json;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;

import java.util.*;

Expand Down Expand Up @@ -71,9 +73,12 @@ public static NodeResolutionResult fromPackageLockJson(Json.Document lockfileJso
if (pkg.getDependencies() != null) {
transitive = new ArrayList<>(pkg.getDependencies().size());
pkg.getDependencies().forEach((name, version) -> {
Dependency d = new Dependency(name, Semver.validate(version, null).getValue(), null);
transitive.add(d);
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(d);
Validated<VersionComparator> validatedVersion = Semver.validate(version, null);
if (validatedVersion.isValid()) {
Dependency d = new Dependency(name, validatedVersion.getValue(), null);
transitive.add(d);
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(d);
}
});
} else {
transitive = emptyList();
Expand All @@ -89,29 +94,32 @@ public static NodeResolutionResult fromPackageLockJson(Json.Document lockfileJso
}
if (pkg.getDependencies() != null) {
pkg.getDependencies().forEach((name, version) -> {
Dependency dep = new Dependency(name, Semver.validate(version, null).getValue(), null);
if (isRoot) {
dependencies.add(dep);
Validated<VersionComparator> validatedVersion = Semver.validate(version, null);
if (validatedVersion.isValid()) {
Dependency dep = new Dependency(name, validatedVersion.getValue(), null);
if (isRoot) {
dependencies.add(dep);
}
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(dep);
}
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(dep);
});
}
if (pkg.getDevDependencies() != null) {
pkg.getDevDependencies().forEach((name, version) -> {
Dependency dep = new Dependency(name, Semver.validate(version, null).getValue(), null);
if (isRoot) {
devDependencies.add(dep);
Validated<VersionComparator> validatedVersion = Semver.validate(version, null);
if (validatedVersion.isValid()) {
Dependency dep = new Dependency(name, validatedVersion.getValue(), null);
if (isRoot) {
devDependencies.add(dep);
}
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(dep);
}
byName.computeIfAbsent(name, n -> new ArrayList<>()).add(dep);
});
}
});
} catch (
JsonProcessingException ignored) {
} catch (JsonProcessingException ignored) {
}
return new

NodeResolutionResult(dependencies, devDependencies);
return new NodeResolutionResult(dependencies, devDependencies);
}

@Value
Expand Down
117 changes: 117 additions & 0 deletions src/test/java/org/openrewrite/nodejs/NodeResolutionResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.nodejs;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.json.JsonIsoVisitor;
import org.openrewrite.json.tree.Json;
import org.openrewrite.nodejs.search.IsPackageLockJson;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.json.Assertions.json;
import static org.openrewrite.test.RewriteTest.toRecipe;

class NodeResolutionResultTest implements RewriteTest {

@Test
@DocumentExample
void invalidVersionsShouldNotBreakResolution() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> Preconditions.check(
new IsPackageLockJson<>(),
new JsonIsoVisitor<>() {
@Override
public Json.Document visitDocument(Json.Document document, ExecutionContext ctx) {
NodeResolutionResult nodeResolutionResult = NodeResolutionResult.fromPackageLockJson(document);
assertThat(nodeResolutionResult.getDependencies()).singleElement()
.satisfies(dependency -> assertThat(dependency.getName()).isEqualTo("rxfire"));
// rxjs version is invalid, but it should not break resolution
return document;
}
}
))),
json(
//language=json
"""
{
"name": "nebular",
"version": "13.0.0",
"license": "MIT",
"author": "akveo <[email protected]>",
"private": true,
"bugs": {
"url": "https://github.com/akveo/nebular/issues"
},
"homepage": "https://github.com/akveo/nebular#readme",
"repository": "git+https://github.com/akveo/nebular.git",
"dependencies": {
"rxfire": "^6.0.0",
"rxjs": "^6.5.3 || ^7.4.0"
}
}
""",
spec -> spec.path("package.json")
),
json(
//language=json
"""
{
"name": "nebular",
"version": "13.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "nebular",
"version": "13.0.0",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"rxfire": "^6.0.0",
"rxjs": "^6.5.3 || ^7.4.0"
}
},
"node_modules/rxfire": {
"version": "6.0.5",
"resolved": "https://registry.npmjs.org/rxfire/-/rxfire-6.0.5.tgz",
"integrity": "sha512-ycBsANGbya3GNtOBKzZVATLEV+0S9gUrlTfwnN15TCXtgG8OgIMAuv2k9+kMeVaevp/DRp1KT+vYf6Wkop6gvw==",
"peerDependencies": {
"rxjs": "^6.0.0 || ^7.0.0"
}
},
"node_modules/rxjs": {
"version": "6.6.7",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz",
"integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==",
"dependencies": {
"tslib": "^1.9.0"
},
"engines": {
"npm": ">=2.0.0"
}
}
}
}
""",
spec -> spec.path("package-lock.json")
)
);
}
}

0 comments on commit 3c8973e

Please sign in to comment.