Skip to content

Commit

Permalink
test: improved test_fileblock and fixed --no-confirm for overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 5, 2023
1 parent 6d580af commit cbc0ffe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions gptme/tools/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,31 @@ def execute_save(
return

with open(path, "a") as f:
f.write("\n" + code)
f.write(code)
yield Message("system", f"Appended to {fn}")
return

# if the file exists, ask to overwrite
if path.exists():
overwrite = ask_execute("File exists, overwrite?")
print()
if ask:
overwrite = ask_execute("File exists, overwrite?")
print()
else:
overwrite = True
print("Skipping overwrite confirmation.")
if not overwrite:
# early return
yield Message("system", "Save cancelled.")
return

# if the folder doesn't exist, ask to create it
if not path.parent.exists():
create = ask_execute("Folder doesn't exist, create it?")
print()
if ask:
create = ask_execute("Folder doesn't exist, create it?")
print()
else:
create = True
print("Skipping folder creation confirmation.")
if create:
path.parent.mkdir(parents=True)
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def test_command_fork(args: list[str], runner: CliRunner, name: str):

@pytest.mark.slow
def test_fileblock(args: list[str], runner: CliRunner):
args_orig = args.copy()

# tests saving with a ```filename.txt block
args.append(f"{CMDFIX}impersonate ```hello.py\nprint('hello')\n```")
result = runner.invoke(gptme.cli.main, args)
Expand All @@ -106,6 +108,23 @@ def test_fileblock(args: list[str], runner: CliRunner):
content = f.read()
assert content == "print('hello')\n"

# test append
args = args_orig.copy()
args.append(f"{CMDFIX}impersonate ```append hello.py\nprint('world')\n```")
result = runner.invoke(gptme.cli.main, args)
assert result.exit_code == 0

# read the file
with open("hello.py", "r") as f:
content = f.read()
assert content == "print('hello')\nprint('world')\n"

# test write file to directory that doesn't exist
args = args_orig.copy()
args.append(f"{CMDFIX}impersonate ```hello/hello.py\nprint('hello')\n```")
result = runner.invoke(gptme.cli.main, args)
assert result.exit_code == 0


def test_shell(args: list[str], runner: CliRunner):
args.append(f"{CMDFIX}shell echo 'yes'")
Expand Down

0 comments on commit cbc0ffe

Please sign in to comment.