2
2
3
3
from codegen .sdk .codebase .config import TestFlags
4
4
from codegen .sdk .codebase .factory .get_session import get_codebase_session
5
+ from codegen .sdk .utils import use_cwd
5
6
6
7
if TYPE_CHECKING :
7
8
from codegen .sdk .core .file import SourceFile
@@ -272,27 +273,31 @@ def func():
272
273
""" ,
273
274
},
274
275
) as codebase :
275
- src_file : SourceFile = codebase .get_file ("a/b/c/src.py" )
276
- consumer_file : SourceFile = codebase .get_file ("consumer.py" )
276
+ # Wrap CWD since we are using py_resolve_syspath
277
+ with use_cwd (tmpdir ):
278
+ src_file : SourceFile = codebase .get_file ("a/b/c/src.py" )
279
+ consumer_file : SourceFile = codebase .get_file ("consumer.py" )
277
280
278
- # Enable resolution via sys.path
279
- codebase .ctx .config .py_resolve_syspath = True
281
+ # Enable resolution via sys.path
282
+ codebase .ctx .config .py_resolve_syspath = True
283
+ # Allow resolving files and modules outside of the repo path
284
+ codebase .ctx .config .allow_external = True
280
285
281
- # =====[ Imports cannot be found without sys.path being set ]=====
282
- assert len (consumer_file .imports ) == 1
283
- src_import : Import = consumer_file .imports [0 ]
284
- src_import_resolution : ImportResolution = src_import .resolve_import ()
285
- assert src_import_resolution is None
286
+ # =====[ Imports cannot be found without sys.path being set ]=====
287
+ assert len (consumer_file .imports ) == 1
288
+ src_import : Import = consumer_file .imports [0 ]
289
+ src_import_resolution : ImportResolution = src_import .resolve_import ()
290
+ assert src_import_resolution is None
286
291
287
- # Modify sys.path for this test only
288
- monkeypatch .syspath_prepend ("a" )
292
+ # Modify sys.path for this test only
293
+ monkeypatch .syspath_prepend ("a" )
289
294
290
- # =====[ Imports can be found with sys.path set and active ]=====
291
- codebase .ctx .config .py_resolve_syspath = True
292
- src_import_resolution = src_import .resolve_import ()
293
- assert src_import_resolution
294
- assert src_import_resolution .from_file is src_file
295
- assert src_import_resolution .imports_file is True
295
+ # =====[ Imports can be found with sys.path set and active ]=====
296
+ codebase .ctx .config .py_resolve_syspath = True
297
+ src_import_resolution = src_import .resolve_import ()
298
+ assert src_import_resolution
299
+ assert src_import_resolution .from_file is src_file
300
+ assert src_import_resolution .imports_file is True
296
301
297
302
298
303
def test_import_resolution_file_custom_resolve_path (tmpdir : str ) -> None :
@@ -366,28 +371,32 @@ def func():
366
371
""" ,
367
372
},
368
373
) as codebase :
369
- src_file : SourceFile = codebase .get_file ("a/b/c/src.py" )
370
- consumer_file : SourceFile = codebase .get_file ("consumer.py" )
371
-
372
- # Ensure we don't have overrites and enable syspath resolution
373
- codebase .ctx .config .import_resolution_paths = []
374
- codebase .ctx .config .py_resolve_syspath = True
375
-
376
- # =====[ Import with sys.path set can be found ]=====
377
- assert len (consumer_file .imports ) == 1
378
- # Modify sys.path for this test only
379
- monkeypatch .syspath_prepend ("a" )
380
- src_import : Import = consumer_file .imports [0 ]
381
- src_import_resolution = src_import .resolve_import ()
382
- assert src_import_resolution
383
- assert src_import_resolution .from_file .file_path == "a/c/src.py"
384
-
385
- # =====[ Imports can be found with custom resolve over sys.path ]=====
386
- codebase .ctx .config .import_resolution_paths = ["a/b" ]
387
- src_import_resolution = src_import .resolve_import ()
388
- assert src_import_resolution
389
- assert src_import_resolution .from_file is src_file
390
- assert src_import_resolution .imports_file is True
374
+ # Wrap CWD since we are using py_resolve_syspath
375
+ with use_cwd (tmpdir ):
376
+ src_file : SourceFile = codebase .get_file ("a/b/c/src.py" )
377
+ consumer_file : SourceFile = codebase .get_file ("consumer.py" )
378
+
379
+ # Ensure we don't have overrites and enable syspath resolution
380
+ codebase .ctx .config .import_resolution_paths = []
381
+ codebase .ctx .config .py_resolve_syspath = True
382
+ # Allow resolving files and modules outside of the repo path
383
+ codebase .ctx .config .allow_external = True
384
+
385
+ # =====[ Import with sys.path set can be found ]=====
386
+ assert len (consumer_file .imports ) == 1
387
+ # Modify sys.path for this test only
388
+ monkeypatch .syspath_prepend ("a" )
389
+ src_import : Import = consumer_file .imports [0 ]
390
+ src_import_resolution = src_import .resolve_import ()
391
+ assert src_import_resolution
392
+ assert src_import_resolution .from_file .file_path == "a/c/src.py"
393
+
394
+ # =====[ Imports can be found with custom resolve over sys.path ]=====
395
+ codebase .ctx .config .import_resolution_paths = ["a/b" ]
396
+ src_import_resolution = src_import .resolve_import ()
397
+ assert src_import_resolution
398
+ assert src_import_resolution .from_file is src_file
399
+ assert src_import_resolution .imports_file is True
391
400
392
401
393
402
def test_import_resolution_default_conflicts_overrite (tmpdir : str , monkeypatch ) -> None :
@@ -412,34 +421,38 @@ def func():
412
421
""" ,
413
422
},
414
423
) as codebase :
415
- src_file : SourceFile = codebase .get_file ("a/src.py" )
416
- src_file_overrite : SourceFile = codebase .get_file ("b/a/src.py" )
417
- consumer_file : SourceFile = codebase .get_file ("consumer.py" )
418
-
419
- # Ensure we don't have overrites and enable syspath resolution
420
- codebase .ctx .config .import_resolution_paths = []
421
- codebase .ctx .config .py_resolve_syspath = True
422
-
423
- # =====[ Default import works ]=====
424
- assert len (consumer_file .imports ) == 1
425
- src_import : Import = consumer_file .imports [0 ]
426
- src_import_resolution = src_import .resolve_import ()
427
- assert src_import_resolution
428
- assert src_import_resolution .from_file is src_file
429
-
430
- # =====[ Sys.path overrite has precedence ]=====
431
- monkeypatch .syspath_prepend ("b" )
432
- src_import_resolution = src_import .resolve_import ()
433
- assert src_import_resolution
434
- assert src_import_resolution .from_file is not src_file
435
- assert src_import_resolution .from_file is src_file_overrite
436
-
437
- # =====[ Custom overrite has precedence ]=====
438
- codebase .ctx .config .import_resolution_paths = ["b" ]
439
- src_import_resolution = src_import .resolve_import ()
440
- assert src_import_resolution
441
- assert src_import_resolution .from_file is not src_file
442
- assert src_import_resolution .from_file is src_file_overrite
424
+ # Wrap CWD since we are using py_resolve_syspath
425
+ with use_cwd (tmpdir ):
426
+ src_file : SourceFile = codebase .get_file ("a/src.py" )
427
+ src_file_overrite : SourceFile = codebase .get_file ("b/a/src.py" )
428
+ consumer_file : SourceFile = codebase .get_file ("consumer.py" )
429
+
430
+ # Ensure we don't have overrites and enable syspath resolution
431
+ codebase .ctx .config .import_resolution_paths = []
432
+ codebase .ctx .config .py_resolve_syspath = True
433
+ # Allow resolving files and modules outside of the repo path
434
+ codebase .ctx .config .allow_external = True
435
+
436
+ # =====[ Default import works ]=====
437
+ assert len (consumer_file .imports ) == 1
438
+ src_import : Import = consumer_file .imports [0 ]
439
+ src_import_resolution = src_import .resolve_import ()
440
+ assert src_import_resolution
441
+ assert src_import_resolution .from_file is src_file
442
+
443
+ # =====[ Sys.path overrite has precedence ]=====
444
+ monkeypatch .syspath_prepend ("b" )
445
+ src_import_resolution = src_import .resolve_import ()
446
+ assert src_import_resolution
447
+ assert src_import_resolution .from_file is not src_file
448
+ assert src_import_resolution .from_file is src_file_overrite
449
+
450
+ # =====[ Custom overrite has precedence ]=====
451
+ codebase .ctx .config .import_resolution_paths = ["b" ]
452
+ src_import_resolution = src_import .resolve_import ()
453
+ assert src_import_resolution
454
+ assert src_import_resolution .from_file is not src_file
455
+ assert src_import_resolution .from_file is src_file_overrite
443
456
444
457
445
458
def test_import_resolution_init_wildcard (tmpdir : str ) -> None :
0 commit comments