workflows/gen_ut/main.py

134 lines
3.8 KiB
Python
Raw Normal View History

2023-12-18 20:46:16 +08:00
from typing import Optional, List, Dict
import os
import sys
import click
from propose_test import propose_test
from find_reference_tests import find_reference_tests
from write_tests import write_and_print_tests
from chat.ask_codebase.tools.retrieve_file_content import retrieve_file_content
2023-12-24 16:58:09 +08:00
from i18n import TUILanguage, get_translation
2023-12-18 20:46:16 +08:00
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs"))
# sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "libs"))
from ui_utils import ui_checkbox_select, ui_text_edit, CheckboxOption
from pprint import pprint
import time
2023-12-18 20:46:16 +08:00
def _get_relevant_content(
repo_root: str,
file_path: str,
function_name: str,
start_line: Optional[int] = None, # 0-based, inclusive
end_line: Optional[int] = None, # 0-based, inclusive
) -> str:
"""
Get the relevant content for a function.
it can be the whole file, or the specified lines of the file.
"""
file_content = retrieve_file_content(file_path, repo_root)
func_content = file_content
if start_line is not None and end_line is not None:
lines = file_content.split("\n")
func_content = "\n".join(lines[start_line : end_line + 1])
return func_content
2023-12-18 20:46:16 +08:00
@click.command()
@click.argument("user_prompt", required=True)
@click.option("-fn", "--func_name", required=True, type=str)
2023-12-18 20:46:16 +08:00
@click.option("-fp", "--file_path", required=True, type=str)
@click.option("-sln", "--start_line", required=False, type=int)
@click.option("-eln", "--end_line", required=False, type=int)
def main(
user_prompt: str,
func_name: str,
2023-12-18 20:46:16 +08:00
file_path: str,
start_line: Optional[int], # 0-based, inclusive
end_line: Optional[int], # 0-based, inclusive
2023-12-18 20:46:16 +08:00
):
repo_root = os.getcwd()
2023-12-24 16:58:09 +08:00
tui_lang = TUILanguage.from_str("zh")
_i = get_translation(tui_lang)
2023-12-18 20:46:16 +08:00
print("\n\n$$$$$$$$$$$\n\n")
print(f"repo_root: {repo_root}\n\n")
print(f"user_prompt: {user_prompt}\n\n")
print(f"func_name: {func_name}\n\n")
2023-12-18 20:46:16 +08:00
print(f"file_path: {file_path}\n\n")
print(f"start_line: {start_line}\n\n")
print(f"end_line: {end_line}\n\n")
2023-12-24 18:23:50 +08:00
print(f"tui_lang: {tui_lang}, {tui_lang.language_code}, { tui_lang.chat_language}\n\n")
2023-12-24 16:58:09 +08:00
print(_i("hello"))
2023-12-18 20:46:16 +08:00
print("\n\n$$$$$$$$$$$\n\n", flush=True)
print(
2023-12-24 18:23:50 +08:00
_i("\n\n```Step\n# Analyzing the function and current unit tests...\n"),
2023-12-18 20:46:16 +08:00
flush=True,
)
relevant_content = _get_relevant_content(
repo_root=repo_root,
file_path=file_path,
function_name=func_name,
start_line=start_line,
end_line=end_line,
)
2023-12-18 20:46:16 +08:00
test_cases = propose_test(
repo_root=repo_root,
user_prompt=user_prompt,
function_name=func_name,
function_content=relevant_content,
2023-12-18 20:46:16 +08:00
file_path=file_path,
chat_language=tui_lang.chat_language,
2023-12-18 20:46:16 +08:00
)
ref_files = find_reference_tests(repo_root, func_name, file_path)
2023-12-24 18:23:50 +08:00
print(_i("Complete analyzing.\n```"), flush=True)
2023-12-18 20:46:16 +08:00
case_id_to_option: Dict[str, CheckboxOption] = {
f"case_{i}": CheckboxOption(
2023-12-24 18:23:50 +08:00
id=f"case_{i}", text=desc, group=_i("proposed cases"), checked=False
2023-12-18 20:46:16 +08:00
)
for i, desc in enumerate(test_cases)
}
selected_ids = ui_checkbox_select(
2023-12-24 18:23:50 +08:00
_i("Select test cases to generate"), list(case_id_to_option.values())
2023-12-18 20:46:16 +08:00
)
2023-12-24 18:23:50 +08:00
time.sleep(3)
2023-12-18 20:46:16 +08:00
selected_cases = [case_id_to_option[id]._text for id in selected_ids]
ref_file = ref_files[0] if ref_files else ""
2023-12-24 18:23:50 +08:00
new_ref_file = ui_text_edit(_i("Edit reference test file"), ref_file)
2023-12-18 20:46:16 +08:00
2023-12-24 18:23:50 +08:00
time.sleep(3)
2023-12-18 20:46:16 +08:00
write_and_print_tests(
2023-12-18 20:46:16 +08:00
root_path=repo_root,
function_name=func_name,
function_content=relevant_content,
2023-12-18 20:46:16 +08:00
file_path=file_path,
test_cases=selected_cases,
reference_files=[new_ref_file] if new_ref_file else None,
stream=True,
2023-12-18 20:46:16 +08:00
)
if __name__ == "__main__":
main()