Skip to content

Commit edd4fc6

Browse files
committed
Remove use_cwd
1 parent 1a94c72 commit edd4fc6

File tree

3 files changed

+75
-106
lines changed

3 files changed

+75
-106
lines changed

src/codegen/sdk/utils.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -339,29 +339,3 @@ 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)

tests/unit/codegen/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pytest
24

35
from codegen.sdk.codebase.factory.get_session import get_codebase_session

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

Lines changed: 73 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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
65

76
if TYPE_CHECKING:
87
from codegen.sdk.core.file import SourceFile
@@ -273,31 +272,29 @@ def func():
273272
""",
274273
},
275274
) 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")
280277

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
285282

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
291288

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

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
301298

302299

303300
def test_import_resolution_file_custom_resolve_path(tmpdir: str) -> None:
@@ -371,32 +368,30 @@ def func():
371368
""",
372369
},
373370
) 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
400395

401396

402397
def test_import_resolution_default_conflicts_overrite(tmpdir: str, monkeypatch) -> None:
@@ -421,38 +416,36 @@ def func():
421416
""",
422417
},
423418
) 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
456449

457450

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

0 commit comments

Comments
 (0)