-
Notifications
You must be signed in to change notification settings - Fork 54
Support Codebase.from_string
and Codebase.from_files
#630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tests/unit/codegen/sdk/codebase/session/test_codebase_from_files.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pytest | ||
|
||
from codegen.sdk.core.codebase import Codebase | ||
|
||
|
||
def test_from_files_python(): | ||
"""Test creating a Python codebase from multiple files""" | ||
files = {"main.py": "from utils import add\nprint(add(1, 2))", "utils.py": "def add(a, b):\n return a + b"} | ||
# Language is optional, will be inferred | ||
codebase = Codebase.from_files(files) | ||
assert len(codebase.files) == 2 | ||
assert any(f.filepath.endswith("main.py") for f in codebase.files) | ||
assert any(f.filepath.endswith("utils.py") for f in codebase.files) | ||
assert any("from utils import add" in f.content for f in codebase.files) | ||
|
||
|
||
def test_from_files_typescript(): | ||
"""Test creating a TypeScript codebase from multiple files""" | ||
files = {"index.ts": "import { add } from './utils';\nconsole.log(add(1, 2));", "utils.ts": "export function add(a: number, b: number): number {\n return a + b;\n}"} | ||
# Language is optional, will be inferred | ||
codebase = Codebase.from_files(files) | ||
assert len(codebase.files) == 2 | ||
assert any(f.filepath.endswith("index.ts") for f in codebase.files) | ||
assert any(f.filepath.endswith("utils.ts") for f in codebase.files) | ||
assert any("import { add }" in f.content for f in codebase.files) | ||
|
||
|
||
def test_from_files_empty(): | ||
"""Test creating a codebase with no files""" | ||
# Defaults to Python when no files provided | ||
codebase = Codebase.from_files({}) | ||
assert len(codebase.files) == 0 | ||
|
||
|
||
def test_from_files_mixed_extensions(): | ||
"""Test files with mixed extensions raises error""" | ||
files = {"main.py": "print('hello')", "test.ts": "console.log('world')"} | ||
with pytest.raises(ValueError, match="Cannot determine single language from extensions"): | ||
Codebase.from_files(files) | ||
|
||
|
||
def test_from_files_typescript_multiple_extensions(): | ||
"""Test TypeScript codebase with various valid extensions""" | ||
files = { | ||
"index.ts": "console.log('hi')", | ||
"component.tsx": "export const App = () => <div>Hello</div>", | ||
"utils.js": "module.exports = { add: (a, b) => a + b }", | ||
"button.jsx": "export const Button = () => <button>Click</button>", | ||
} | ||
# Language is optional, will be inferred as TypeScript | ||
codebase = Codebase.from_files(files) | ||
assert len(codebase.files) == 4 | ||
|
||
|
||
def test_from_files_explicit_language_mismatch(): | ||
"""Test error when explicit language doesn't match extensions""" | ||
files = {"main.py": "print('hello')", "utils.py": "def add(a, b): return a + b"} | ||
with pytest.raises(ValueError, match="Provided language.*doesn't match inferred language"): | ||
Codebase.from_files(files, language="typescript") | ||
|
||
|
||
def test_from_files_explicit_language_match(): | ||
"""Test explicit language matching file extensions works""" | ||
files = {"main.py": "print('hello')", "utils.py": "def add(a, b): return a + b"} | ||
codebase = Codebase.from_files(files, language="python") | ||
assert len(codebase.files) == 2 |
78 changes: 78 additions & 0 deletions
78
tests/unit/codegen/sdk/codebase/session/test_codebase_from_string.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
|
||
from codegen.sdk.core.codebase import Codebase | ||
from codegen.shared.enums.programming_language import ProgrammingLanguage | ||
|
||
|
||
def test_from_string_python(): | ||
"""Test creating a Python codebase from string""" | ||
code = """ | ||
def hello(): | ||
return "world" | ||
""" | ||
codebase = Codebase.from_string(code, language="python") | ||
assert len(codebase.files) == 1 | ||
assert codebase.files[0].filepath.endswith("test.py") | ||
assert "def hello" in codebase.files[0].content | ||
|
||
|
||
def test_from_string_typescript(): | ||
"""Test creating a TypeScript codebase from string""" | ||
code = """ | ||
function hello(): string { | ||
return "world"; | ||
} | ||
""" | ||
codebase = Codebase.from_string(code, language="typescript") | ||
assert len(codebase.files) == 1 | ||
assert codebase.files[0].filepath.endswith("test.ts") | ||
assert "function hello" in codebase.files[0].content | ||
|
||
|
||
def test_from_string_with_enum(): | ||
"""Test creating a codebase using ProgrammingLanguage enum""" | ||
code = "const x = 42;" | ||
codebase = Codebase.from_string(code, language=ProgrammingLanguage.TYPESCRIPT) | ||
assert len(codebase.files) == 1 | ||
assert codebase.files[0].filepath.endswith("test.ts") | ||
|
||
|
||
def test_from_string_invalid_syntax(): | ||
"""Test that invalid syntax is still accepted (parsing happens later)""" | ||
code = "this is not valid python" | ||
codebase = Codebase.from_string(code, language="python") | ||
assert len(codebase.files) == 1 | ||
assert codebase.files[0].content == code | ||
|
||
|
||
def test_from_string_empty(): | ||
"""Test creating a codebase from empty string""" | ||
codebase = Codebase.from_string("", language="python") | ||
assert len(codebase.files) == 1 | ||
assert codebase.files[0].content == "" | ||
|
||
|
||
def test_from_string_missing_language(): | ||
"""Test that language is required""" | ||
with pytest.raises(TypeError, match="missing.*required.*argument.*language"): | ||
Codebase.from_string("print('hello')") | ||
|
||
|
||
def test_from_string_invalid_language(): | ||
"""Test that invalid language raises error""" | ||
with pytest.raises(ValueError): | ||
Codebase.from_string("print('hello')", language="invalid") | ||
|
||
|
||
def test_from_string_multifile(): | ||
"""Test that multifile is not supported yet""" | ||
code = """ | ||
# file1.py | ||
def hello(): pass | ||
|
||
# file2.py | ||
def world(): pass | ||
""" | ||
# Still works, just puts everything in one file | ||
codebase = Codebase.from_string(code, language="python") | ||
assert len(codebase.files) == 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we not import this top level?