Skip to content

Commit ee4dd5c

Browse files
committed
holy shit add tests!!!
1 parent 904f6e2 commit ee4dd5c

File tree

6 files changed

+162
-121
lines changed

6 files changed

+162
-121
lines changed

codegpt/main.py

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,99 @@
1616

1717
app = typer.Typer()
1818

19+
1920
@app.command("do")
2021
def edit_file(
2122
instruction: str = typer.Argument(
22-
..., help="Instruction to edit the file(s). Keep it short! Wrap with quotes.",
23+
...,
24+
help="Instruction to edit the file(s). Keep it short! Wrap with quotes.",
2325
),
2426
filenames: List[Path] = typer.Argument(
25-
[], help="List of filenames to edit. If not provided, will prompt for input.",
27+
["#CLIPBOARD"],
28+
help="List of filenames to edit. If not provided, will prompt for input.",
2629
),
2730
backup: bool = typer.Option(
28-
False, "--backup", "-b", help="Whether to create a backup of the original file(s).",
31+
False,
32+
"--backup",
33+
"-b",
34+
help="Whether to create a backup of the original file(s).",
35+
),
36+
yes: bool = typer.Option(
37+
False,
38+
"--yes",
39+
"-y",
40+
help="Don't ask for confirmation.",
41+
),
42+
raw_code: str = typer.Option(
43+
None,
44+
"--raw-code",
45+
"-c",
46+
help="Raw code to edit. Overrides filenames. Use quotes to wrap the code.",
47+
),
48+
json_out: bool = typer.Option(
49+
False, "--json-out", "-j", help="Output the response in raw json format."
50+
),
51+
raw_out: bool = typer.Option(
52+
False,
53+
"--raw-out",
54+
"-r",
55+
help="Output the raw 'code' from the response and exit the function.",
2956
),
30-
yes: bool = typer.Option(False, "--yes", "-y", help="Don't ask for confirmation.",),
31-
raw_code: str = typer.Option(None, "--raw-code", "-c", help="Raw code to edit. Overrides filenames"),
32-
json_out: bool = typer.Option(False, "--json-out", "-j", help="Output to raw json."),
33-
):
57+
):
3458
"""
3559
Do something given some code for context. Asking for documents, queries, etc. should work okay. Edits are iffy, but work a lot of the time.
36-
37-
Your code better be in git before you use this.
60+
61+
Your code better be in git before you use this. If the instruction is one of the quick prompt options (like 'comment' or 'docs'), it will do that prompt automatically. For more info, run 'codegpt quick --help'.
3862
3963
FILENAMES: list of filenames to edit. If not provided, will prompt for input.
4064
INSTRUCTION: the instruction to edit the file(s). Keep it short!
4165
"""
4266

4367
if not filenames and not raw_code:
44-
raise typer.BadParameter("Either FILENAMES or --raw-code (-c) must be provided.")
68+
raise typer.BadParameter(
69+
"Either filenames or --raw-code (-c) must be provided."
70+
)
71+
4572
code = {"code": raw_code} if raw_code else files.load_text(filenames)
73+
74+
if instruction in prompts.prompts:
75+
instruction = prompts.prompts[instruction]
76+
4677
result = gpt.send_iffy_edit(instruction, code, yes=yes, clipboard=bool(raw_code))
4778

4879
if json_out:
4980
return json.dumps(result, sort_keys=True, indent=4)
5081

82+
if raw_out:
83+
return 'result["code"]'
84+
5185
files.write_text(result, backup)
5286
typer.secho("Done!", color=typer.colors.BRIGHT_BLUE)
5387

5488

5589
@app.command("quick")
5690
def quick_edit_file(
5791
option: str = typer.Argument(..., help=f"{{{'|'.join(prompts.prompts.keys())}}}"),
58-
filenames: List[str] = typer.Argument(..., help="Enter the filenames to edit, separated by spaces"),
59-
backup: bool = typer.Option(
60-
False, "--backup", "-b", help="Whether to create a backup of the original file(s).",
92+
filenames: List[str] = typer.Argument(
93+
..., help="Enter the filenames to edit, separated by spaces"
94+
),
95+
backup: bool = typer.Option(
96+
False,
97+
"--backup",
98+
"-b",
99+
help="Whether to create a backup of the original file(s).",
100+
),
101+
yes: bool = typer.Option(
102+
False,
103+
"--yes",
104+
"-y",
105+
help="Don't ask for confirmation.",
106+
),
107+
raw_code: str = typer.Option(
108+
None, "--raw-code", "-c", help="Raw code to edit. Overrides filenames"
61109
),
62-
yes: bool = typer.Option(False, "--yes", "-y", help="Don't ask for confirmation.",),
63-
raw_code: str = typer.Option(None, "--raw-code", "-c", help="Raw code to edit. Overrides filenames"),
64110
json_out: bool = typer.Option(False, "--json", "-j", help="Output in JSON format"),
65-
):
111+
):
66112
"""
67113
Edit a file using codegpt's built in prompts.
68114
@@ -75,13 +121,19 @@ def quick_edit_file(
75121
- vulns - Comment in code where the vulns are if GPT sees them (iffy)
76122
"""
77123
if option not in prompts.prompts:
78-
raise typer.BadParameter(f"{option} is not a valid option. Must be one of {list(prompts.prompts.keys())}")
124+
raise typer.BadParameter(
125+
f"{option} is not a valid option. Must be one of {list(prompts.prompts.keys())}"
126+
)
79127

