-
Notifications
You must be signed in to change notification settings - Fork 10
fix(maven): resolve version ranges in component analysis #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,8 @@ static Stream<String> testFolders() { | |
| "deps_with_ignore_on_version", | ||
| "deps_with_ignore_on_wrong", | ||
| "deps_with_no_ignore", | ||
| "pom_deps_with_no_ignore_common_paths"); | ||
| "pom_deps_with_no_ignore_common_paths", | ||
| "deps_with_version_range"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
|
|
@@ -143,12 +144,29 @@ void test_the_provideComponent(String testFolder) throws IOException { | |
| getClass(), String.format("tst_manifests/maven/%s/effectivePom.xml", testFolder))) { | ||
| effectivePom = new String(is.readAllBytes()); | ||
| } | ||
|
|
||
| String depTree; | ||
| try (var is = | ||
| getResourceAsStreamDecision( | ||
| getClass(), String.format("tst_manifests/maven/%s/depTree.txt", testFolder))) { | ||
| depTree = new String(is.readAllBytes()); | ||
| } | ||
|
|
||
| try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) { | ||
| mockedOperations | ||
| .when(() -> Operations.runProcess(any(), any(), any())) | ||
| .thenAnswer( | ||
| invocationOnMock -> | ||
| getOutputFileAndOverwriteItWithMock(effectivePom, invocationOnMock, "-Doutput")); | ||
| invocationOnMock -> { | ||
|
Comment on lines
155
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add a test ensuring we fall back gracefully when the dependency:tree invocation fails The current tests only cover the successful path where
This will lock in the intended failure behavior and guard against regressions in error handling. Suggested implementation: mockedOperations
.when(() -> Operations.runProcess(any(), any(), any()))
.thenAnswer(
invocationOnMock -> {
String result =
getOutputFileAndOverwriteItWithMock(
effectivePom, invocationOnMock, "-Doutput=");
if (result == null) {
result =
getOutputFileAndOverwriteItWithMock(
depTree, invocationOnMock, "-DoutputFile");
}
return result;
});
// Mock Operations.getCustomPathOrElse to return "mvn"
mockedOperations.when(() -> Operations.getCustomPathOrElse(anyString())).thenReturn("mvn");To implement the requested test coverage for the failure path of the
@Test
void testProvideComponent_DependencyTreeFailureFallsBackGracefully() throws Exception {
String testFolder = "basic";
String effectivePom;
try (var is =
getResourceAsStreamDecision(
getClass(), String.format("tst_manifests/maven/%s/effectivePom.xml", testFolder))) {
effectivePom = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) {
mockedOperations
.when(() -> Operations.runProcess(any(), any(), any()))
.thenAnswer(
invocationOnMock -> {
// First, handle the effective POM generation as usual
String result =
getOutputFileAndOverwriteItWithMock(
effectivePom, invocationOnMock, "-Doutput=");
if (result == null) {
// Simulate failure of the dependency:tree invocation (-DoutputFile)
throw new IOException("Simulated dependency:tree failure");
}
return result;
});
mockedOperations.when(() -> Operations.getCustomPathOrElse(anyString())).thenReturn("mvn");
Java_Maven_Provider provider = new Java_Maven_Provider();
Optional<Component> componentOpt = provider.provideComponent(testFolder);
// Verify that the provider swallowed the failure and still produced a component / SBOM
assertThat(componentOpt).isPresent();
Component component = componentOpt.get();
assertThat(component).isNotNull();
// If your existing tests assert on SBOM contents, add similar assertions here.
// For example, if unresolved versions are expected when dep tree is missing:
// assertThat(component.getVersion()).isNull();
// or check for whatever "unresolved" representation you're currently using.
}
}
@Test
void testProvideComponentWithPath_DependencyTreeFailureFallsBackGracefully() throws Exception {
String testFolder = "basic";
String effectivePom;
try (var is =
getResourceAsStreamDecision(
getClass(), String.format("tst_manifests/maven/%s/effectivePom.xml", testFolder))) {
effectivePom = new String(is.readAllBytes(), StandardCharsets.UTF_8);
}
Path projectPath =
Paths.get("src", "test", "resources", "tst_manifests", "maven", testFolder).toAbsolutePath();
try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) {
mockedOperations
.when(() -> Operations.runProcess(any(), any(), any()))
.thenAnswer(
invocationOnMock -> {
String result =
getOutputFileAndOverwriteItWithMock(
effectivePom, invocationOnMock, "-Doutput=");
if (result == null) {
// Simulate failure of the dependency:tree invocation (-DoutputFile)
throw new IOException("Simulated dependency:tree failure");
}
return result;
});
mockedOperations.when(() -> Operations.getCustomPathOrElse(anyString())).thenReturn("mvn");
Java_Maven_Provider provider = new Java_Maven_Provider();
Optional<Component> componentOpt = provider.provideComponent_With_Path(projectPath);
// Verify that the provider swallowed the failure and still produced a component / SBOM
assertThat(componentOpt).isPresent();
Component component = componentOpt.get();
assertThat(component).isNotNull();
// As above, assert on unresolved versions / missing dep-tree information as appropriate.
}
}
|
||
| String result = | ||
| getOutputFileAndOverwriteItWithMock( | ||
| effectivePom, invocationOnMock, "-Doutput="); | ||
| if (result == null) { | ||
| result = | ||
| getOutputFileAndOverwriteItWithMock( | ||
| depTree, invocationOnMock, "-DoutputFile"); | ||
| } | ||
| return result; | ||
| }); | ||
| // Mock Operations.getCustomPathOrElse to return "mvn" | ||
| mockedOperations.when(() -> Operations.getCustomPathOrElse(anyString())).thenReturn("mvn"); | ||
| mockedOperations | ||
|
|
@@ -189,12 +207,29 @@ void test_the_provideComponent_With_Path(String testFolder) throws IOException { | |
| getClass(), String.format("tst_manifests/maven/%s/effectivePom.xml", testFolder))) { | ||
| effectivePom = new String(is.readAllBytes()); | ||
| } | ||
|
|
||
| String depTree; | ||
| try (var is = | ||
| getResourceAsStreamDecision( | ||
| getClass(), String.format("tst_manifests/maven/%s/depTree.txt", testFolder))) { | ||
| depTree = new String(is.readAllBytes()); | ||
| } | ||
|
|
||
| try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) { | ||
| mockedOperations | ||
| .when(() -> Operations.runProcess(any(), any(), any())) | ||
| .thenAnswer( | ||
| invocationOnMock -> | ||
| getOutputFileAndOverwriteItWithMock(effectivePom, invocationOnMock, "-Doutput")); | ||
| invocationOnMock -> { | ||
| String result = | ||
| getOutputFileAndOverwriteItWithMock( | ||
| effectivePom, invocationOnMock, "-Doutput="); | ||
| if (result == null) { | ||
| result = | ||
| getOutputFileAndOverwriteItWithMock( | ||
| depTree, invocationOnMock, "-DoutputFile"); | ||
| } | ||
| return result; | ||
| }); | ||
| // Mock Operations.getCustomPathOrElse to return "mvn" | ||
| mockedOperations.when(() -> Operations.getCustomPathOrElse(anyString())).thenReturn("mvn"); | ||
| mockedOperations | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pom-with-deps-version-range:pom-with-version-range-for-tests:jar:0.0.1 | ||
| +- log4j:log4j:jar:1.2.17:compile | ||
| \- org.xerial.snappy:snappy-java:jar:1.1.10.0:compile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Deleting the temp file can throw and violate the "return original list" fallback guarantee
Per the Javadoc, failures in resolving version ranges should result in the original list being returned unchanged. However,
Files.deleteIfExists(tmpFile)in thefinallyblock can throw anIOExceptionthat bypasses the existingcatch (Exception e)and leaks out of the method, violating that contract. Please isolate the delete in its own try/catch (logging or ignoring failures) so cleanup errors don’t alter the method’s behavior, or adjust the signature/contract if you intend to let such IOExceptions propagate.