Skip to content

Commit 613aab8

Browse files
committed
Fix square bracket file name issue
1 parent 35c35b3 commit 613aab8

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

gpt_engineer/chat_to_files.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def parse_chat(chat): # -> List[Tuple[str, str]]:
1111
# Strip the filename of any non-allowed characters and convert / to \
1212
path = re.sub(r'[<>"|?*]', "", match.group(1))
1313

14+
# Remove leading and trailing brackets
15+
path = re.sub(r"^\[(.*)\]$", r"\1", path)
16+
1417
# Get the code
1518
code = match.group(2)
1619

tests/test_chat_to_files.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import textwrap
2+
3+
from gpt_engineer.chat_to_files import to_files
4+
5+
6+
def test_to_files():
7+
chat = textwrap.dedent(
8+
"""
9+
This is a sample program.
10+
11+
file1.py
12+
```python
13+
print("Hello, World!")
14+
```
15+
16+
file2.py
17+
```python
18+
def add(a, b):
19+
return a + b
20+
```
21+
"""
22+
)
23+
24+
workspace = {}
25+
to_files(chat, workspace)
26+
27+
assert workspace["all_output.txt"] == chat
28+
29+
expected_files = {
30+
"file1.py": 'print("Hello, World!")\n',
31+
"file2.py": "def add(a, b):\n return a + b\n",
32+
"README.md": "\nThis is a sample program.\n\nfile1.py\n",
33+
}
34+
35+
for file_name, file_content in expected_files.items():
36+
assert workspace[file_name] == file_content
37+
38+
39+
def test_to_files_with_square_brackets():
40+
chat = textwrap.dedent(
41+
"""
42+
This is a sample program.
43+
44+
[file1.py]
45+
```python
46+
print("Hello, World!")
47+
```
48+
49+
[file2.py]
50+
```python
51+
def add(a, b):
52+
return a + b
53+
```
54+
"""
55+
)
56+
workspace = {}
57+
to_files(chat, workspace)
58+
59+
assert workspace["all_output.txt"] == chat
60+
61+
expected_files = {
62+
"file1.py": 'print("Hello, World!")\n',
63+
"file2.py": "def add(a, b):\n return a + b\n",
64+
"README.md": "\nThis is a sample program.\n\n[file1.py]\n",
65+
}
66+
67+
for file_name, file_content in expected_files.items():
68+
assert workspace[file_name] == file_content
69+
70+
71+
def test_files_with_brackets_in_name():
72+
chat = textwrap.dedent(
73+
"""
74+
This is a sample program.
75+
76+
[id].jsx
77+
```javascript
78+
console.log("Hello, World!")
79+
```
80+
"""
81+
)
82+
83+
workspace = {}
84+
to_files(chat, workspace)
85+
86+
assert workspace["all_output.txt"] == chat
87+
88+
expected_files = {
89+
"[id].jsx": 'console.log("Hello, World!")\n',
90+
"README.md": "\nThis is a sample program.\n\n[id].jsx\n",
91+
}
92+
93+
for file_name, file_content in expected_files.items():
94+
assert workspace[file_name] == file_content

0 commit comments

Comments
 (0)