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