Codium-ai / cover-agent

QodoAI Cover-Agent: An AI-Powered Tool for Automated Test Generation and Code Coverage Enhancement! ๐Ÿ’ป๐Ÿค–๐Ÿงช๐Ÿž
https://qodo.ai/
GNU Affero General Public License v3.0
4.37k stars 327 forks source link

Auto scan entire repo ! #202

Closed mrT23 closed 2 weeks ago

mrT23 commented 2 weeks ago

PR Type

enhancement, documentation


Description


Changes walkthrough ๐Ÿ“

Relevant files
Error handling
1 files
AICaller.py
Add error handling for W&B logging                                             

cover_agent/AICaller.py - Added error handling for logging to W&B.
+14/-11 
Enhancement
10 files
CoverAgent.py
Validate project root and update initialization                   

cover_agent/CoverAgent.py
  • Added validation for project root existence.
  • Updated UnitTestGenerator initialization with project_root.
  • +8/-0     
    PromptBuilder.py
    Add project root and use relative paths                                   

    cover_agent/PromptBuilder.py
  • Added project_root parameter.
  • Updated file path handling to use relative paths.
  • +10/-6   
    UnitTestGenerator.py
    Include project root in initialization                                     

    cover_agent/UnitTestGenerator.py - Added `project_root` parameter to initialization.
    +3/-0     
    logic.py
    Simplify context function parameters                                         

    cover_agent/lsp_logic/logic.py
  • Removed target_file parameter from context functions.
  • Constructed target_file within functions.
  • +5/-3     
    main.py
    Update context function calls                                                       

    cover_agent/lsp_logic/scripts/main.py - Removed `target_file` parameter from context function calls.
    +2/-4     
    utils_context.py
    Add context analysis utilities                                                     

    cover_agent/lsp_logic/utils/utils_context.py
  • Added new utility functions for context analysis.
  • Implemented functions to analyze test files against context.
  • +96/-0   
    main.py
    Add project root argument to CLI                                                 

    cover_agent/main.py - Added `project-root` argument to CLI.
    +3/-0     
    main_full_repo.py
    Implement full repo scan and analysis script                         

    cover_agent/main_full_repo.py - Implemented script to scan entire repo and analyze test files.
    +49/-0   
    utils.py
    Add utilities for full repo analysis                                         

    cover_agent/utils.py - Added functions to parse full repo arguments and find test files.
    +139/-0 
    test_generation_prompt.toml
    Enhance test generation prompt                                                     

    cover_agent/settings/test_generation_prompt.toml - Updated test generation prompt with additional instructions.
    +5/-2     
    Configuration changes
    2 files
    config_loader.py
    Update settings for test analysis                                               

    cover_agent/settings/config_loader.py - Added new settings file for test analysis.
    +1/-0     
    analyze_test_against_context.toml
    Add test analysis configuration                                                   

    cover_agent/settings/analyze_test_against_context.toml - Added configuration for analyzing test against context.
    +45/-0   
    Documentation
    1 files
    README.md
    Document new full repo scan mode                                                 

    README.md
  • Documented new mode for scanning entire repo.
  • Updated usage examples with project-root.
  • +26/-0   

    ๐Ÿ’ก PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    codiumai-pr-agent-pro[bot] commented 2 weeks ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    โฑ๏ธ Estimated effort to review: 3 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšชโšช
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Error Handling
    The new error handling for logging to W&B might suppress important errors. Consider logging the error or raising a custom exception instead of just printing it. Input Validation
    The new validation for project root existence assumes that `args.project_root` is always set. There should be a check to ensure it's not None before validating. Path Handling
    The use of `os.path.relpath` assumes that the source and test files are within the project root. This might cause issues if the files are outside the project root. Error Handling
    The main loop for analyzing test files doesn't have any error handling. Consider adding try-except blocks to handle potential errors during file analysis and CoverAgent execution.
    codiumai-pr-agent-pro[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add error handling and logging to the main processing loop for better robustness and debugging ___ **Consider adding error handling and logging for the main loop that processes test
    files to ensure robustness and easier debugging.** [cover_agent/main_full_repo.py [28-45]](https://github.com/Codium-ai/cover-agent/pull/202/files#diff-d3343a49fd7e2fdb3276857162c04eb4b8e6a85ff901fdb1150a3d96398b9869R28-R45) ```diff # main loop for analyzing test files for test_file in test_files: - # Find the context files for the test file - context_files = await find_test_file_context(args, lsp, test_file) - print(f"Context files for test file '{test_file}':\n{''.join([f'{f}\n' for f in context_files])}") + try: + # Find the context files for the test file + context_files = await find_test_file_context(args, lsp, test_file) + logging.info(f"Context files for test file '{test_file}':\n{''.join([f'{f}\n' for f in context_files])}") - # Analyze the test file against the context files - print("\nAnalyzing test file against context files...") - source_file, context_files_include = await analyze_context(test_file, context_files, args, ai_caller) - print(f"Main source file:\n'{source_file}'") + # Analyze the test file against the context files + logging.info("\nAnalyzing test file against context files...") + source_file, context_files_include = await analyze_context(test_file, context_files, args, ai_caller) + logging.info(f"Main source file:\n'{source_file}'") - if source_file: - # Run the CoverAgent for the test file - args_copy = copy.deepcopy(args) - args_copy.source_file_path = source_file - args_copy.test_file_path = test_file - args_copy.included_files = context_files_include - agent = CoverAgent(args_copy) - agent.run() + if source_file: + # Run the CoverAgent for the test file + args_copy = copy.deepcopy(args) + args_copy.source_file_path = source_file + args_copy.test_file_path = test_file + args_copy.included_files = context_files_include + agent = CoverAgent(args_copy) + agent.run() + except Exception as e: + logging.error(f"Error processing test file {test_file}: {str(e)}") + continue ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: Adding error handling and logging to the main processing loop significantly improves the robustness of the code and makes debugging easier, which is crucial for maintainability and reliability.
    9
    Performance
    Use a generator-based approach for finding test files to improve memory efficiency ___ **Consider using a more efficient approach for finding test files, such as using
    os.walk() with yield to create a generator, which can be more memory-efficient for
    large directories.** [cover_agent/utils.py [263-279]](https://github.com/Codium-ai/cover-agent/pull/202/files#diff-4b68afa4ecc709b261a42ca40bfe986f4d150bf47184bdecdadc4954d434dcb0R263-R279) ```diff -for root, dirs, files in os.walk(project_dir): - # Check if the current directory is a 'test' directory - if not is_forbidden_directory(root.__add__(os.sep), language): - if 'test' in root.split(os.sep): - for file in files: - if filename_to_lang(file) == language: - test_files.append(os.path.join(root, file)) - else: - # Check if any file contains 'test' in its name - for file in files: - if 'test' in file: - if filename_to_lang(file) == language: - test_files.append(os.path.join(root, file)) - if len(test_files) >= MAX_TEST_FILES and args.look_for_oldest_unchanged_test_file: - print(f"Found {len(test_files)} test files. Stopping at {MAX_TEST_FILES} test files.") - break +def find_test_files_generator(project_dir, language, MAX_TEST_FILES): + for root, _, files in os.walk(project_dir): + if is_forbidden_directory(os.path.join(root, ''), language): + continue + is_test_dir = 'test' in root.split(os.sep) + for file in files: + if (is_test_dir or 'test' in file) and filename_to_lang(file) == language: + yield os.path.join(root, file) +test_files = list(itertools.islice(find_test_files_generator(project_dir, language, MAX_TEST_FILES), MAX_TEST_FILES)) +if args.look_for_oldest_unchanged_test_file: + print(f"Found {len(test_files)} test files. Stopping at {MAX_TEST_FILES} test files.") + ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: The generator-based approach can significantly improve memory efficiency when dealing with large directories, which is important for scalability and performance.
    8
    Best practice
    Use context managers for file operations to ensure proper resource handling ___ **Consider using a context manager for file operations to ensure proper resource
    handling.** [cover_agent/lsp_logic/utils/utils_context.py [31]](https://github.com/Codium-ai/cover-agent/pull/202/files#diff-ba4366e47e15c971d6f61b63ab27958c539da2da9fc6fdfa2e3630ad84bfd8b6R31-R31) ```diff -test_file_content": open(test_file, 'r').read(), +with open(test_file, 'r') as f: + test_file_content = f.read() ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Using a context manager for file operations ensures proper resource handling and file closure. This is a best practice that can prevent resource leaks and potential issues.
    7
    Maintainability
    โœ… Use more descriptive variable names to enhance code readability ___ **Consider using a more descriptive variable name instead of 'd_path' to improve code
    readability.** [cover_agent/lsp_logic/logic.py [30-33]](https://github.com/Codium-ai/cover-agent/pull/202/files#diff-f3a8c8f823b3661f9d22607af370de218c1e224a9e0502d73244652cb7ed1604R30-R33) ```diff -if d_path != target_file: - if project_dir not in d_path: +if definition_path != target_file: + if project_dir not in definition_path: continue - if not is_forbidden_directory(d_path, language): + if not is_forbidden_directory(definition_path, language): ``` `[Suggestion has been applied]`
    Suggestion importance[1-10]: 5 Why: Renaming 'd_path' to 'definition_path' improves code readability by making the variable's purpose clearer. This is a minor improvement that enhances maintainability.
    5

    ๐Ÿ’ก Need additional feedback ? start a PR chat

    codiumai-pr-agent-pro[bot] commented 2 weeks ago

    CI Failure Feedback ๐Ÿง

    (Checks updated until commit https://github.com/Codium-ai/cover-agent/commit/46dbd3902ae2526808ac5b0c668e7d0b89ef07c8)

    **Action:** build (macos-latest)
    **Failed stage:** [Test Executable (Unix)](https://github.com/Codium-ai/cover-agent/actions/runs/11688477030/job/32549070894) [โŒ]
    **Failure summary:** The action failed because of a ModuleNotFoundError indicating that the module tree_sitter could not
    be found. This error occurred during the execution of the script 'main' when attempting to import
    the tree_sitter_languages package.
    Relevant error logs: ```yaml 1: ##[group]Operating System 2: macOS ... 738: pythonLocation: /Users/runner/hostedtoolcache/Python/3.12.7/arm64 739: ##[endgroup] 740: poetry run pyinstaller \ 741: --add-data "cover_agent/version.txt:." \ 742: --add-data "cover_agent/settings/language_extensions.toml:." \ 743: --add-data "cover_agent/settings/test_generation_prompt.toml:." \ 744: --add-data "cover_agent/settings/analyze_suite_test_headers_indentation.toml:." \ 745: --add-data "cover_agent/settings/analyze_suite_test_insert_line.toml:." \ 746: --add-data "cover_agent/settings/analyze_test_run_failure.toml:." \ ... 907: from grep_ast import filename_to_lang 908: File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module 909: File "grep_ast/__init__.py", line 3, in 910: File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module 911: File "grep_ast/grep_ast.py", line 5, in 912: File "PyInstaller/loader/pyimod02_importers.py", line 384, in exec_module 913: File "tree_sitter_languages/__init__.py", line 4, in 914: File "tree_sitter_languages/core.pyx", line 4, in init tree_sitter_languages.core 915: ModuleNotFoundError: No module named 'tree_sitter' 916: [PYI-2323:ERROR] Failed to execute script 'main' due to unhandled exception! 917: ##[error]Process completed with exit code 1. ```

    โœจ CI feedback usage guide:
    The CI feedback tool (`/checks)` automatically triggers when a PR has a failed check. The tool analyzes the failed checks and provides several feedbacks: - Failed stage - Failed test name - Failure summary - Relevant error logs In addition to being automatically triggered, the tool can also be invoked manually by commenting on a PR: ``` /checks "https://github.com/{repo_name}/actions/runs/{run_number}/job/{job_number}" ``` where `{repo_name}` is the name of the repository, `{run_number}` is the run number of the failed check, and `{job_number}` is the job number of the failed check. #### Configuration options - `enable_auto_checks_feedback` - if set to true, the tool will automatically provide feedback when a check is failed. Default is true. - `excluded_checks_list` - a list of checks to exclude from the feedback, for example: ["check1", "check2"]. Default is an empty list. - `enable_help_text` - if set to true, the tool will provide a help message with the feedback. Default is true. - `persistent_comment` - if set to true, the tool will overwrite a previous checks comment with the new feedback. Default is true. - `final_update_message` - if `persistent_comment` is true and updating a previous checks message, the tool will also create a new message: "Persistent checks updated to latest commit". Default is true. See more information about the `checks` tool in the [docs](https://pr-agent-docs.codium.ai/tools/ci_feedback/).
    EmbeddedDevops1 commented 2 weeks ago

    @mrT23 I fixed the PyInstaller issue that you were seeing in CI. Sometimes just including a package within the pyproject.toml isn't good enough (not sure why).

    I had to add a --hidden-import=tree_sitter option in the build script (see Makefile) and also add that package to the build stage setup in CI (see .github/workflows/ci_pipeline.yml). Note that this was also necessary for wandb.

    mrT23 commented 2 weeks ago

    @EmbeddedDevops1 great. i will implement this from now on

    image