2023-12-18 20:46:16 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2024-01-05 12:27:57 +08:00
|
|
|
import click
|
2024-01-03 16:06:17 +08:00
|
|
|
from chat.ask_codebase.tools.retrieve_file_content import retrieve_file_content
|
2023-12-18 20:46:16 +08:00
|
|
|
from find_reference_tests import find_reference_tests
|
2023-12-24 16:58:09 +08:00
|
|
|
from i18n import TUILanguage, get_translation
|
2024-01-03 16:06:17 +08:00
|
|
|
from model import FuncToTest, TokenBudgetExceededException, UserCancelledException
|
2024-01-05 12:27:57 +08:00
|
|
|
from propose_test import propose_test
|
|
|
|
from write_tests import write_and_print_tests
|
2023-12-18 20:46:16 +08:00
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs"))
|
|
|
|
|
2024-01-05 12:27:57 +08:00
|
|
|
from chatmark import Checkbox, Form, TextEditor # noqa: E402
|
2023-12-24 18:38:59 +08:00
|
|
|
from ide_services import ide_language # noqa: E402
|
|
|
|
|
2023-12-18 20:46:16 +08:00
|
|
|
|
2023-12-28 15:56:15 +08:00
|
|
|
def generate_unit_tests_workflow(
|
2023-12-18 20:46:16 +08:00
|
|
|
user_prompt: str,
|
2023-12-28 15:56:15 +08:00
|
|
|
func_to_test: FuncToTest,
|
|
|
|
tui_lang: TUILanguage,
|
2023-12-18 20:46:16 +08:00
|
|
|
):
|
2023-12-28 15:56:15 +08:00
|
|
|
"""
|
|
|
|
The main workflow for generating unit tests.
|
|
|
|
"""
|
2023-12-18 20:46:16 +08:00
|
|
|
repo_root = os.getcwd()
|
2023-12-24 16:58:09 +08:00
|
|
|
|
2023-12-28 15:56:15 +08:00
|
|
|
_i = get_translation(tui_lang)
|
2023-12-18 20:46:16 +08:00
|
|
|
|
2023-12-28 16:40:42 +08:00
|
|
|
msg = _i("Analyzing the function and current unit tests...")
|
2023-12-18 20:46:16 +08:00
|
|
|
print(
|
2023-12-28 16:40:42 +08:00
|
|
|
f"\n\n```Step\n# {msg}\n```\n",
|
2023-12-18 20:46:16 +08:00
|
|
|
flush=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
test_cases = propose_test(
|
|
|
|
user_prompt=user_prompt,
|
2023-12-28 15:56:15 +08:00
|
|
|
func_to_test=func_to_test,
|
2023-12-24 17:31:35 +08:00
|
|
|
chat_language=tui_lang.chat_language,
|
2023-12-18 20:46:16 +08:00
|
|
|
)
|
2023-12-28 15:56:15 +08:00
|
|
|
|
2024-01-11 21:48:48 +08:00
|
|
|
ref_files = find_reference_tests(repo_root, func_to_test.func_name, func_to_test.file_path)
|
2024-01-02 19:37:37 +08:00
|
|
|
ref_file = ref_files[0] if ref_files else ""
|
2023-12-18 20:46:16 +08:00
|
|
|
|
2024-01-02 19:37:37 +08:00
|
|
|
cases_checkbox = Checkbox(
|
|
|
|
options=test_cases,
|
|
|
|
title=_i("Select test cases to generate"),
|
2023-12-18 20:46:16 +08:00
|
|
|
)
|
2024-01-11 21:35:17 +08:00
|
|
|
ref_file_editor = TextEditor(
|
|
|
|
text=ref_file,
|
2024-01-11 21:48:48 +08:00
|
|
|
title=_i("Edit reference test file\n(Multiple files can be separated by line breaks)"),
|
2024-01-11 21:35:17 +08:00
|
|
|
)
|
2023-12-24 17:21:31 +08:00
|
|
|
|
2024-01-02 19:37:37 +08:00
|
|
|
form = Form(components=[cases_checkbox, ref_file_editor])
|
|
|
|
form.render()
|
2023-12-18 20:46:16 +08:00
|
|
|
|
2024-01-02 19:37:37 +08:00
|
|
|
selected_cases = [cases_checkbox.options[idx] for idx in cases_checkbox.selections]
|
2024-01-11 21:35:17 +08:00
|
|
|
new_refs = ref_file_editor.new_text
|
2023-12-18 20:46:16 +08:00
|
|
|
|
2024-01-03 16:06:17 +08:00
|
|
|
# Check user input
|
2024-01-03 16:32:39 +08:00
|
|
|
# Check if any test case is selected
|
2024-01-03 16:06:17 +08:00
|
|
|
if not cases_checkbox.selections:
|
2024-01-11 21:48:48 +08:00
|
|
|
raise UserCancelledException(_i("No test case is selected. Quit generating tests."))
|
2024-01-11 21:35:17 +08:00
|
|
|
|
|
|
|
# Validate reference files
|
|
|
|
valid_files = []
|
|
|
|
invalid_files = []
|
|
|
|
ref_files = [f.strip() for f in new_refs.split("\n")]
|
|
|
|
|
|
|
|
for ref_file in ref_files:
|
|
|
|
if not ref_file:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
retrieve_file_content(file_path=ref_file, root_path=repo_root)
|
|
|
|
valid_files.append(ref_file)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
invalid_files.append(ref_file)
|
|
|
|
|
|
|
|
title = ""
|
|
|
|
lines = []
|
|
|
|
if not valid_files:
|
2024-01-11 21:48:48 +08:00
|
|
|
title = _i("No valid file is provided. Will not use reference to generate tests.")
|
2024-01-11 21:35:17 +08:00
|
|
|
else:
|
|
|
|
title = _i("Will use the following reference files to generate tests:")
|
|
|
|
lines.append(_i("\nValid reference files:"))
|
|
|
|
lines.extend(valid_files)
|
|
|
|
|
|
|
|
if invalid_files:
|
|
|
|
lines.append(_i("\nInvalid files:"))
|
|
|
|
lines.extend(invalid_files)
|
|
|
|
|
|
|
|
info = "\n\n```Step\n"
|
|
|
|
info += f"# {title}\n"
|
|
|
|
info += "\n".join(lines)
|
|
|
|
info += "\n```\n\n"
|
|
|
|
print(info, flush=True)
|
2024-01-03 16:06:17 +08:00
|
|
|
|
2023-12-24 17:21:31 +08:00
|
|
|
write_and_print_tests(
|
2023-12-18 20:46:16 +08:00
|
|
|
root_path=repo_root,
|
2023-12-28 15:56:15 +08:00
|
|
|
func_to_test=func_to_test,
|
2023-12-18 20:46:16 +08:00
|
|
|
test_cases=selected_cases,
|
2024-01-11 21:35:17 +08:00
|
|
|
reference_files=valid_files,
|
2023-12-24 18:38:59 +08:00
|
|
|
chat_language=tui_lang.chat_language,
|
2023-12-18 20:46:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-28 15:56:15 +08:00
|
|
|
@click.command()
|
2024-01-04 22:45:07 +08:00
|
|
|
@click.argument("input", required=True)
|
|
|
|
def main(input: str):
|
|
|
|
"""
|
|
|
|
The main entry point for the unit tests generation workflow.
|
|
|
|
"/unit_tests {a}:::{b}:::{c}:::{d}:::{e}:::{f}"
|
|
|
|
"""
|
|
|
|
# Parse input
|
|
|
|
params = input.strip().split(":::")
|
|
|
|
assert len(params) == 6, f"Invalid input: {input}, number of params: {len(params)}"
|
|
|
|
|
|
|
|
(
|
|
|
|
file_path,
|
|
|
|
func_name,
|
|
|
|
func_start_line, # 0-based, inclusive
|
|
|
|
func_end_line, # 0-based, inclusive
|
|
|
|
container_start_line, # 0-based, inclusive
|
|
|
|
container_end_line, # 0-based, inclusive
|
|
|
|
) = params
|
|
|
|
|
|
|
|
try:
|
|
|
|
func_start_line = int(func_start_line)
|
|
|
|
func_end_line = int(func_end_line)
|
|
|
|
container_start_line = int(container_start_line)
|
|
|
|
container_end_line = int(container_end_line)
|
|
|
|
except Exception as e:
|
|
|
|
raise Exception(f"Invalid input: {input}, error: {e}")
|
|
|
|
|
|
|
|
user_prompt = f"Help me write unit tests for the `{func_name}` function"
|
|
|
|
|
2023-12-28 15:56:15 +08:00
|
|
|
repo_root = os.getcwd()
|
|
|
|
ide_lang = ide_language()
|
|
|
|
tui_lang = TUILanguage.from_str(ide_lang)
|
|
|
|
_i = get_translation(tui_lang)
|
|
|
|
|
|
|
|
# Use relative path in inner logic
|
|
|
|
if os.path.isabs(file_path):
|
|
|
|
file_path = os.path.relpath(file_path, repo_root)
|
|
|
|
|
|
|
|
func_to_test = FuncToTest(
|
|
|
|
func_name=func_name,
|
|
|
|
repo_root=repo_root,
|
|
|
|
file_path=file_path,
|
|
|
|
func_start_line=func_start_line,
|
|
|
|
func_end_line=func_end_line,
|
|
|
|
container_start_line=container_start_line,
|
|
|
|
container_end_line=container_end_line,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
generate_unit_tests_workflow(user_prompt, func_to_test, tui_lang)
|
|
|
|
|
|
|
|
except TokenBudgetExceededException as e:
|
2024-01-05 10:15:43 +08:00
|
|
|
msg = _i("The function's size surpasses AI's context capacity.")
|
2023-12-28 16:40:42 +08:00
|
|
|
|
|
|
|
info = "\n\n```Step\n"
|
|
|
|
info += f"# {msg}\n"
|
|
|
|
info += f"\n{e}\n```\n"
|
|
|
|
print(info, flush=True)
|
|
|
|
|
2024-01-03 16:06:17 +08:00
|
|
|
except UserCancelledException as e:
|
|
|
|
info = "\n\n```Step\n"
|
|
|
|
info += f"# {e}\n"
|
|
|
|
info += "\n```\n"
|
|
|
|
print(info, flush=True)
|
|
|
|
|
2023-12-28 15:56:15 +08:00
|
|
|
except Exception as e:
|
2024-01-04 22:45:07 +08:00
|
|
|
raise e
|
2023-12-28 15:56:15 +08:00
|
|
|
|
|
|
|
|
2023-12-18 20:46:16 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|