Skip to content

Commit 2a04e5e

Browse files
committed
fix: validate if npm has lock file
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 79d0578 commit 2a04e5e

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/main/java/com/redhat/exhort/Provider.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ protected Provider(Ecosystem.Type ecosystem, Path manifest) {
7070
*/
7171
public abstract Content provideComponent() throws IOException;
7272

73-
public boolean validateLockFile(Path lockFile) {
74-
return true;
73+
/**
74+
* If a package manager requires having a lock file it must exist in the provided path
75+
*
76+
* @param lockFileDir Path to the directory where the lock file must exist
77+
*/
78+
public void validateLockFile(Path lockFileDir) {
79+
// Default implementation. Do not require a lock file.
7580
}
7681
}

src/main/java/com/redhat/exhort/providers/JavaScriptNpmProvider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,12 @@ Map<String, String> getNpmExecEnv() {
232232
}
233233
return null;
234234
}
235+
236+
@Override
237+
public void validateLockFile(Path lockFileDir) {
238+
if (!Files.isRegularFile(lockFileDir.resolve("package-lock.json"))) {
239+
throw new IllegalStateException(
240+
"Lock file does not exist or is not supported. Execute 'npm install' to generate it.");
241+
}
242+
}
235243
}

src/main/java/com/redhat/exhort/tools/Ecosystem.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ private Ecosystem() {
5656
*/
5757
public static Provider getProvider(final Path manifestPath) {
5858
var provider = resolveProvider(manifestPath);
59-
if (!provider.validateLockFile(manifestPath)) {
60-
throw new IllegalStateException(
61-
"Missing lock file for manifest file: " + manifestPath.toString());
62-
}
59+
provider.validateLockFile(manifestPath.getParent());
6360
return provider;
6461
}
6562

src/test/java/com/redhat/exhort/providers/Golang_Modules_Provider_Test.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ void Test_Golang_Modules_with_Match_Manifest_Version(boolean MatchManifestVersio
149149
String actualSbomWithTSStripped = dropIgnoredKeepFormat(sbomString);
150150

151151
assertEquals(
152-
getStringFromFile("msc/golang/expected_sbom_ca.json").trim(), actualSbomWithTSStripped);
152+
dropIgnored(getStringFromFile("msc/golang/expected_sbom_ca.json")).trim(),
153+
dropIgnored(actualSbomWithTSStripped));
153154
}
154155
}
155156

@@ -188,10 +189,13 @@ void Test_Golang_MvS_Logic_Enabled() throws IOException {
188189
}
189190

190191
private String dropIgnored(String s) {
191-
return s.replaceAll("\\s+", "").replaceAll("\"timestamp\":\"[a-zA-Z0-9\\-\\:]+\",", "");
192+
return s.replaceAll("goarch=\\w+&goos=\\w+&", "")
193+
.replaceAll("\\s+", "")
194+
.replaceAll("\"timestamp\":\"[a-zA-Z0-9\\-\\:]+\",", "");
192195
}
193196

194197
private String dropIgnoredKeepFormat(String s) {
195-
return s.replaceAll("\"timestamp\" : \"[a-zA-Z0-9\\-\\:]+\",\n ", "");
198+
return s.replaceAll("goarch=\\w+&goos=\\w+&", "")
199+
.replaceAll("\"timestamp\" : \"[a-zA-Z0-9\\-\\:]+\",\n ", "");
196200
}
197201
}

src/test/java/com/redhat/exhort/tools/Ecosystem_Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

2122
import com.redhat.exhort.providers.JavaMavenProvider;
2223
import java.nio.file.Path;
@@ -36,4 +37,10 @@ void get_a_provider_for_a_pom_xml_file_should_return_java_maven_manifest() {
3637
var manifestPath = Path.of("/supported/manifest/pom.xml");
3738
assertThat(Ecosystem.getProvider(manifestPath)).isInstanceOf(JavaMavenProvider.class);
3839
}
40+
41+
@Test
42+
void get_a_provider_with_missing_lock_file() {
43+
var manifestPath = Path.of("src/test/resources/tst_manifests/npm/empty/package.json");
44+
assertThrows(IllegalStateException.class, () -> Ecosystem.getProvider(manifestPath));
45+
}
3946
}

0 commit comments

Comments
 (0)