Skip to content

Commit 7619895

Browse files
authored
Fix get_codebase_session local filesystem corruption + Fix codebase verify_output (#558)
**This PR is broken down into three parts:** - Fix rare bug where `get_codebase_session` would modify files in the `codegen-sdk` repo instead of the test - Fix & re-enable `verify_output` logic - Fix any broken tests caused by `verify_output`
1 parent 3211e6f commit 7619895

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

src/codegen/sdk/codebase/factory/get_session.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from codegen.sdk.codebase.config import CodebaseConfig, ProjectConfig, SessionOptions, TestFlags
1010
from codegen.sdk.codebase.factory.codebase_factory import CodebaseFactory
1111
from codegen.sdk.core.codebase import Codebase, PyCodebaseType, TSCodebaseType
12+
from codegen.sdk.core.file import SourceFile
1213
from codegen.sdk.tree_sitter_parser import print_errors
1314
from codegen.shared.configs.models.feature_flags import CodebaseFeatureFlags
1415
from codegen.shared.configs.models.secrets import SecretsConfig
@@ -83,9 +84,15 @@ def get_codebase_session(
8384
):
8485
if verify_input:
8586
for file in codebase.files:
86-
if os.path.exists(file.filepath):
87-
print_errors(file.filepath, file.content)
88-
assert not file.ts_node.has_error, "Invalid syntax in test case"
87+
# NOTE: We only check SourceFiles for syntax errors
88+
abs_filepath = os.path.join(tmpdir, file.filepath)
89+
if os.path.exists(abs_filepath):
90+
if isinstance(file, SourceFile):
91+
# Check for syntax errors
92+
print_errors(abs_filepath, file.content)
93+
if file.ts_node.has_error:
94+
msg = "Invalid syntax in test case"
95+
raise SyntaxError(msg)
8996
yield codebase
9097

9198
if verify_output:

tests/unit/codegen/sdk/codebase/codebase_graph/test_codebase_reset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,18 @@ def __init__(self):
200200
@pytest.mark.parametrize(
201201
"original, expected",
202202
[
203-
({"src/a.py": "original content"}, {"src/a.py": "modified content", "src/b.py": "new file content"}),
203+
({"src/a.py": "# original content"}, {"src/a.py": "# modified content", "src/b.py": "# new file content"}),
204204
],
205205
indirect=["original", "expected"],
206206
)
207207
def test_codebase_reset_preserves_external_changes(codebase: Codebase, assert_expected, tmp_path):
208208
# Make external changes to existing file
209209
src_dir = tmp_path / "src"
210210
src_dir.mkdir(exist_ok=True)
211-
(src_dir / "a.py").write_text("modified content")
211+
(src_dir / "a.py").write_text("# modified content")
212212

213213
# Add new file externally
214-
(src_dir / "b.py").write_text("new file content")
214+
(src_dir / "b.py").write_text("# new file content")
215215

216216
# Reset should detect and preserve these changes
217217
codebase.commit()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello World!")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
from codegen.sdk.codebase.factory.get_session import get_codebase_session
4+
from codegen.shared.enums.programming_language import ProgrammingLanguage
5+
6+
7+
def test_get_codebase_session(tmpdir) -> None:
8+
# READ THIS CONTEXT BEFORE EDITING THIS TEST!
9+
# Essentially, there was this bug where the file in the base codegen-sdk repo was being edited instead of the file in the tmpdir
10+
# This test is to ensure that this bug does not happen again.
11+
# target_file must be a file that shares the same relative path in the codegen-sdk repo
12+
13+
# Init Test
14+
target_file = "tests/unit/codegen/sdk/codebase/session/target_python_file.py"
15+
assert os.path.exists(target_file), f"Target file {target_file} does not exist! Please change this to a file that exists in codegen-sdk"
16+
target_orig_content = open(target_file).read()
17+
18+
# Setup dummy test
19+
try:
20+
with get_codebase_session(
21+
tmpdir=tmpdir,
22+
# NOTE: For this edge case in particular, the final file SHOULD be an invalid python file!
23+
files={target_file: "This should not be here\n" + target_orig_content},
24+
programming_language=ProgrammingLanguage.PYTHON,
25+
verify_output=True,
26+
) as codebase:
27+
file = codebase.get_file(target_file)
28+
assert file.content is not None
29+
assert file.content != ""
30+
assert file.content != target_orig_content
31+
assert "This should not be here" in file.content
32+
except SyntaxError:
33+
pass # This is expected!
34+
35+
# Verify the file on codegen-sdk was not edited
36+
assert open(target_file).read() == target_orig_content
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from codegen.sdk.codebase.factory.get_session import get_codebase_session
2+
from codegen.shared.enums.programming_language import ProgrammingLanguage
3+
4+
5+
def test_verify_output_python(tmpdir) -> None:
6+
try:
7+
with get_codebase_session(
8+
tmpdir=tmpdir,
9+
files={"invalid.py": "print('Hello, world!'", "valid.py": "print('Hello, world!')"},
10+
programming_language=ProgrammingLanguage.PYTHON,
11+
verify_output=True,
12+
) as codebase:
13+
pass
14+
15+
# If we reach here, the verify_output failed
16+
msg = "Verify output failed"
17+
raise Exception(msg)
18+
except SyntaxError:
19+
pass

0 commit comments

Comments
 (0)