From 613aab8ef731b733c17a51e6c4c94cf471b7537f Mon Sep 17 00:00:00 2001 From: David Cameron Date: Sun, 18 Jun 2023 19:07:58 -0400 Subject: [PATCH] Fix square bracket file name issue --- gpt_engineer/chat_to_files.py | 3 ++ tests/test_chat_to_files.py | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/test_chat_to_files.py diff --git a/gpt_engineer/chat_to_files.py b/gpt_engineer/chat_to_files.py index a043eae..f4812d4 100644 --- a/gpt_engineer/chat_to_files.py +++ b/gpt_engineer/chat_to_files.py @@ -11,6 +11,9 @@ def parse_chat(chat): # -> List[Tuple[str, str]]: # Strip the filename of any non-allowed characters and convert / to \ path = re.sub(r'[<>"|?*]', "", match.group(1)) + # Remove leading and trailing brackets + path = re.sub(r"^\[(.*)\]$", r"\1", path) + # Get the code code = match.group(2) diff --git a/tests/test_chat_to_files.py b/tests/test_chat_to_files.py new file mode 100644 index 0000000..176c19b --- /dev/null +++ b/tests/test_chat_to_files.py @@ -0,0 +1,94 @@ +import textwrap + +from gpt_engineer.chat_to_files import to_files + + +def test_to_files(): + chat = textwrap.dedent( + """ + This is a sample program. + + file1.py + ```python + print("Hello, World!") + ``` + + file2.py + ```python + def add(a, b): + return a + b + ``` + """ + ) + + workspace = {} + to_files(chat, workspace) + + assert workspace["all_output.txt"] == chat + + expected_files = { + "file1.py": 'print("Hello, World!")\n', + "file2.py": "def add(a, b):\n return a + b\n", + "README.md": "\nThis is a sample program.\n\nfile1.py\n", + } + + for file_name, file_content in expected_files.items(): + assert workspace[file_name] == file_content + + +def test_to_files_with_square_brackets(): + chat = textwrap.dedent( + """ + This is a sample program. + + [file1.py] + ```python + print("Hello, World!") + ``` + + [file2.py] + ```python + def add(a, b): + return a + b + ``` + """ + ) + workspace = {} + to_files(chat, workspace) + + assert workspace["all_output.txt"] == chat + + expected_files = { + "file1.py": 'print("Hello, World!")\n', + "file2.py": "def add(a, b):\n return a + b\n", + "README.md": "\nThis is a sample program.\n\n[file1.py]\n", + } + + for file_name, file_content in expected_files.items(): + assert workspace[file_name] == file_content + + +def test_files_with_brackets_in_name(): + chat = textwrap.dedent( + """ + This is a sample program. + + [id].jsx + ```javascript + console.log("Hello, World!") + ``` + """ + ) + + workspace = {} + to_files(chat, workspace) + + assert workspace["all_output.txt"] == chat + + expected_files = { + "[id].jsx": 'console.log("Hello, World!")\n', + "README.md": "\nThis is a sample program.\n\n[id].jsx\n", + } + + for file_name, file_content in expected_files.items(): + assert workspace[file_name] == file_content