Skip to content

Commit 54d9d89

Browse files
jayhackcodegen-bot
and
codegen-bot
authored
feat: mdx docs and tests (#61)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed - [ ] I have read and agree to the [Contributor License Agreement](../CLA.md) --------- Co-authored-by: codegen-bot <[email protected]>
1 parent 5345330 commit 54d9d89

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

docs/building-with-codegen/files-and-directories.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ dir = file.directory
5050
exists = codebase.has_directory("path/to/dir")
5151
```
5252

53+
## Working with Non-Code Files (README, JSON, etc.)
54+
55+
By default, Codegen focuses on source code files (Python, TypeScript, etc). However, you can access all files in your codebase, including documentation, configuration, and other non-code files like README.md, package.json, or .env:
56+
57+
```python
58+
# Get all files in the codebase (including README, docs, config files)
59+
files = codebase.files(extensions="*")
60+
61+
# Print files that are not source code (documentation, config, etc)
62+
for file in files:
63+
if not file.filepath.endswith(('.py', '.ts', '.js')):
64+
print(f"📄 Non-code file: {file.filepath}")
65+
```
66+
67+
You can also filter for specific file types:
68+
69+
```python
70+
# Get only markdown documentation files
71+
docs = codebase.files(extensions=[".md", ".mdx"])
72+
73+
# Get configuration files
74+
config_files = codebase.files(extensions=[".json", ".yaml", ".toml"])
75+
```
76+
77+
These APIs are similar for [`Directory`](../api-reference/core/Directory), which provides similar methods for accessing files and subdirectories.
78+
5379
## Raw Content and Metadata
5480

5581
```python

src/codegen/sdk/core/codebase.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ def files(self, *, extensions: list[str] | Literal["*"] | None = None) -> list[T
224224
if extensions is None:
225225
# Return all source files
226226
files = self.G.get_nodes(NodeType.FILE)
227+
elif isinstance(extensions, str) and extensions != "*":
228+
raise ValueError("extensions must be a list of extensions or '*'")
227229
else:
228230
files = []
229231
# Get all files with the specified extensions

tests/unit/codebase/file/test_file.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ def test_codebase_files(tmpdir) -> None:
5959
assert {f for f in codebase.files(extensions=[".bin"])} == {file3}
6060

6161

62+
@pytest.mark.skip("MDX editing is broken")
63+
def test_codebase_edit_mdx(tmpdir) -> None:
64+
"""Editing MDx seems broken currently - it will just prepend to the file"""
65+
with get_codebase_session(tmpdir=tmpdir, files={"file1.mdx": "# Header", "file2.tsx": "console.log('hello, world!')"}) as codebase:
66+
file = codebase.get_file("file1.mdx")
67+
file.edit("NEW TEXT")
68+
codebase.commit()
69+
file = codebase.get_file("file1.mdx")
70+
assert file.content == "NEW TEXT"
71+
72+
73+
@pytest.mark.skip("MDX replacing is broken")
74+
def test_codebase_replace_mdx(tmpdir) -> None:
75+
"""Editing MDx seems broken currently - it will just prepend to the file"""
76+
with get_codebase_session(tmpdir=tmpdir, files={"file1.mdx": "# Header"}) as codebase:
77+
file = codebase.get_file("file1.mdx")
78+
file.replace("# Header", "NEW TEXT")
79+
codebase.commit()
80+
file = codebase.get_file("file1.mdx")
81+
assert file.content == "NEW TEXT"
82+
83+
6284
@pytest.mark.skipif(sys.platform == "darwin", reason="macOS is case-insensitive")
6385
def test_file_extensions_ignore_case(tmpdir) -> None:
6486
with get_codebase_session(tmpdir=tmpdir, files={"file1.py": "print(123)", "file2.py": "print(456)", "file3.bin": b"\x89PNG", "file4": "Hello world!"}) as codebase:

0 commit comments

Comments
 (0)