|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 Trustify Dependency Analytics Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.github.guacsec.trustifyda.impl; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import org.junit.jupiter.api.Assumptions; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Tag; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +@Tag("IntegrationTest") |
| 31 | +class MavenWorkspaceDiscoveryIT { |
| 32 | + |
| 33 | + private static final Path MAVEN_FIXTURES = |
| 34 | + Path.of("src/test/resources/tst_manifests/workspace/maven"); |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + static void requireMaven() { |
| 38 | + boolean mavenAvailable; |
| 39 | + try { |
| 40 | + Process p = new ProcessBuilder("mvn", "-v").redirectErrorStream(true).start(); |
| 41 | + mavenAvailable = p.waitFor() == 0; |
| 42 | + } catch (Exception e) { |
| 43 | + mavenAvailable = false; |
| 44 | + } |
| 45 | + Assumptions.assumeTrue(mavenAvailable, "mvn not available on PATH"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void discoverWorkspaceManifests_mavenMultiModule() throws IOException { |
| 50 | + Path workspaceDir = MAVEN_FIXTURES.resolve("maven_multi_module").toAbsolutePath().normalize(); |
| 51 | + ExhortApi api = new ExhortApi(); |
| 52 | + List<Path> manifests = api.discoverWorkspaceManifests(workspaceDir, Set.of()); |
| 53 | + |
| 54 | + assertThat(manifests).hasSize(3); |
| 55 | + assertThat(manifests).allMatch(p -> p.getFileName().toString().equals("pom.xml")); |
| 56 | + assertThat(manifests.getFirst()).isEqualTo(workspaceDir.resolve("pom.xml")); |
| 57 | + assertThat(manifests).anyMatch(p -> p.toString().contains("module-a")); |
| 58 | + assertThat(manifests).anyMatch(p -> p.toString().contains("module-b")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void discoverWorkspaceManifests_nestedAggregator() throws IOException { |
| 63 | + Path workspaceDir = |
| 64 | + MAVEN_FIXTURES.resolve("maven_nested_aggregator").toAbsolutePath().normalize(); |
| 65 | + ExhortApi api = new ExhortApi(); |
| 66 | + List<Path> manifests = api.discoverWorkspaceManifests(workspaceDir, Set.of()); |
| 67 | + |
| 68 | + assertThat(manifests).hasSize(3); |
| 69 | + assertThat(manifests.getFirst()).isEqualTo(workspaceDir.resolve("pom.xml")); |
| 70 | + assertThat(manifests) |
| 71 | + .anyMatch(p -> p.toString().contains("parent" + java.io.File.separator + "pom.xml")); |
| 72 | + assertThat(manifests) |
| 73 | + .anyMatch( |
| 74 | + p -> |
| 75 | + p.toString() |
| 76 | + .contains( |
| 77 | + "parent" |
| 78 | + + java.io.File.separator |
| 79 | + + "child" |
| 80 | + + java.io.File.separator |
| 81 | + + "pom.xml")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void discoverWorkspaceManifests_noModules() throws IOException { |
| 86 | + Path workspaceDir = MAVEN_FIXTURES.resolve("maven_no_modules").toAbsolutePath().normalize(); |
| 87 | + ExhortApi api = new ExhortApi(); |
| 88 | + List<Path> manifests = api.discoverWorkspaceManifests(workspaceDir, Set.of()); |
| 89 | + |
| 90 | + assertThat(manifests).hasSize(1); |
| 91 | + assertThat(manifests.getFirst()).isEqualTo(workspaceDir.resolve("pom.xml")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void discoverWorkspaceManifests_missingModuleDirectory() throws IOException { |
| 96 | + // Maven fails to read the POM when a declared module directory is missing, |
| 97 | + // so graceful degradation returns only the root pom.xml. |
| 98 | + Path workspaceDir = MAVEN_FIXTURES.resolve("maven_missing_module").toAbsolutePath().normalize(); |
| 99 | + ExhortApi api = new ExhortApi(); |
| 100 | + List<Path> manifests = api.discoverWorkspaceManifests(workspaceDir, Set.of()); |
| 101 | + |
| 102 | + assertThat(manifests).hasSize(1); |
| 103 | + assertThat(manifests.getFirst()).isEqualTo(workspaceDir.resolve("pom.xml")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void discoverWorkspaceManifests_ignorePatternFiltering() throws IOException { |
| 108 | + Path workspaceDir = MAVEN_FIXTURES.resolve("maven_multi_module").toAbsolutePath().normalize(); |
| 109 | + ExhortApi api = new ExhortApi(); |
| 110 | + List<Path> manifests = api.discoverWorkspaceManifests(workspaceDir, Set.of("**/module-b/**")); |
| 111 | + |
| 112 | + assertThat(manifests).anyMatch(p -> p.toString().contains("module-a")); |
| 113 | + assertThat(manifests).noneMatch(p -> p.toString().contains("module-b")); |
| 114 | + } |
| 115 | +} |
0 commit comments