Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/main/java/com/redhat/exhort/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ protected Provider(Ecosystem.Type ecosystem, Path manifest) {
*/
public abstract Content provideComponent() throws IOException;

public boolean validateLockFile(Path lockFile) {
return true;
/**
* If a package manager requires having a lock file it must exist in the provided path
*
* @param lockFileDir Path to the directory where the lock file must exist
*/
public void validateLockFile(Path lockFileDir) {
// Default implementation. Do not require a lock file.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,12 @@ Map<String, String> getNpmExecEnv() {
}
return null;
}

@Override
public void validateLockFile(Path lockFileDir) {
if (!Files.isRegularFile(lockFileDir.resolve("package-lock.json"))) {
throw new IllegalStateException(
"Lock file does not exist or is not supported. Execute 'npm install' to generate it.");
}
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/redhat/exhort/tools/Ecosystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ private Ecosystem() {
*/
public static Provider getProvider(final Path manifestPath) {
var provider = resolveProvider(manifestPath);
if (!provider.validateLockFile(manifestPath)) {
throw new IllegalStateException(
"Missing lock file for manifest file: " + manifestPath.toString());
}
provider.validateLockFile(manifestPath.getParent());
return provider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ void Test_Golang_Modules_with_Match_Manifest_Version(boolean MatchManifestVersio
String actualSbomWithTSStripped = dropIgnoredKeepFormat(sbomString);

assertEquals(
getStringFromFile("msc/golang/expected_sbom_ca.json").trim(), actualSbomWithTSStripped);
dropIgnored(getStringFromFile("msc/golang/expected_sbom_ca.json")).trim(),
dropIgnored(actualSbomWithTSStripped));
}
}

Expand All @@ -163,7 +164,7 @@ void Test_Golang_MvS_Logic_Enabled() throws IOException {
goModulesProvider.getDependenciesSbom(Path.of(goModPath), true).getAsJsonString());
String expectedSbom =
getStringFromFile("msc/golang/mvs_logic/expected_sbom_stack_analysis.json").trim();
assertEquals(expectedSbom, resultSbom);
assertEquals(dropIgnored(expectedSbom), dropIgnored(resultSbom));

// check that only one version of package golang/go.opencensus.io is in sbom for
// EXHORT_GO_MVS_LOGIC_ENABLED=true
Expand All @@ -188,10 +189,13 @@ void Test_Golang_MvS_Logic_Enabled() throws IOException {
}

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

private String dropIgnoredKeepFormat(String s) {
return s.replaceAll("\"timestamp\" : \"[a-zA-Z0-9\\-\\:]+\",\n ", "");
return s.replaceAll("goarch=\\w+&goos=\\w+&", "")
.replaceAll("\"timestamp\" : \"[a-zA-Z0-9\\-\\:]+\",\n ", "");
}
}
7 changes: 7 additions & 0 deletions src/test/java/com/redhat/exhort/tools/Ecosystem_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.redhat.exhort.providers.JavaMavenProvider;
import java.nio.file.Path;
Expand All @@ -36,4 +37,10 @@ void get_a_provider_for_a_pom_xml_file_should_return_java_maven_manifest() {
var manifestPath = Path.of("/supported/manifest/pom.xml");
assertThat(Ecosystem.getProvider(manifestPath)).isInstanceOf(JavaMavenProvider.class);
}

@Test
void get_a_provider_with_missing_lock_file() {
var manifestPath = Path.of("src/test/resources/tst_manifests/npm/empty/package.json");
assertThrows(IllegalStateException.class, () -> Ecosystem.getProvider(manifestPath));
}
}
Loading