diff --git a/docs/using-diffci.md b/docs/using-diffci.md index 8bdae3b..bd81109 100644 --- a/docs/using-diffci.md +++ b/docs/using-diffci.md @@ -688,6 +688,7 @@ _Module resource and dependency impact contributed by [@FredApps](https://github | `Maven analysis found no pom.xml` | A Java repository built with another tool | No | | `Maven reactor contains no Java sources` | A `pom.xml` with only resources | No | | `Maven …: custom source roots require full validation` | Set ``, or a `` that is interpolated (`${…}`), absolute, outside the module, declared more than once, inside a profile, or inherited from a parent POM | Declare a plain relative `` inside the module, in the module's own ``; DiffCI finds tests there | +| `Maven …: Java sources outside the standard roots require full validation` | A `.java` or `.kt` file in the module outside `src/main/{java,kotlin}`, `src/test/{java,kotlin}` and the module's ``, such as a `src/it/java` root added by `build-helper-maven-plugin` or a test root set by a parent POM outside the repository | Move the files under a standard root, or declare the test root as a plain relative `` in the module's own POM | | `Maven …: parent dependency inheritance requires full validation` | Inherit dependencies from an external parent POM | No | | `Maven …: unresolved dependency coordinates require full validation` | Use a `${property}` version DiffCI cannot resolve | No | diff --git a/src/repo/adapters/maven.ts b/src/repo/adapters/maven.ts index 823b94f..5ba8967 100644 --- a/src/repo/adapters/maven.ts +++ b/src/repo/adapters/maven.ts @@ -17,6 +17,8 @@ interface Pom { declaredTestRoots: string[]; /** Whether a `` sits inside a profile, which may or may not be active. */ profileTestRoot: boolean; + /** Whether the POM configures build-helper-maven-plugin, which can add any folder, resources included, as a source root. */ + buildHelper: boolean; } const DEFAULT_TEST_ROOT = "src/test/java"; @@ -86,6 +88,7 @@ function parsePom(path: string, source: string): Pom { customSourceRoots: tags(xml, "sourceDirectory").some(root => root !== "src/main/java"), testRoot: profileTestRoot ? undefined : resolveTestRoot(dir, declaredTestRoots), declaredTestRoots, profileTestRoot, + buildHelper: xml.includes("build-helper-maven-plugin"), }; } @@ -148,6 +151,15 @@ export function analyzeMaven(context: AdapterContext): AdapterContribution { const sources = owned.filter(file => /\.(?:java|kt)$/.test(file) && (/\/src\/(?:main|test)\/(?:java|kotlin)\//.test("/" + file) || (pom.testRoot !== undefined && file.startsWith(pom.testRoot + "/")))); const assets = owned.filter(file => /\/src\/(?:main|test)\/resources\//.test("/" + file)); + // Java or Kotlin outside every root DiffCI recognises may still be compiled and run by Maven: a test root + // added by build-helper-maven-plugin, or one set by a parent POM outside the repository. Fail closed (#327). + // Resources (archetype templates, compiler-test inputs) and the Maven wrapper's downloader are not sources, + // unless build-helper-maven-plugin is configured: it can add a folder under resources as a source root. + const known = new Set(sources); + if (owned.some(file => /\.(?:java|kt)$/.test(file) && !known.has(file) && + (pom.buildHelper || !/\/src\/(?:main|test)\/resources\//.test("/" + file)) && !/(?:^|\/)\.mvn\//.test(file))) { + result.blockers.push(`Maven ${pom.path}: Java sources outside the standard roots require full validation`); + } members.set(pom, sources); resources.set(pom, assets); const anchor = sources[0] ?? assets[0]; diff --git a/tests/repo/maven-test-source-directory.test.ts b/tests/repo/maven-test-source-directory.test.ts index 721d6e8..99cb0e7 100644 --- a/tests/repo/maven-test-source-directory.test.ts +++ b/tests/repo/maven-test-source-directory.test.ts @@ -144,3 +144,67 @@ for (const [name, parentBuild, childBuild, testPath] of [ } finally { rmSync(root, { recursive: true, force: true }); } }); } + +const outsideRoots = /Java sources outside the standard roots require full validation/; + +// #327: Java or Kotlin that Maven may compile and run from a root DiffCI doesn't know falls back, for a library +// change and for a change to that file itself. +for (const [label, extra, buildHelper] of [ + ["a src/it/java test root added by build-helper-maven-plugin", "application/src/it/java/example/AppIT.java", true], + ["a test root set by a parent POM outside the repository", "application/src/integration/java/example/AppIT.java", false], + ["a Kotlin file outside the roots", "application/src/it/kotlin/example/AppIT.kt", false], +] as const) { + test(`Maven falls back for ${label} (#327)`, async () => { + const build = buildHelper + ? "org.codehaus.mojobuild-helper-maven-pluginadd-test-sourcesrc/it/java" + : ""; + const files = reactor(build, "application/src/test/java/example/AppTest.java"); + files[extra] = "package example; class AppIT {}"; + if (label.includes("parent POM")) { + files["application/pom.xml"] = files["application/pom.xml"]!.replace("", "com.acmecompany-parent3"); + } + const root = fixture(files); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + assert.deepEqual(graph.adapterBlockers, ["Maven application/pom.xml: Java sources outside the standard roots require full validation"]); + for (const changed of ["library/src/main/java/example/Library.java", extra]) { + const impact = new ImpactAnalyzer().analyze(delta(changed), graph, graph.profile); + assert.equal(impact.fallbackRequired, true, changed); + assert.ok(impact.fallbackReasons.some((reason) => outsideRoots.test(reason)), impact.fallbackReasons.join("; ")); + } + } finally { rmSync(root, { recursive: true, force: true }); } + }); +} + +test("Maven does not fall back for Java under resources or in the Maven wrapper (#327)", async () => { + const files = reactor("", "application/src/test/java/example/AppTest.java"); + files["application/src/main/resources/archetype-resources/src/main/java/App.java"] = "public class App {}"; + files["application/src/test/resources/compile/Input.java"] = "class Input {}"; + files[".mvn/wrapper/MavenWrapperDownloader.java"] = "public class MavenWrapperDownloader {}"; + const root = fixture(files); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + assert.deepEqual(graph.adapterBlockers, []); + const impact = new ImpactAnalyzer().analyze(delta("library/src/main/java/example/Library.java"), graph, graph.profile); + assert.equal(impact.fallbackRequired, false, impact.fallbackReasons.join("; ")); + } finally { rmSync(root, { recursive: true, force: true }); } +}); + +test("A file under a resolved is inside the roots (#327)", async () => { + const root = fixture(reactor("tests", "application/tests/example/AppTest.java")); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + assert.ok(!graph.adapterBlockers?.some((reason) => outsideRoots.test(reason)), graph.adapterBlockers?.join("; ")); + } finally { rmSync(root, { recursive: true, force: true }); } +}); + +test("Maven falls back for Java under resources when build-helper-maven-plugin could add it as a root (#327)", async () => { + const build = "org.codehaus.mojobuild-helper-maven-pluginadd-test-sourcesrc/test/resources/java"; + const files = reactor(build, "application/src/test/java/example/AppTest.java"); + files["application/src/test/resources/java/example/ResourceIT.java"] = "package example; class ResourceIT {}"; + const root = fixture(files); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + assert.deepEqual(graph.adapterBlockers, ["Maven application/pom.xml: Java sources outside the standard roots require full validation"]); + } finally { rmSync(root, { recursive: true, force: true }); } +});