-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathimportResolver.test.ts
More file actions
310 lines (267 loc) · 8.32 KB
/
importResolver.test.ts
File metadata and controls
310 lines (267 loc) · 8.32 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
import * as assert from "node:assert"
import { analyzeFile } from "../../core/analyzer"
import { resolveImport, resolveNamedImport } from "../../core/importResolver"
import { Parser } from "../../core/parser"
import { fixtures, nodeFileSystem, wasmBinaries } from "../testUtils"
const standardRoot = fixtures.standard.root
suite("importResolver", () => {
let parser: Parser
suiteSetup(async () => {
parser = new Parser()
await parser.init(wasmBinaries)
})
suiteTeardown(() => {
parser.dispose()
})
suite("resolveImport", () => {
test("resolves relative import to .py file", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
const result = await resolveImport(
{ modulePath: "routes.users", isRelative: true, relativeDots: 1 },
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("users.py"))
})
test("resolves relative import to __init__.py", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
const result = await resolveImport(
{ modulePath: "routes", isRelative: true, relativeDots: 1 },
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("__init__.py"))
})
test("resolves double-dot relative import", async () => {
// from .. import something (2 dots, no module name)
// From app/routes/users.py, this goes to parent package (app)
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"routes",
"users.py",
)
const projectRoot = standardRoot
const result = await resolveImport(
{ modulePath: "", isRelative: true, relativeDots: 2 },
currentFile,
projectRoot,
nodeFileSystem,
)
// 2 dots from routes/users.py goes to app/
assert.ok(result)
assert.ok(result.endsWith("app/__init__.py"))
})
test("resolves absolute import", async () => {
const currentFile = nodeFileSystem.joinPath(standardRoot, "main.py")
const projectRoot = standardRoot
const result = await resolveImport(
{
modulePath: "app.routes.users",
isRelative: false,
relativeDots: 0,
},
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("users.py"))
})
test("falls back to src/ for absolute imports in src layout", async () => {
// Project root is the pyproject.toml dir, but source is under src/
const currentFile = fixtures.srcLayout.mainPy
const projectRoot = fixtures.srcLayout.workspaceRoot
const result = await resolveImport(
{ modulePath: "app.api", isRelative: false, relativeDots: 0 },
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("api/__init__.py"))
})
test("returns null for non-existent module", async () => {
const currentFile = nodeFileSystem.joinPath(standardRoot, "main.py")
const projectRoot = standardRoot
const result = await resolveImport(
{
modulePath: "nonexistent.module",
isRelative: false,
relativeDots: 0,
},
currentFile,
projectRoot,
nodeFileSystem,
)
assert.strictEqual(result, null)
})
})
suite("resolveNamedImport", () => {
test("resolves named import to .py file", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
const result = await resolveNamedImport(
{
modulePath: "routes",
names: ["users"],
isRelative: true,
relativeDots: 1,
},
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("users.py"))
})
test("resolves re-exported name from __init__.py", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
// The __init__.py has: from .users import router as users_router
const result = await resolveNamedImport(
{
modulePath: "routes",
names: ["users_router"],
isRelative: true,
relativeDots: 1,
},
currentFile,
projectRoot,
nodeFileSystem,
(uri) => analyzeFile(uri, parser, nodeFileSystem),
)
assert.ok(result)
assert.ok(result.endsWith("users.py"))
})
test("falls back to base module for non-existent named import", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
const result = await resolveNamedImport(
{
modulePath: "routes",
names: ["nonexistent"],
isRelative: true,
relativeDots: 1,
},
currentFile,
projectRoot,
nodeFileSystem,
)
// Falls back to the base module when named import not found
assert.ok(result)
assert.ok(
result.endsWith("routes/__init__.py") || result.endsWith("routes.py"),
)
})
test("resolves relative named import from namespace package (no __init__.py)", async () => {
const currentFile = nodeFileSystem.joinPath(
standardRoot,
"app",
"main.py",
)
const projectRoot = standardRoot
// namespace_routes has no __init__.py, but api_routes.py exists
const result = await resolveNamedImport(
{
modulePath: "namespace_routes",
names: ["api_routes"],
isRelative: true,
relativeDots: 1,
},
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("api_routes.py"))
})
test("resolves absolute named import from namespace package (no __init__.py)", async () => {
const currentFile = nodeFileSystem.joinPath(standardRoot, "main.py")
const projectRoot = standardRoot
// app.namespace_routes has no __init__.py, but api_routes.py exists
const result = await resolveNamedImport(
{
modulePath: "app.namespace_routes",
names: ["api_routes"],
isRelative: false,
relativeDots: 0,
},
currentFile,
projectRoot,
nodeFileSystem,
)
assert.ok(result)
assert.ok(result.endsWith("api_routes.py"))
})
test("resolves named import via src/ fallback for src layout", async () => {
const currentFile = fixtures.srcLayout.mainPy
const projectRoot = fixtures.srcLayout.workspaceRoot
// "from app.api import api_router" — the actual import from the issue
const result = await resolveNamedImport(
{
modulePath: "app.api",
names: ["api_router"],
isRelative: false,
relativeDots: 0,
},
currentFile,
projectRoot,
nodeFileSystem,
(uri) => analyzeFile(uri, parser, nodeFileSystem),
)
assert.ok(result)
assert.ok(result.endsWith("api/__init__.py"))
})
test("resolves variable import from .py file (not submodule)", async () => {
// This tests "from .neon import router" where router is a variable in neon.py,
// NOT a submodule. Should return neon.py, not look for router.py
const reexportRoot = fixtures.reexport.root
const currentFile = nodeFileSystem.joinPath(
reexportRoot,
"app",
"integrations",
"router.py",
)
const result = await resolveNamedImport(
{
modulePath: "neon",
names: ["router"],
isRelative: true,
relativeDots: 1,
},
currentFile,
reexportRoot,
nodeFileSystem,
)
assert.ok(result, "Should resolve import")
assert.ok(result.endsWith("neon.py"), `Expected neon.py, got ${result}`)
})
})
})