forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdtls-root.test.ts
More file actions
459 lines (404 loc) · 19.7 KB
/
Copy pathjdtls-root.test.ts
File metadata and controls
459 lines (404 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import { describe, test, expect, afterAll } from "bun:test"
import path from "path"
import fs from "fs/promises"
import os from "os"
import * as LSPServer from "@/lsp/server"
import type { InstanceContext } from "@/project/instance-context"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const tmpBase = path.join(os.tmpdir(), "opencode-jdtls-test")
function makeCtx(directory: string): InstanceContext {
return { directory, worktree: "/", project: {} as any }
}
async function mkdirp(p: string) {
await fs.mkdir(p, { recursive: true })
}
async function touch(p: string) {
await mkdirp(path.dirname(p))
await fs.writeFile(p, "", "utf-8")
}
// ---------------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------------
afterAll(async () => {
await fs.rm(tmpBase, { recursive: true, force: true }).catch(() => {})
})
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("JDTLS.root", () => {
// -------------------------------------------------------------------------
// Maven
// -------------------------------------------------------------------------
describe("Maven", () => {
test("single-module Maven project returns pom.xml directory", async () => {
const root = path.join(tmpBase, "single-maven")
await mkdirp(root)
await touch(path.join(root, "pom.xml"))
const srcDir = path.join(root, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
test("multi-module Maven project follows <module> chain to top-level pom.xml", async () => {
const root = path.join(tmpBase, "multi-maven")
await mkdirp(root)
// Parent pom with <module>module-a</module>
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>module-a</module></modules></project>")
// Child module with its own pom.xml
const childDir = path.join(root, "module-a")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
// Parent declares module-a as module → root is parent directory
expect(result).toBe(root)
})
test("Maven project inside a nested directory (ctx.directory is workspace root)", async () => {
// Workspace root = ctx.directory, Maven project in a subdirectory
const workspace = path.join(tmpBase, "maven-workspace")
await mkdirp(workspace)
const projectDir = path.join(workspace, "my-maven-app")
await touch(path.join(projectDir, "pom.xml"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
// findUp finds projectDir's pom.xml (only one), returns projectDir
expect(result).toBe(projectDir)
})
test("nested independent Maven project stops at its own pom.xml", async () => {
const workspace = path.join(tmpBase, "nested-independent")
await mkdirp(workspace)
// Parent pom WITHOUT <module>tools/sample</module>
await Bun.write(
path.join(workspace, "pom.xml"),
"<project><modules><module>module-a</module></modules></project>",
)
// Independent project nested inside
const projectDir = path.join(workspace, "tools", "sample")
await mkdirp(projectDir)
await touch(path.join(projectDir, "pom.xml"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
// workspace/pom.xml does NOT declare tools/sample as module → stop at tools/sample
expect(result).toBe(projectDir)
})
test("three-level Maven module chain resolves to top-level", async () => {
const root = path.join(tmpBase, "three-level")
await mkdirp(root)
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>apps</module></modules></project>")
const appsDir = path.join(root, "apps")
await mkdirp(appsDir)
await Bun.write(path.join(appsDir, "pom.xml"), "<project><modules><module>my-app</module></modules></project>")
const appDir = path.join(appsDir, "my-app")
await mkdirp(appDir)
await touch(path.join(appDir, "pom.xml"))
const srcDir = path.join(appDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
test("three-level Maven chain stops when <module> link is broken", async () => {
const root = path.join(tmpBase, "broken-chain")
await mkdirp(root)
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>apps</module></modules></project>")
const appsDir = path.join(root, "apps")
await mkdirp(appsDir)
await touch(path.join(appsDir, "pom.xml")) // Empty pom, no <module> declaration
const appDir = path.join(appsDir, "my-app")
await mkdirp(appDir)
await touch(path.join(appDir, "pom.xml"))
const srcDir = path.join(appDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
// apps/pom.xml has no <module>my-app</module> → stop at my-app
expect(result).toBe(appDir)
})
test("<module> with ./ prefix is normalized correctly", async () => {
const root = path.join(tmpBase, "dot-slash-module")
await mkdirp(root)
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>./module-a</module></modules></project>")
const childDir = path.join(root, "module-a")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
test("<module> with trailing slash is normalized correctly", async () => {
const root = path.join(tmpBase, "trailing-slash-module")
await mkdirp(root)
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>module-a/</module></modules></project>")
const childDir = path.join(root, "module-a")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
})
// -------------------------------------------------------------------------
// Gradle
// -------------------------------------------------------------------------
describe("Gradle", () => {
test("Gradle project with settings.gradle in a subdirectory of ctx.directory", async () => {
// Workspace root = ctx.directory, Gradle project in a subdirectory
const workspace = path.join(tmpBase, "gradle-sub")
await mkdirp(workspace)
const projectDir = path.join(workspace, "gradle-app")
await touch(path.join(projectDir, "settings.gradle"))
await touch(path.join(projectDir, "build.gradle"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
test("Gradle project with only build.gradle in a subdirectory", async () => {
const workspace = path.join(tmpBase, "gradle-build-sub")
await mkdirp(workspace)
const projectDir = path.join(workspace, "gradle-app")
await touch(path.join(projectDir, "build.gradle"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
test("Gradle monorepo with settings.gradle takes precedence over nested pom.xml", async () => {
const workspace = path.join(tmpBase, "gradle-monorepo")
await mkdirp(workspace)
const gradleRoot = path.join(workspace, "gradle-project")
await touch(path.join(gradleRoot, "settings.gradle"))
await touch(path.join(gradleRoot, "gradlew"))
// Submodule has pom.xml too
const subDir = path.join(gradleRoot, "module-a")
await mkdirp(subDir)
await touch(path.join(subDir, "pom.xml"))
const srcDir = path.join(subDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
// Gradle markers found at gradleRoot level
expect(result).toBe(gradleRoot)
})
test("settings.gradle.kts (Kotlin DSL) is recognized", async () => {
const workspace = path.join(tmpBase, "gradle-kts-settings")
await mkdirp(workspace)
const projectDir = path.join(workspace, "gradle-app")
await touch(path.join(projectDir, "settings.gradle.kts"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
test("build.gradle.kts (Kotlin DSL) is recognized", async () => {
const workspace = path.join(tmpBase, "gradle-kts-build")
await mkdirp(workspace)
const projectDir = path.join(workspace, "gradle-app")
await touch(path.join(projectDir, "build.gradle.kts"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
test("gradlew (without settings.gradle) in a subdirectory is recognized", async () => {
const workspace = path.join(tmpBase, "gradlew-sub")
await mkdirp(workspace)
const projectDir = path.join(workspace, "gradle-app")
await touch(path.join(projectDir, "gradlew"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
test("pom.xml is excluded when gradlew is present at same level", async () => {
const workspace = path.join(tmpBase, "gradle-excludes-maven")
await mkdirp(workspace)
const projectDir = path.join(workspace, "mixed-project")
// Both pom.xml and gradlew exist
await touch(path.join(projectDir, "pom.xml"))
await touch(path.join(projectDir, "gradlew"))
const srcDir = path.join(projectDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
// Gradle wrapper takes precedence
expect(result).toBe(projectDir)
})
})
// -------------------------------------------------------------------------
// Eclipse
// -------------------------------------------------------------------------
describe("Eclipse", () => {
test("Eclipse project with .project in a subdirectory", async () => {
const workspace = path.join(tmpBase, "eclipse-sub")
await mkdirp(workspace)
const projectDir = path.join(workspace, "eclipse-app")
await touch(path.join(projectDir, ".project"))
await touch(path.join(projectDir, ".classpath"))
const srcDir = path.join(projectDir, "src", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(workspace))
expect(result).toBe(projectDir)
})
})
// -------------------------------------------------------------------------
// No markers
// -------------------------------------------------------------------------
describe("No build markers", () => {
test("Java file with no build markers returns undefined", async () => {
const root = path.join(tmpBase, "no-build")
await mkdirp(root)
const srcDir = path.join(root, "src")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBeUndefined()
})
})
// -------------------------------------------------------------------------
// Additional validation scenarios
// -------------------------------------------------------------------------
describe("Additional Maven module-chain validation", () => {
// Scenario 1: <module> with multi-segment path (e.g. <module>tools/sample</module>)
test("<module> multi-segment path matches nested directory", async () => {
const root = path.join(tmpBase, "multi-seg-module")
await mkdirp(root)
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>tools/sample</module></modules></project>")
const childDir = path.join(root, "tools", "sample")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
// Scenario 2: <module> declaration does not match actual directory name
test("<module> declaration mismatch does not falsely match", async () => {
const root = path.join(tmpBase, "module-mismatch")
await mkdirp(root)
// Parent declares module-a, but actual directory is module-b
await Bun.write(path.join(root, "pom.xml"), "<project><modules><module>module-a</module></modules></project>")
const childDir = path.join(root, "module-b")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
// module-b is not declared as a module → stop at module-b
expect(result).toBe(childDir)
})
// Scenario 3: Multiple <module> declarations
test("multiple <module> declarations allow second module to traverse up", async () => {
const root = path.join(tmpBase, "multi-modules")
await mkdirp(root)
await Bun.write(
path.join(root, "pom.xml"),
"<project><modules><module>module-a</module><module>module-b</module></modules></project>",
)
const childA = path.join(root, "module-a")
await mkdirp(childA)
await touch(path.join(childA, "pom.xml"))
const childB = path.join(root, "module-b")
await mkdirp(childB)
await touch(path.join(childB, "pom.xml"))
const srcDir = path.join(childB, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
// Scenario 4: XML comments should not be matched as module declarations
test("XML-commented <module> is not matched", async () => {
const root = path.join(tmpBase, "commented-module")
await mkdirp(root)
await Bun.write(
path.join(root, "pom.xml"),
"<project><modules><!-- <module>module-a</module> --></modules></project>",
)
const childDir = path.join(root, "module-a")
await mkdirp(childDir)
await touch(path.join(childDir, "pom.xml"))
const srcDir = path.join(childDir, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
// Commented-out module should not be matched → stop at module-a
expect(result).toBe(childDir)
})
// Scenario 5: pom.xml at ctx.directory itself
test("pom.xml at ctx.directory itself is found correctly", async () => {
const root = path.join(tmpBase, "pom-at-ctx")
await mkdirp(root)
await touch(path.join(root, "pom.xml"))
const srcDir = path.join(root, "src", "main", "java", "com", "example")
await mkdirp(srcDir)
await touch(path.join(srcDir, "App.java"))
const file = path.join(srcDir, "App.java")
const result = await LSPServer.JDTLS.root(file, makeCtx(root))
expect(result).toBe(root)
})
// Scenario 6: Mixed Gradle + Maven sibling projects don't interfere
test("Maven and Gradle sibling projects don't interfere", async () => {
const workspace = path.join(tmpBase, "mixed-siblings")
await mkdirp(workspace)
// Gradle project
const gradleDir = path.join(workspace, "gradle-project")
await touch(path.join(gradleDir, "settings.gradle"))
const gradleSrc = path.join(gradleDir, "src", "main", "java", "com", "example")
await mkdirp(gradleSrc)
await touch(path.join(gradleSrc, "GradleApp.java"))
// Maven project
const mavenDir = path.join(workspace, "maven-project")
await touch(path.join(mavenDir, "pom.xml"))
const mavenSrc = path.join(mavenDir, "src", "main", "java", "com", "example")
await mkdirp(mavenSrc)
await touch(path.join(mavenSrc, "MavenApp.java"))
const gradleResult = await LSPServer.JDTLS.root(path.join(gradleSrc, "GradleApp.java"), makeCtx(workspace))
expect(gradleResult).toBe(gradleDir)
const mavenResult = await LSPServer.JDTLS.root(path.join(mavenSrc, "MavenApp.java"), makeCtx(workspace))
expect(mavenResult).toBe(mavenDir)
})
})
})