|
| 1 | +/* |
| 2 | + * Unit tests for Gradle multi-module discovery. |
| 3 | + * Plain mocha + chai, no VS Code instance required. |
| 4 | + * |
| 5 | + * Gradle multi-module relationship is one-directional: |
| 6 | + * - Root declares subprojects via settings.gradle include() |
| 7 | + * - Children do NOT declare a parent reference |
| 8 | + * - No two-pass strategy needed — settings.gradle is single source of truth |
| 9 | + * |
| 10 | + * Variables: |
| 11 | + * RL = Root has Liberty plugin in build.gradle |
| 12 | + * CL = Child has Liberty plugin in build.gradle |
| 13 | + * SI = settings.gradle includes the child |
| 14 | + * |
| 15 | + * Group | RL | CL | SI | Expected |
| 16 | + * -------|----|----|----|----------------------------------------- |
| 17 | + * 1 | N | Y | Y | Root display-only aggregator, child actionable |
| 18 | + * 2 | Y | Y | Y | Root aggregator, child actionable |
| 19 | + * 3 | N | N | Y | Both excluded (no Liberty anywhere) |
| 20 | + * 4 | N | Y | N | Child standalone, root excluded |
| 21 | + * |
| 22 | + * Reference test project: ~/test/multi-module-test-projects/bob-nested-gradle-ears |
| 23 | + * - Root build.gradle has no Liberty plugin |
| 24 | + * - app-one-ear/build.gradle and app-two-ear/build.gradle declare Liberty directly |
| 25 | + * - settings.gradle includes all subprojects via colon-separated paths |
| 26 | + */ |
| 27 | +import { strict as assert } from "assert"; |
| 28 | +import { detectLibertyPluginFromText, findChildGradleProjects, hasGradleSubprojects } from "../../util/gradleUtil"; |
| 29 | +import { LIBERTY_PROJECT_GRADLE } from "../../definitions/constants"; |
| 30 | + |
| 31 | +// ── Fixture builders ────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +function makeRootBuildGradle(hasLiberty: boolean): string { |
| 34 | + return hasLiberty |
| 35 | + ? `plugins { |
| 36 | + id("io.openliberty.tools.gradle.Liberty") version "3.0" |
| 37 | +}` |
| 38 | + : `plugins { |
| 39 | + id 'base' |
| 40 | +}`; |
| 41 | +} |
| 42 | + |
| 43 | +function makeChildBuildGradle(hasLiberty: boolean): string { |
| 44 | + return hasLiberty |
| 45 | + ? `plugins { |
| 46 | + id 'java' |
| 47 | + id("io.openliberty.tools.gradle.Liberty") version "3.0" |
| 48 | +}` |
| 49 | + : `plugins { |
| 50 | + id 'java' |
| 51 | +}`; |
| 52 | +} |
| 53 | + |
| 54 | +function makeSettingsGradle(includeChild: boolean): any { |
| 55 | + // Simulate parsed settings.gradle as g2js would return |
| 56 | + return includeChild |
| 57 | + ? { "rootProject.name": "my-root", include: ["child-module"] } |
| 58 | + : { "rootProject.name": "my-root" }; |
| 59 | +} |
| 60 | + |
| 61 | +// ── Group 1: Root no Liberty, child has Liberty, settings includes child ─────── |
| 62 | +// Root display-only aggregator, child actionable |
| 63 | + |
| 64 | +describe("gradle group 1 — root no Liberty, child has Liberty, included in settings", () => { |
| 65 | + const rootContent = makeRootBuildGradle(false); |
| 66 | + const childContent = makeChildBuildGradle(true); |
| 67 | + const parsedSettings = makeSettingsGradle(true); |
| 68 | + |
| 69 | + it("root is not detected as Liberty project via text", () => { |
| 70 | + const result = detectLibertyPluginFromText(rootContent); |
| 71 | + assert.equal(result, null); |
| 72 | + }); |
| 73 | + |
| 74 | + it("root is detected as aggregator via settings.gradle include", () => { |
| 75 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 76 | + assert.ok(result.getChildren().length > 0); |
| 77 | + }); |
| 78 | + |
| 79 | + it("child is detected as Liberty project via text", () => { |
| 80 | + const result = detectLibertyPluginFromText(childContent); |
| 81 | + assert.notEqual(result, null); |
| 82 | + assert.equal(result!.getProjectType(), LIBERTY_PROJECT_GRADLE); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +// ── Group 2: Both root and child have Liberty, settings includes child ───────── |
| 87 | +// Root aggregator, child actionable |
| 88 | + |
| 89 | +describe("gradle group 2 — both have Liberty, included in settings", () => { |
| 90 | + const rootContent = makeRootBuildGradle(true); |
| 91 | + const childContent = makeChildBuildGradle(true); |
| 92 | + const parsedSettings = makeSettingsGradle(true); |
| 93 | + |
| 94 | + it("root is detected as Liberty project via text", () => { |
| 95 | + const result = detectLibertyPluginFromText(rootContent); |
| 96 | + assert.notEqual(result, null); |
| 97 | + assert.equal(result!.getProjectType(), LIBERTY_PROJECT_GRADLE); |
| 98 | + }); |
| 99 | + |
| 100 | + it("root is also detected as aggregator via settings.gradle include", () => { |
| 101 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 102 | + assert.ok(result.getChildren().length > 0); |
| 103 | + }); |
| 104 | + |
| 105 | + it("child is detected as Liberty project via text", () => { |
| 106 | + const result = detectLibertyPluginFromText(childContent); |
| 107 | + assert.notEqual(result, null); |
| 108 | + assert.equal(result!.getProjectType(), LIBERTY_PROJECT_GRADLE); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +// ── Group 3: Neither has Liberty, settings includes child ───────────────────── |
| 113 | +// Both excluded — no Liberty anywhere |
| 114 | + |
| 115 | +describe("gradle group 3 — no Liberty anywhere, included in settings", () => { |
| 116 | + const rootContent = makeRootBuildGradle(false); |
| 117 | + const childContent = makeChildBuildGradle(false); |
| 118 | + const parsedSettings = makeSettingsGradle(true); |
| 119 | + |
| 120 | + it("root not detected as Liberty project", () => { |
| 121 | + assert.equal(detectLibertyPluginFromText(rootContent), null); |
| 122 | + }); |
| 123 | + |
| 124 | + it("child not detected as Liberty project", () => { |
| 125 | + assert.equal(detectLibertyPluginFromText(childContent), null); |
| 126 | + }); |
| 127 | + |
| 128 | + it("root still detected as aggregator structurally — excluded later by hasLibertyDescendants", () => { |
| 129 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 130 | + assert.ok(result.getChildren().length > 0); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +// ── Group 4: Child has Liberty, not included in settings ────────────────────── |
| 135 | +// Child standalone, root excluded |
| 136 | + |
| 137 | +describe("gradle group 4 — child has Liberty, not in settings", () => { |
| 138 | + const rootContent = makeRootBuildGradle(false); |
| 139 | + const childContent = makeChildBuildGradle(true); |
| 140 | + const parsedSettings = makeSettingsGradle(false); |
| 141 | + |
| 142 | + it("root not detected as Liberty project", () => { |
| 143 | + assert.equal(detectLibertyPluginFromText(rootContent), null); |
| 144 | + }); |
| 145 | + |
| 146 | + it("root has no children in settings — not an aggregator", () => { |
| 147 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 148 | + assert.equal(result.getChildren().length, 0); |
| 149 | + }); |
| 150 | + |
| 151 | + it("child is detected as standalone Liberty project", () => { |
| 152 | + const result = detectLibertyPluginFromText(childContent); |
| 153 | + assert.notEqual(result, null); |
| 154 | + assert.equal(result!.getProjectType(), LIBERTY_PROJECT_GRADLE); |
| 155 | + }); |
| 156 | +}); |
| 157 | + |
| 158 | +// ── Colon-path resolution ───────────────────────────────────────────────────── |
| 159 | +// settings.gradle include 'application-one:app-one-ear' style paths |
| 160 | + |
| 161 | +describe("gradle colon-path subproject inclusion", () => { |
| 162 | + const parsedSettings = { |
| 163 | + "rootProject.name": "bob-nested-gradle-ears", |
| 164 | + include: [ |
| 165 | + "application-one:app-one-ear", |
| 166 | + "application-one:app-one-ejb", |
| 167 | + "application-two:app-two-ear", |
| 168 | + ], |
| 169 | + }; |
| 170 | + |
| 171 | + it("findChildGradleProjects returns all colon-path children", () => { |
| 172 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 173 | + const children = result.getChildren(); |
| 174 | + assert.ok(children.includes("application-one:app-one-ear")); |
| 175 | + assert.ok(children.includes("application-one:app-one-ejb")); |
| 176 | + assert.ok(children.includes("application-two:app-two-ear")); |
| 177 | + }); |
| 178 | + |
| 179 | + it("colon paths cover all three ear subprojects from bob-nested-gradle-ears", () => { |
| 180 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 181 | + assert.equal(result.getChildren().length, 3); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +// ── hasGradleSubprojects — phase 2 guard fix ────────────────────────────────── |
| 186 | +// The bug: phase 2 skips no-Liberty roots because parsedBuild/regexBuildFile |
| 187 | +// are null. hasGradleSubprojects() lets discovery check settings.gradle |
| 188 | +// independently of whether the root has Liberty, so findChildGradleProjects |
| 189 | +// is called for no-Liberty aggregators like bob-nested-gradle-ears. |
| 190 | + |
| 191 | +describe("hasGradleSubprojects — detects aggregator from settings alone", () => { |
| 192 | + it("returns true when parsedSettings has include entries", () => { |
| 193 | + const parsedSettings = { "rootProject.name": "my-root", include: ["app-one-ear", "app-two-ear"] }; |
| 194 | + assert.equal(hasGradleSubprojects(parsedSettings), true); |
| 195 | + }); |
| 196 | + |
| 197 | + it("returns false when parsedSettings has no include entries", () => { |
| 198 | + const parsedSettings = { "rootProject.name": "my-root" }; |
| 199 | + assert.equal(hasGradleSubprojects(parsedSettings), false); |
| 200 | + }); |
| 201 | + |
| 202 | + it("returns false when parsedSettings is null", () => { |
| 203 | + assert.equal(hasGradleSubprojects(null), false); |
| 204 | + }); |
| 205 | + |
| 206 | + it("returns false when parsedSettings is undefined", () => { |
| 207 | + assert.equal(hasGradleSubprojects(undefined), false); |
| 208 | + }); |
| 209 | + |
| 210 | + it("returns true for colon-path includes (bob-nested-gradle-ears pattern)", () => { |
| 211 | + const parsedSettings = { |
| 212 | + "rootProject.name": "bob-nested-gradle-ears", |
| 213 | + include: [ |
| 214 | + "application-one:app-one-ear", |
| 215 | + "application-one:app-one-ejb", |
| 216 | + "application-two:app-two-ear", |
| 217 | + ], |
| 218 | + }; |
| 219 | + assert.equal(hasGradleSubprojects(parsedSettings), true); |
| 220 | + }); |
| 221 | + |
| 222 | + it("no-Liberty root + subprojects → findChildGradleProjects returns valid aggregator", () => { |
| 223 | + const parsedSettings = { |
| 224 | + "rootProject.name": "bob-nested-gradle-ears", |
| 225 | + include: ["application-one:app-one-ear", "application-two:app-two-ear"], |
| 226 | + }; |
| 227 | + // Simulate what phase 2 should do: check hasGradleSubprojects first, |
| 228 | + // then call findChildGradleProjects with empty parsedBuild for no-Liberty root |
| 229 | + const isAggregator = hasGradleSubprojects(parsedSettings); |
| 230 | + assert.equal(isAggregator, true); |
| 231 | + |
| 232 | + const result = findChildGradleProjects({}, parsedSettings, "/workspace/build.gradle"); |
| 233 | + assert.equal(result.isValidBuildFile(), true); |
| 234 | + assert.ok(result.getChildren().includes("application-one:app-one-ear")); |
| 235 | + assert.ok(result.getChildren().includes("application-two:app-two-ear")); |
| 236 | + }); |
| 237 | +}); |
0 commit comments