File size: 1,406 Bytes
1afb40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from unittest.mock import patch, mock_open
import pytest

from scripts.agent import (
    run_bash_command,
    run_file_read,
    run_repo_ast_parser,
    process_query,
)


def test_run_bash_command_exception():
    with patch("subprocess.run", side_effect=Exception("Subprocess failure")):
        result = run_bash_command("echo test")
        assert "Error executing bash command: Subprocess failure" in result


def test_run_file_read_exception():
    with patch("os.path.exists", return_value=True), \
         patch("builtins.open", side_effect=Exception("File access denied")):
        result = run_file_read("some_file.py")
        assert "Error reading file: File access denied" in result


def test_run_repo_ast_parser_imports_and_classes():
    code_content = """
from module_a import something
class SampleClass:
    pass
"""
    with patch("os.path.exists", return_value=True), \
         patch("builtins.open", mock_open(read_data=code_content)):
        result = run_repo_ast_parser("dummy.py")
        assert "module_a" in result
        assert "SampleClass" in result


def test_process_query_scripts_branch():
    with patch("scripts.agent.run_pytest_suite", return_value="OK") as mock_suite:
        res = process_query("run pytest coverage scripts")
        mock_suite.assert_called_once()
        _, kwargs = mock_suite.call_args
        assert kwargs.get("cov_module") == "scripts"