80128
if not filenames and not raw_code:
81-
raise typer.BadParameter("Either FILENAMES or --raw-code (-c) must be provided.")
129+
raise typer.BadParameter(
130+
"Either FILENAMES or --raw-code (-c) must be provided."
131+
)
82132

83133
code = {"code": raw_code} if raw_code else files.load_text(filenames)
84-
result = gpt.send_iffy_edit(prompts.prompts[option], code, yes=yes, clipboard=bool(raw_code))
134+
result = gpt.send_iffy_edit(
135+
prompts.prompts[option], code, yes=yes, clipboard=bool(raw_code)
136+
)
85137

86138
if json_out:
87139
return json.dumps(result, sort_keys=True, indent=4)
@@ -97,7 +149,10 @@ def config():
97149
"""
98150
# check if the secret key is already set in the environment variables
99151
if "OPENAI_SECRET_KEY" in os.environ:
100-
typer.secho("OPENAI_SECRET_KEY is already set in the environment! You probably don't need this.", typer.colors.BRIGHT_BLUE)
152+
typer.secho(
153+
"OPENAI_SECRET_KEY is already set in the environment! You probably don't need this.",
154+
typer.colors.BRIGHT_BLUE,
155+
)
101156
else:
102157
typer.confirm(
103158
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "codegpt"
3-
version = "0.2.10"
3+
version = "0.2.11"
44
license = "GPL-3.0-or-later"
55
description = "A CLI tool for refactoring code using OpenAI's models"
66
authors = ["John Partee"]

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
python_files = test_*.py
3+
testpaths = tests

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subprocess
2+
import sys
3+
from tqdm import tqdm
4+
5+
6+
import subprocess
7+
import sys
8+
from tqdm import tqdm
9+
10+
11+
def pytest_configure(config):
12+
print("Building project...")
13+
with tqdm(total=1, desc="Building project", file=sys.stdout) as pbar:
14+
subprocess.run(["poetry", "build"])
15+
pbar.update(1)
16+
print("Installing project...")
17+
with tqdm(total=1, desc="Installing project", file=sys.stdout) as pbar:
18+
subprocess.run(["pip", "install", "--force", "--find-links=dist", "codegpt"])
19+
pbar.update(1)
20+
print("Installation complete.")
21+
22+
23+
if __name__ == "__main__":
24+
pytest_configure({})

tests/test_edit_file.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import re
2+
from codegpt.main import edit_file
3+
4+
5+
def test_edit_file_add_comment():
6+
# Set up the test inputs
7+
instruction = "comment"
8+
raw_code = "def foo():\n print('Hello, world!')"
9+
10+
# Call the function being tested
11+
result = edit_file(instruction, raw_code=raw_code, yes=True, raw_out=True)
12+
13+
# Use a regex to match the output of the function
14+
assert "#" in result
15+
assert "def foo():" in result
16+
assert "print('Hello, world!')" in result
17+
18+
19+
def test_edit_file_reverse_string():
20+
# Set up the test inputs and expected outputs
21+
instruction = "reverse this string"
22+
raw_code = "abcdef"
23+
expected_output = {"code": "fedcba"}
24+
25+
# Call the function being tested
26+
result = edit_file(instruction, raw_code=raw_code, yes=True, raw_out=True)
27+
28+
print(result)
29+
30+
# Verify that the function is behaving as expected
31+
assert len(result) >= len(expected_output["code"])
32+
33+
34+
def test_edit_file_convert_to_uppercase():
35+
# Set up the test inputs and expected outputs
36+
instruction = "make this uppercase"
37+
raw_code = "abcdef"
38+
expected_output = "ABCDEF"
39+
40+
# Call the function being tested
41+
result = edit_file(instruction, raw_code=raw_code, yes=True, raw_out=True)
42+
43+
print(result)
44+
45+
# Verify that the function is behaving as expected
46+
assert len(result) >= len(expected_output)

0 commit comments

Comments
 (0)