Skip to content

Commit

Permalink
Fixes to tests and paths rel to cwd/project dir by Axel
Browse files Browse the repository at this point in the history
  • Loading branch information
similato87 committed Jan 5, 2024
1 parent 6cad645 commit 9ac23f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
24 changes: 17 additions & 7 deletions gpt_engineer/applications/cli/file_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,15 @@ def ask_for_files(project_path: Union[str, Path]) -> FilesDict:
"""

metadata_db = DiskMemory(metadata_path(project_path))
if os.getenv("TEST_MODE"):
print("Test mode: Simulating file selection")
resolved_path = Path(project_path).resolve()
all_files = list(resolved_path.glob("**/*"))
selected_files = [file for file in all_files if file.is_file()]
if os.getenv("GPTE_TEST_MODE"):
assert FILE_LIST_NAME in metadata_db
selected_files = get_files_from_toml(
project_path, metadata_db.path / FILE_LIST_NAME
)
# print("Test mode: Simulating file selection")
# resolved_path = Path(project_path).resolve()
# all_files = list(resolved_path.glob("**/*"))
# selected_files = [file for file in all_files if file.is_file()]
else:
if FILE_LIST_NAME in metadata_db:
print(
Expand All @@ -293,9 +297,11 @@ def ask_for_files(project_path: Union[str, Path]) -> FilesDict:
selected_files = editor_file_selector(project_path, True)
content_dict = {}
for file_path in selected_files:
# selected files contains paths that are relative to the project path
file_path = Path(file_path)
try:
with open(file_path, "r") as content:
# to open the file we need the path from the cwd
with open(Path(project_path) / file_path, "r") as content:
content_dict[str(file_path)] = content.read()
except FileNotFoundError:
print(f"Warning: File not found {file_path}")
Expand Down Expand Up @@ -381,11 +387,15 @@ def editor_file_selector(input_path: str, init: bool = True) -> List[str]:
)
open_with_default_editor(toml_file)

return get_files_from_toml(input_path, toml_file)


def get_files_from_toml(input_path, toml_file):
selected_files = []
edited_tree = toml.load(toml_file)
for file, properties in edited_tree["files"].items():
if properties.get("selected", False):
selected_files.append(str(Path(input_path).joinpath(file).resolve()))
selected_files.append(file)

if not selected_files:
raise Exception(
Expand Down
17 changes: 13 additions & 4 deletions tests/applications/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gpt_engineer.applications.cli.main as main

from gpt_engineer.core.default.disk_execution_env import DiskExecutionEnv
from gpt_engineer.core.default.paths import ENTRYPOINT_FILE
from gpt_engineer.core.default.paths import ENTRYPOINT_FILE, META_DATA_REL_PATH
from tests.caching_ai import CachingAI

main.AI = CachingAI
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_default_settings_generate_project(self, tmp_path, monkeypatch):
# Runs gpt-engineer with improve mode and improves an existing project in the specified path.
def test_improve_existing_project(self, tmp_path, monkeypatch):
def improve_generator():
yield "y" # First response
yield "y"
while True:
yield "n" # Subsequent responses

Expand All @@ -75,14 +75,23 @@ def improve_generator():
p = tmp_path / "projects/example"
p.mkdir(parents=True)
(p / "prompt").write_text(prompt_text)
os.environ["TEST_MODE"] = "True"
(p / "main.py").write_text("The program will be written in this file")
meta_p = p / META_DATA_REL_PATH
meta_p.mkdir(parents=True)
(meta_p / "file_selection.toml").write_text(
"""
[files."main.py"]
selected = true
"""
)
os.environ["GPTE_TEST_MODE"] = "True"
simplified_main(str(p), "improve")
ex_env = DiskExecutionEnv(path=p)
ex_env.run(f"bash {ENTRYPOINT_FILE}")
assert (p / "output.txt").exists()
text = (p / "output.txt").read_text().strip()
assert text == "hello"
del os.environ["TEST_MODE"]
del os.environ["GPTE_TEST_MODE"]

# Runs gpt-engineer with lite mode and generates a project with only the main prompt.
def test_lite_mode_generate_project(self, tmp_path, monkeypatch):
Expand Down

0 comments on commit 9ac23f9

Please sign in to comment.