|
| 1 | +import os |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +from jinja2 import Environment, StrictUndefined |
| 5 | + |
| 6 | +from cover_agent.lsp_logic.file_map.file_map import FileMap |
| 7 | +from cover_agent.lsp_logic.logic import get_direct_context |
| 8 | +from cover_agent.lsp_logic.multilspy import LanguageServer |
| 9 | +from cover_agent.lsp_logic.multilspy.multilspy_config import MultilspyConfig |
| 10 | +from cover_agent.lsp_logic.multilspy.multilspy_logger import MultilspyLogger |
| 11 | + |
| 12 | +from cover_agent.settings.config_loader import get_settings |
| 13 | +from cover_agent.utils import load_yaml |
| 14 | + |
| 15 | + |
| 16 | +async def analyze_context(test_file, context_files, args, ai_caller): |
| 17 | + """ |
| 18 | + # we now want to analyze the test file against the source files and determine several things: |
| 19 | + # 1. If this test file is a unit test file |
| 20 | + # 2. Which of the context files can be seen as the main source file for this test file, for which we want to increase coverage |
| 21 | + # 3. Set all other context files as additional 'included_files' for the CoverAgent |
| 22 | + """ |
| 23 | + source_file = None |
| 24 | + context_files_include = context_files |
| 25 | + try: |
| 26 | + test_file_rel_str = os.path.relpath(test_file, args.project_root) |
| 27 | + context_files_rel_filtered_list_str = "" |
| 28 | + for file in context_files: |
| 29 | + context_files_rel_filtered_list_str += f"`{os.path.relpath(file, args.project_root)}\n`" |
| 30 | + variables = {"language": args.project_language, |
| 31 | + "test_file_name_rel": test_file_rel_str, |
| 32 | + "test_file_content": open(test_file, 'r').read(), |
| 33 | + "context_files_names_rel": context_files_rel_filtered_list_str |
| 34 | + } |
| 35 | + file = 'analyze_test_against_context' |
| 36 | + environment = Environment(undefined=StrictUndefined) |
| 37 | + settings = get_settings().get(file) |
| 38 | + system_prompt = environment.from_string(settings.system).render(variables) |
| 39 | + user_prompt = environment.from_string(settings.user).render(variables) |
| 40 | + response, prompt_token_count, response_token_count = ( |
| 41 | + ai_caller.call_model(prompt={"system": system_prompt, "user": user_prompt}) |
| 42 | + ) |
| 43 | + response_dict = load_yaml(response) |
| 44 | + if int(response_dict.get('is_this_a_unit_test', 0)) == 1: |
| 45 | + source_file_rel = response_dict.get('main_file', "").strip().strip('`') |
| 46 | + source_file = os.path.join(args.project_root, source_file_rel) |
| 47 | + for file in context_files: |
| 48 | + file_rel = os.path.relpath(file, args.project_root) |
| 49 | + if file_rel == source_file_rel: |
| 50 | + context_files_include = [f for f in context_files if f != file] |
| 51 | + |
| 52 | + if source_file: |
| 53 | + print(f"Test file: `{test_file}` is a unit test file for source file: `{source_file}`") |
| 54 | + else: |
| 55 | + print(f"Test file: `{test_file}` is not a unit test file") |
| 56 | + except Exception as e: |
| 57 | + print(f"Error while analyzing test file {test_file} against context files: {e}") |
| 58 | + |
| 59 | + return source_file, context_files_include |
| 60 | + |
| 61 | + |
| 62 | +async def find_test_file_context(args, lsp, test_file): |
| 63 | + try: |
| 64 | + target_file = test_file |
| 65 | + rel_file = os.path.relpath(target_file, args.project_root) |
| 66 | + |
| 67 | + # get tree-sitter query results |
| 68 | + # print("\nGetting tree-sitter query results for the target file...") |
| 69 | + fname_summary = FileMap(target_file, parent_context=False, child_context=False, |
| 70 | + header_max=0, project_base_path=args.project_root) |
| 71 | + query_results, captures = fname_summary.get_query_results() |
| 72 | + # print("Tree-sitter query results for the target file done.") |
| 73 | + |
| 74 | + # print("\nGetting context ...") |
| 75 | + context_files, context_symbols = await get_direct_context(captures, |
| 76 | + args.project_language, |
| 77 | + lsp, |
| 78 | + args.project_root, |
| 79 | + rel_file) |
| 80 | + # filter empty files |
| 81 | + context_files_filtered = [] |
| 82 | + for file in context_files: |
| 83 | + with open(file, 'r') as f: |
| 84 | + if f.read().strip(): |
| 85 | + context_files_filtered.append(file) |
| 86 | + context_files = context_files_filtered |
| 87 | + # print("Getting context done.") |
| 88 | + except Exception as e: |
| 89 | + print(f"Error while getting context for test file {test_file}: {e}") |
| 90 | + context_files = [] |
| 91 | + |
| 92 | + return context_files |
| 93 | + |
| 94 | + |
| 95 | +async def initialize_language_server(args): |
| 96 | + logger = MultilspyLogger() |
| 97 | + config = MultilspyConfig.from_dict({"code_language": args.project_language}) |
| 98 | + lsp = LanguageServer.create(config, logger, args.project_root) |
| 99 | + sleep(0.1) |
| 100 | + return lsp |
0 commit comments