Skip to content

Commit 779a7ad

Browse files
committed
Fix tests (cwd fix)
1 parent 246624f commit 779a7ad

File tree

2 files changed

+107
-67
lines changed

2 files changed

+107
-67
lines changed

src/codegen/sdk/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,30 @@ def is_minified_js(content):
339339
except Exception as e:
340340
print(f"Error analyzing content: {e}")
341341
return False
342+
343+
344+
@contextmanager
345+
def use_cwd(path):
346+
"""Context manager that temporarily changes the current working directory.
347+
348+
Args:
349+
path (str): The directory path to change to.
350+
351+
Yields:
352+
str: The new current working directory.
353+
354+
Example:
355+
```python
356+
with use_cwd('/path/to/directory'):
357+
# Code here runs with the working directory set to '/path/to/directory'
358+
...
359+
# Working directory is restored to the original
360+
```
361+
"""
362+
old_cwd = os.getcwd()
363+
try:
364+
os.chdir(path)
365+
yield path
366+
finally:
367+
os.chdir(old_cwd)
368+

tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py

Lines changed: 80 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from codegen.sdk.codebase.config import TestFlags
44
from codegen.sdk.codebase.factory.get_session import get_codebase_session
5+
from codegen.sdk.utils import use_cwd
56

67
if TYPE_CHECKING:
78
from codegen.sdk.core.file import SourceFile
@@ -272,27 +273,31 @@ def func():
272273
""",
273274
},
274275
) 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")
277280

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
280285

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
286291

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")
289294

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
296301

297302

298303
def test_import_resolution_file_custom_resolve_path(tmpdir: str) -> None:
@@ -366,28 +371,32 @@ def func():
366371
""",
367372
},
368373
) 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
391400

392401

393402
def test_import_resolution_default_conflicts_overrite(tmpdir: str, monkeypatch) -> None:
@@ -412,34 +421,38 @@ def func():
412421
""",
413422
},
414423
) 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
443456

444457

445458
def test_import_resolution_init_wildcard(tmpdir: str) -> None:

0 commit comments

Comments
 (0)