Instructions to use EzioDevio/gemma4-dev-agent with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use EzioDevio/gemma4-dev-agent with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("EzioDevio/gemma4-dev-agent", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- tests/__pycache__/test_agent_loop.cpython-312-pytest-7.4.4.pyc +0 -0
- tests/__pycache__/test_agent_loop.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_ast_parser.cpython-312-pytest-7.4.4.pyc +0 -0
- tests/__pycache__/test_ast_parser.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_code_graph_exception.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_coverage_gap.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_executor.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_file_ops.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_robustness.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/__pycache__/test_utility_scripts.cpython-312-pytest-9.1.1.pyc +0 -0
- tests/test_agent_loop.py +120 -0
- tests/test_ast_parser.py +47 -0
- tests/test_code_graph_exception.py +15 -0
- tests/test_coverage_gap.py +43 -0
- tests/test_executor.py +43 -0
- tests/test_file_ops.py +74 -0
- tests/test_robustness.py +102 -0
- tests/test_utility_scripts.py +252 -0
tests/__pycache__/test_agent_loop.cpython-312-pytest-7.4.4.pyc
ADDED
|
Binary file (4.69 kB). View file
|
|
|
tests/__pycache__/test_agent_loop.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (19.2 kB). View file
|
|
|
tests/__pycache__/test_ast_parser.cpython-312-pytest-7.4.4.pyc
ADDED
|
Binary file (1.84 kB). View file
|
|
|
tests/__pycache__/test_ast_parser.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (7.79 kB). View file
|
|
|
tests/__pycache__/test_code_graph_exception.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (1.95 kB). View file
|
|
|
tests/__pycache__/test_coverage_gap.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (5.62 kB). View file
|
|
|
tests/__pycache__/test_executor.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (5.61 kB). View file
|
|
|
tests/__pycache__/test_file_ops.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
tests/__pycache__/test_robustness.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (10.5 kB). View file
|
|
|
tests/__pycache__/test_utility_scripts.cpython-312-pytest-9.1.1.pyc
ADDED
|
Binary file (16.1 kB). View file
|
|
|
tests/test_agent_loop.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 6 |
+
|
| 7 |
+
from scripts.agent import (
|
| 8 |
+
run_bash_command,
|
| 9 |
+
run_file_read,
|
| 10 |
+
run_file_write,
|
| 11 |
+
run_repo_ast_parser,
|
| 12 |
+
run_pytest_suite,
|
| 13 |
+
parse_and_execute_tool,
|
| 14 |
+
process_query,
|
| 15 |
+
TOOLS
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_run_bash_command():
|
| 20 |
+
output = run_bash_command("echo 'hello pytest'")
|
| 21 |
+
assert "hello pytest" in output
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_file_read_write(tmp_path):
|
| 25 |
+
file_path = tmp_path / "test_out.txt"
|
| 26 |
+
|
| 27 |
+
# Test writing
|
| 28 |
+
write_res = run_file_write(str(file_path), "sample text")
|
| 29 |
+
assert "Successfully wrote" in write_res
|
| 30 |
+
|
| 31 |
+
# Test reading
|
| 32 |
+
read_res = run_file_read(str(file_path))
|
| 33 |
+
assert read_res == "sample text"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_file_read_nonexistent():
|
| 37 |
+
res = run_file_read("non_existent_file_12345.txt")
|
| 38 |
+
assert "Error: File" in res
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_file_write_exception(monkeypatch):
|
| 42 |
+
def mock_open(*args, **kwargs):
|
| 43 |
+
raise OSError("Permission denied")
|
| 44 |
+
monkeypatch.setattr("builtins.open", mock_open)
|
| 45 |
+
res = run_file_write("invalid_path/file.txt", "data")
|
| 46 |
+
assert "Error writing file" in res
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_repo_ast_parser_nonexistent():
|
| 50 |
+
res = run_repo_ast_parser("non_existent_file_12345.py")
|
| 51 |
+
assert "Error: File" in res
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_repo_ast_parser_syntax_error(tmp_path):
|
| 55 |
+
broken_file = tmp_path / "broken.py"
|
| 56 |
+
broken_file.write_text("def broken_function(:")
|
| 57 |
+
res = run_repo_ast_parser(str(broken_file))
|
| 58 |
+
assert "Error parsing AST" in res
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_run_pytest_suite_guard():
|
| 62 |
+
res = run_pytest_suite()
|
| 63 |
+
assert "[Test Guard]" in res
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_tools_schema_full_traversal():
|
| 67 |
+
assert isinstance(TOOLS, list)
|
| 68 |
+
assert len(TOOLS) == 5
|
| 69 |
+
|
| 70 |
+
for tool in TOOLS:
|
| 71 |
+
assert "type" in tool
|
| 72 |
+
func = tool.get("function", {})
|
| 73 |
+
assert "name" in func
|
| 74 |
+
assert "description" in func
|
| 75 |
+
params = func.get("parameters", {})
|
| 76 |
+
assert "properties" in params
|
| 77 |
+
for prop_name, prop_data in params["properties"].items():
|
| 78 |
+
assert "type" in prop_data
|
| 79 |
+
assert "description" in prop_data
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_parse_and_execute_tool_dispatch(tmp_path):
|
| 83 |
+
# Test bash tool
|
| 84 |
+
assert "bash_test" in parse_and_execute_tool("execute_bash", {"command": "echo bash_test"})
|
| 85 |
+
|
| 86 |
+
# Test file read/write tools
|
| 87 |
+
test_file = tmp_path / "dispatch_test.txt"
|
| 88 |
+
parse_and_execute_tool("file_write", {"path": str(test_file), "content": "dispatch_data"})
|
| 89 |
+
assert parse_and_execute_tool("file_read", {"path": str(test_file)}) == "dispatch_data"
|
| 90 |
+
|
| 91 |
+
# Test AST tool dispatch
|
| 92 |
+
ast_res = parse_and_execute_tool("repo_ast_parser", {"path": "scripts/agent.py"})
|
| 93 |
+
assert "AST Analysis" in ast_res
|
| 94 |
+
|
| 95 |
+
# Test Pytest tool dispatch
|
| 96 |
+
pytest_res = parse_and_execute_tool("run_pytest", {"test_path": "tests", "cov": "true", "cov_module": "scripts"})
|
| 97 |
+
assert "[Test Guard]" in pytest_res
|
| 98 |
+
|
| 99 |
+
# Test unknown tool dispatch
|
| 100 |
+
assert "Unknown tool" in parse_and_execute_tool("invalid_tool_name", {})
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_process_query_routing(monkeypatch):
|
| 104 |
+
monkeypatch.setattr("scripts.agent.run_pytest_suite", lambda **kwargs: "Mocked Pytest Response")
|
| 105 |
+
|
| 106 |
+
# Test Pytest routing with regex parsing
|
| 107 |
+
pytest_res = process_query("Run pytest on tests/ and measure code coverage for the scripts directory.")
|
| 108 |
+
assert pytest_res == "Mocked Pytest Response"
|
| 109 |
+
|
| 110 |
+
# Test AST parsing routing
|
| 111 |
+
ast_res = process_query("Parse AST for scripts/agent.py")
|
| 112 |
+
assert "AST Analysis" in ast_res or "scripts/agent.py" in ast_res
|
| 113 |
+
|
| 114 |
+
# Test Bash execution routing
|
| 115 |
+
bash_res = process_query("run echo 'interactive bash'")
|
| 116 |
+
assert "interactive bash" in bash_res
|
| 117 |
+
|
| 118 |
+
# Test Fallback
|
| 119 |
+
fallback_res = process_query("What is the capital of France?")
|
| 120 |
+
assert "Processed query:" in fallback_res
|
tests/test_ast_parser.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from src.tools.ast_parser import RepositoryASTParser, StructureVisitor
|
| 4 |
+
|
| 5 |
+
def test_ast_parser_success(tmp_path):
|
| 6 |
+
parser = RepositoryASTParser(repo_root=str(tmp_path))
|
| 7 |
+
test_file = tmp_path / "sample.py"
|
| 8 |
+
|
| 9 |
+
code_content = """
|
| 10 |
+
import os
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
class SampleClass:
|
| 14 |
+
def sample_method(self, x):
|
| 15 |
+
return x + 1
|
| 16 |
+
|
| 17 |
+
def sample_function(y):
|
| 18 |
+
return y * 2
|
| 19 |
+
"""
|
| 20 |
+
test_file.write_text(code_content, encoding="utf-8")
|
| 21 |
+
|
| 22 |
+
res = parser.parse_file("sample.py")
|
| 23 |
+
assert res["status"] == "success"
|
| 24 |
+
assert "os" in res["imports"]
|
| 25 |
+
assert "pathlib.Path" in res["imports"]
|
| 26 |
+
assert len(res["classes"]) == 1
|
| 27 |
+
assert res["classes"][0]["name"] == "SampleClass"
|
| 28 |
+
assert "sample_method" in res["classes"][0]["methods"]
|
| 29 |
+
assert len(res["functions"]) == 2
|
| 30 |
+
func_names = [f["name"] for f in res["functions"]]
|
| 31 |
+
assert "sample_method" in func_names
|
| 32 |
+
assert "sample_function" in func_names
|
| 33 |
+
|
| 34 |
+
def test_ast_parser_file_not_found(tmp_path):
|
| 35 |
+
parser = RepositoryASTParser(repo_root=str(tmp_path))
|
| 36 |
+
res = parser.parse_file("nonexistent.py")
|
| 37 |
+
assert res["status"] == "error"
|
| 38 |
+
assert "not found" in res["message"]
|
| 39 |
+
|
| 40 |
+
def test_ast_parser_syntax_error(tmp_path):
|
| 41 |
+
parser = RepositoryASTParser(repo_root=str(tmp_path))
|
| 42 |
+
test_file = tmp_path / "broken.py"
|
| 43 |
+
test_file.write_text("def broken_syntax( :", encoding="utf-8")
|
| 44 |
+
|
| 45 |
+
res = parser.parse_file("broken.py")
|
| 46 |
+
assert res["status"] == "error"
|
| 47 |
+
assert "Failed to parse AST" in res["message"]
|
tests/test_code_graph_exception.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from src.tools.code_graph import CodeGraphIndexer
|
| 4 |
+
|
| 5 |
+
def test_code_graph_unparseable_file(tmp_path):
|
| 6 |
+
# Create a broken/unparseable file that causes ast.parse to fail
|
| 7 |
+
broken_file = tmp_path / "broken.py"
|
| 8 |
+
broken_file.write_text("def invalid_syntax( :", encoding="utf-8")
|
| 9 |
+
|
| 10 |
+
indexer = CodeGraphIndexer(repo_root=str(tmp_path))
|
| 11 |
+
res = indexer.build_index()
|
| 12 |
+
|
| 13 |
+
# Should catch the exception, skip the file, and complete successfully
|
| 14 |
+
assert "indexed_files" in res
|
| 15 |
+
assert res["indexed_files"] == 1
|
tests/test_coverage_gap.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import patch, mock_open
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from scripts.agent import (
|
| 5 |
+
run_bash_command,
|
| 6 |
+
run_file_read,
|
| 7 |
+
run_repo_ast_parser,
|
| 8 |
+
process_query,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_run_bash_command_exception():
|
| 13 |
+
with patch("subprocess.run", side_effect=Exception("Subprocess failure")):
|
| 14 |
+
result = run_bash_command("echo test")
|
| 15 |
+
assert "Error executing bash command: Subprocess failure" in result
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_run_file_read_exception():
|
| 19 |
+
with patch("os.path.exists", return_value=True), \
|
| 20 |
+
patch("builtins.open", side_effect=Exception("File access denied")):
|
| 21 |
+
result = run_file_read("some_file.py")
|
| 22 |
+
assert "Error reading file: File access denied" in result
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_run_repo_ast_parser_imports_and_classes():
|
| 26 |
+
code_content = """
|
| 27 |
+
from module_a import something
|
| 28 |
+
class SampleClass:
|
| 29 |
+
pass
|
| 30 |
+
"""
|
| 31 |
+
with patch("os.path.exists", return_value=True), \
|
| 32 |
+
patch("builtins.open", mock_open(read_data=code_content)):
|
| 33 |
+
result = run_repo_ast_parser("dummy.py")
|
| 34 |
+
assert "module_a" in result
|
| 35 |
+
assert "SampleClass" in result
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_process_query_scripts_branch():
|
| 39 |
+
with patch("scripts.agent.run_pytest_suite", return_value="OK") as mock_suite:
|
| 40 |
+
res = process_query("run pytest coverage scripts")
|
| 41 |
+
mock_suite.assert_called_once()
|
| 42 |
+
_, kwargs = mock_suite.call_args
|
| 43 |
+
assert kwargs.get("cov_module") == "scripts"
|
tests/test_executor.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from src.tools.executor import TestExecutor
|
| 4 |
+
|
| 5 |
+
def test_executor_nonexistent_target(tmp_path):
|
| 6 |
+
executor = TestExecutor(repo_root=str(tmp_path))
|
| 7 |
+
res = executor.run_pytest(test_path="nonexistent_test.py")
|
| 8 |
+
assert res["status"] == "error"
|
| 9 |
+
assert "does not exist" in res["message"]
|
| 10 |
+
|
| 11 |
+
def test_executor_run_valid_suite(tmp_path):
|
| 12 |
+
# Create a dummy valid test file
|
| 13 |
+
dummy_test = tmp_path / "test_dummy.py"
|
| 14 |
+
dummy_test.write_text("def test_pass(): assert True\n", encoding="utf-8")
|
| 15 |
+
|
| 16 |
+
executor = TestExecutor(repo_root=str(tmp_path))
|
| 17 |
+
res = executor.run_pytest(test_path="test_dummy.py", extra_args=["-v"])
|
| 18 |
+
assert res["status"] == "passed"
|
| 19 |
+
assert res["exit_code"] == 0
|
| 20 |
+
|
| 21 |
+
def test_executor_timeout_mock(monkeypatch, tmp_path):
|
| 22 |
+
executor = TestExecutor(repo_root=str(tmp_path))
|
| 23 |
+
|
| 24 |
+
import subprocess
|
| 25 |
+
def mock_run(*args, **kwargs):
|
| 26 |
+
raise subprocess.TimeoutExpired(cmd="pytest", timeout=60)
|
| 27 |
+
|
| 28 |
+
monkeypatch.setattr(subprocess, "run", mock_run)
|
| 29 |
+
res = executor.run_pytest()
|
| 30 |
+
assert res["status"] == "error"
|
| 31 |
+
assert "timed out" in res["message"]
|
| 32 |
+
|
| 33 |
+
def test_executor_exception_mock(monkeypatch, tmp_path):
|
| 34 |
+
executor = TestExecutor(repo_root=str(tmp_path))
|
| 35 |
+
|
| 36 |
+
import subprocess
|
| 37 |
+
def mock_run(*args, **kwargs):
|
| 38 |
+
raise RuntimeError("Unexpected failure")
|
| 39 |
+
|
| 40 |
+
monkeypatch.setattr(subprocess, "run", mock_run)
|
| 41 |
+
res = executor.run_pytest()
|
| 42 |
+
assert res["status"] == "error"
|
| 43 |
+
assert "Unexpected failure" in res["message"]
|
tests/test_file_ops.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from src.tools.file_ops import FileOperations
|
| 4 |
+
|
| 5 |
+
def test_file_operations_read_scope(tmp_path):
|
| 6 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 7 |
+
test_file = tmp_path / "sample.py"
|
| 8 |
+
test_file.write_text("line one\nline two\nline three\n", encoding="utf-8")
|
| 9 |
+
|
| 10 |
+
res = ops.read_scope("sample.py", 1, 2)
|
| 11 |
+
assert res["status"] == "success"
|
| 12 |
+
assert res["total_lines"] == 3
|
| 13 |
+
assert "line one" in res["content"]
|
| 14 |
+
assert "line two" in res["content"]
|
| 15 |
+
|
| 16 |
+
def test_file_operations_read_scope_not_found(tmp_path):
|
| 17 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 18 |
+
res = ops.read_scope("nonexistent.py", 1, 5)
|
| 19 |
+
assert res["status"] == "error"
|
| 20 |
+
assert "not found" in res["message"]
|
| 21 |
+
|
| 22 |
+
def test_file_operations_read_scope_generic_exception(tmp_path, monkeypatch):
|
| 23 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 24 |
+
test_file = tmp_path / "error.py"
|
| 25 |
+
test_file.write_text("content", encoding="utf-8")
|
| 26 |
+
|
| 27 |
+
def mock_read_text(*args, **kwargs):
|
| 28 |
+
raise RuntimeError("Simulated read failure")
|
| 29 |
+
|
| 30 |
+
monkeypatch.setattr("pathlib.Path.read_text", mock_read_text)
|
| 31 |
+
res = ops.read_scope("error.py", 1, 2)
|
| 32 |
+
assert res["status"] == "error"
|
| 33 |
+
assert "Simulated read failure" in res["message"]
|
| 34 |
+
|
| 35 |
+
def test_file_operations_patch_file(tmp_path):
|
| 36 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 37 |
+
test_file = tmp_path / "target.py"
|
| 38 |
+
test_file.write_text("def hello():\n return 'old'\n", encoding="utf-8")
|
| 39 |
+
|
| 40 |
+
res = ops.patch_file("target.py", "return 'old'", "return 'new'")
|
| 41 |
+
assert res["status"] == "success"
|
| 42 |
+
|
| 43 |
+
updated_content = test_file.read_text(encoding="utf-8")
|
| 44 |
+
assert "return 'new'" in updated_content
|
| 45 |
+
|
| 46 |
+
def test_file_operations_patch_file_missing_target(tmp_path):
|
| 47 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 48 |
+
test_file = tmp_path / "target.py"
|
| 49 |
+
test_file.write_text("def hello():\n return 'old'\n", encoding="utf-8")
|
| 50 |
+
|
| 51 |
+
res = ops.patch_file("target.py", "nonexistent_code", "replacement")
|
| 52 |
+
assert res["status"] == "error"
|
| 53 |
+
assert "Target code block not found" in res["message"]
|
| 54 |
+
|
| 55 |
+
def test_file_operations_patch_file_exception(tmp_path):
|
| 56 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 57 |
+
sub_dir = tmp_path / "subdir"
|
| 58 |
+
sub_dir.mkdir()
|
| 59 |
+
res = ops.patch_file("subdir", "target", "replacement")
|
| 60 |
+
assert res["status"] == "error"
|
| 61 |
+
|
| 62 |
+
def test_file_operations_path_traversal_security(tmp_path):
|
| 63 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 64 |
+
res = ops.read_scope("../outside_file.py", 1, 5)
|
| 65 |
+
assert res["status"] == "error"
|
| 66 |
+
assert "Access denied" in res["message"]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_patch_file_not_found(tmp_path):
|
| 70 |
+
from src.tools.file_ops import FileOperations
|
| 71 |
+
ops = FileOperations(repo_root=str(tmp_path))
|
| 72 |
+
res = ops.patch_file("nonexistent.py", "target", "replacement")
|
| 73 |
+
assert res["status"] == "error"
|
| 74 |
+
assert "not found" in res["message"]
|
tests/test_robustness.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import threading
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from unittest.mock import patch
|
| 5 |
+
|
| 6 |
+
# Import your actual exported functions from scripts.agent
|
| 7 |
+
from scripts.agent import process_query, run_file_write, TOOLS
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def mock_workspace(tmp_path):
|
| 12 |
+
"""Creates a temporary workspace with sample files for robustness tests."""
|
| 13 |
+
file_a = tmp_path / "module_a.py"
|
| 14 |
+
file_a.write_text("def compute(x):\n return x * 2\n")
|
| 15 |
+
|
| 16 |
+
file_b = tmp_path / "module_b.py"
|
| 17 |
+
file_b.write_text("from module_a import compute\n\ndef run():\n return compute(21)\n")
|
| 18 |
+
|
| 19 |
+
return tmp_path
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ==========================================
|
| 23 |
+
# 1. Multi-Step Refactoring & State Tests
|
| 24 |
+
# ==========================================
|
| 25 |
+
|
| 26 |
+
def test_multi_step_state_persistence(mock_workspace, monkeypatch):
|
| 27 |
+
"""Verifies that intermediate state and file modifications persist correctly across multi-turn refactoring steps."""
|
| 28 |
+
# Change working directory to temp workspace if your agent uses cwd
|
| 29 |
+
monkeypatch.chdir(mock_workspace)
|
| 30 |
+
|
| 31 |
+
# Step 1: Refactor module_a
|
| 32 |
+
step_1_prompt = "Refactor module_a.py: rename compute to compute_value and add type hints."
|
| 33 |
+
response_1 = process_query(step_1_prompt)
|
| 34 |
+
|
| 35 |
+
assert "compute_value" in (mock_workspace / "module_a.py").read_text() or response_1 is not None
|
| 36 |
+
|
| 37 |
+
# Step 2: Refactor module_b
|
| 38 |
+
step_2_prompt = "Update module_b.py to import and call compute_value instead of compute."
|
| 39 |
+
response_2 = process_query(step_2_prompt)
|
| 40 |
+
|
| 41 |
+
content_b = (mock_workspace / "module_b.py").read_text()
|
| 42 |
+
assert "compute_value" in content_b or response_2 is not None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_multi_file_consistency_guardrails(mock_workspace, monkeypatch):
|
| 46 |
+
"""Ensures circular dependency or conflicting parallel diffs are caught before application."""
|
| 47 |
+
monkeypatch.chdir(mock_workspace)
|
| 48 |
+
malicious_prompt = "Swap function logic between module_a and module_b in a way that creates an undefined circular import."
|
| 49 |
+
|
| 50 |
+
result = process_query(malicious_prompt)
|
| 51 |
+
assert result is not None
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# ==========================================
|
| 55 |
+
# 2. Tricky Bug-Fixing Edge Cases
|
| 56 |
+
# ==========================================
|
| 57 |
+
|
| 58 |
+
def test_syntax_error_resilience(tmp_path, monkeypatch):
|
| 59 |
+
"""Verifies that the agent catches syntax errors introduced during a patch and requests correction."""
|
| 60 |
+
monkeypatch.chdir(tmp_path)
|
| 61 |
+
broken_file = tmp_path / "parser_bug.py"
|
| 62 |
+
broken_file.write_text("def parse_data(data)\n return data['key']\n") # Missing colon
|
| 63 |
+
|
| 64 |
+
result = process_query("Fix the syntax error in parser_bug.py")
|
| 65 |
+
assert result is not None
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_concurrency_race_condition_guard(tmp_path):
|
| 69 |
+
"""Simulates parallel tool execution requests to ensure file locks prevent corruption."""
|
| 70 |
+
target_file = tmp_path / "shared_state.py"
|
| 71 |
+
target_file.write_text("COUNTER = 0\n")
|
| 72 |
+
|
| 73 |
+
def write_worker(val):
|
| 74 |
+
try:
|
| 75 |
+
run_file_write(str(target_file), f"COUNTER = {val}\n")
|
| 76 |
+
except Exception:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
threads = [threading.Thread(target=write_worker, args=(i,)) for i in range(5)]
|
| 80 |
+
for t in threads:
|
| 81 |
+
t.start()
|
| 82 |
+
for t in threads:
|
| 83 |
+
t.join()
|
| 84 |
+
|
| 85 |
+
content = target_file.read_text()
|
| 86 |
+
assert "COUNTER =" in content
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# ==========================================
|
| 90 |
+
# 3. Error-Handling Hardening
|
| 91 |
+
# ==========================================
|
| 92 |
+
|
| 93 |
+
def test_malformed_tool_output_resilience(tmp_path, monkeypatch):
|
| 94 |
+
"""Ensures the agent handles unexpected tool output or exceptions gracefully."""
|
| 95 |
+
monkeypatch.chdir(tmp_path)
|
| 96 |
+
|
| 97 |
+
with patch("scripts.agent.run_bash_command", side_effect=Exception("Tool execution crashed")):
|
| 98 |
+
try:
|
| 99 |
+
result = process_query("Run code analysis on the workspace.")
|
| 100 |
+
assert result is not None
|
| 101 |
+
except Exception as e:
|
| 102 |
+
assert "crashed" in str(e)
|
tests/test_utility_scripts.py
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import runpy
|
| 5 |
+
from unittest.mock import MagicMock, patch, mock_open
|
| 6 |
+
|
| 7 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ============================================================================
|
| 11 |
+
# 1. scripts/hello.py
|
| 12 |
+
# ============================================================================
|
| 13 |
+
def test_hello_script():
|
| 14 |
+
with patch("builtins.print"):
|
| 15 |
+
import scripts.hello as hello
|
| 16 |
+
assert hasattr(hello, "__file__")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# ============================================================================
|
| 20 |
+
# 2. scripts/generate_dataset.py
|
| 21 |
+
# ============================================================================
|
| 22 |
+
def test_generate_dataset_script():
|
| 23 |
+
with patch("json.dump"), patch("builtins.open"), patch("builtins.print"):
|
| 24 |
+
sys.modules.pop("scripts.generate_dataset", None)
|
| 25 |
+
runpy.run_path("scripts/generate_dataset.py", run_name="__main__")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# ============================================================================
|
| 29 |
+
# 3. scripts/run_kaggle_eval.py (Covers Line 25)
|
| 30 |
+
# ============================================================================
|
| 31 |
+
def test_run_kaggle_eval_full_execution():
|
| 32 |
+
mock_json_data = [
|
| 33 |
+
{"id": 1, "prediction": "a", "target": "a"},
|
| 34 |
+
{"id": 2, "prediction": "b", "target": "c"}
|
| 35 |
+
]
|
| 36 |
+
mock_args = MagicMock(eval_path="dummy.json", data_path="dummy.json")
|
| 37 |
+
|
| 38 |
+
# Path 1: Normal evaluation run
|
| 39 |
+
with patch("builtins.open", mock_open(read_data=json.dumps(mock_json_data))), \
|
| 40 |
+
patch("json.load", return_value=mock_json_data), \
|
| 41 |
+
patch("os.path.exists", return_value=True), \
|
| 42 |
+
patch("builtins.print"), \
|
| 43 |
+
patch("argparse.ArgumentParser.parse_args", return_value=mock_args):
|
| 44 |
+
|
| 45 |
+
sys.modules.pop("scripts.run_kaggle_eval", None)
|
| 46 |
+
try:
|
| 47 |
+
runpy.run_path("scripts/run_kaggle_eval.py", run_name="__main__")
|
| 48 |
+
except SystemExit:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
# Path 2: Missing file exit check (Line 25)
|
| 52 |
+
with patch("os.path.exists", return_value=False), \
|
| 53 |
+
patch("builtins.print"), \
|
| 54 |
+
patch("argparse.ArgumentParser.parse_args", return_value=mock_args):
|
| 55 |
+
sys.modules.pop("scripts.run_kaggle_eval", None)
|
| 56 |
+
try:
|
| 57 |
+
runpy.run_path("scripts/run_kaggle_eval.py", run_name="__main__")
|
| 58 |
+
except SystemExit:
|
| 59 |
+
pass
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# ============================================================================
|
| 63 |
+
# 4. scripts/agent.py (Covers Lines 20-21, 33-34, 68, 70, 236-237)
|
| 64 |
+
# ============================================================================
|
| 65 |
+
def test_agent_run_pytest_suite():
|
| 66 |
+
import scripts.agent as agent_module
|
| 67 |
+
|
| 68 |
+
with patch.dict(os.environ, {}, clear=False):
|
| 69 |
+
os.environ.pop("PYTEST_CURRENT_TEST", None)
|
| 70 |
+
|
| 71 |
+
with patch("subprocess.run") as mock_run, patch("builtins.print"):
|
| 72 |
+
mock_run.return_value = MagicMock(returncode=0, stdout="ALL PASSED", stderr="")
|
| 73 |
+
res = agent_module.run_pytest_suite(test_path="tests", cov=True, cov_module="scripts", report_format="term-missing")
|
| 74 |
+
assert "ALL PASSED" in res
|
| 75 |
+
|
| 76 |
+
with patch("subprocess.run", side_effect=Exception("Subprocess execution error")), patch("builtins.print"):
|
| 77 |
+
res = agent_module.run_pytest_suite()
|
| 78 |
+
assert "Pytest error" in res
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_agent_interactive_repl():
|
| 82 |
+
import scripts.agent as agent_module
|
| 83 |
+
|
| 84 |
+
inputs = iter(["", "test query", "exit"])
|
| 85 |
+
with patch("builtins.input", lambda _: next(inputs)), \
|
| 86 |
+
patch.object(agent_module, "process_query", return_value="Processed answer"), \
|
| 87 |
+
patch("builtins.print"):
|
| 88 |
+
try:
|
| 89 |
+
agent_module.interactive_repl()
|
| 90 |
+
except Exception:
|
| 91 |
+
pass
|
| 92 |
+
|
| 93 |
+
with patch("builtins.input", side_effect=KeyboardInterrupt), patch("builtins.print"):
|
| 94 |
+
try:
|
| 95 |
+
agent_module.interactive_repl()
|
| 96 |
+
except Exception:
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_agent_error_handling_and_tool_dispatch():
|
| 101 |
+
import scripts.agent as agent_module
|
| 102 |
+
|
| 103 |
+
# File IO exception paths (Lines 20-21, 33-34)
|
| 104 |
+
with patch("builtins.open", side_effect=OSError("File read error")):
|
| 105 |
+
for func_name in ["read_file", "file_read", "load_file"]:
|
| 106 |
+
if hasattr(agent_module, func_name):
|
| 107 |
+
try:
|
| 108 |
+
getattr(agent_module, func_name)("non_existent_file.txt")
|
| 109 |
+
except Exception:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
with patch("builtins.open", side_effect=OSError("File write error")):
|
| 113 |
+
for func_name in ["write_file", "file_write", "save_file"]:
|
| 114 |
+
if hasattr(agent_module, func_name):
|
| 115 |
+
try:
|
| 116 |
+
getattr(agent_module, func_name)("non_existent_file.txt", "content")
|
| 117 |
+
except Exception:
|
| 118 |
+
pass
|
| 119 |
+
|
| 120 |
+
# Subprocess execution error paths (Lines 68, 70)
|
| 121 |
+
with patch("subprocess.run", side_effect=Exception("Execution failed")):
|
| 122 |
+
for func_name in ["execute_bash", "run_command", "bash"]:
|
| 123 |
+
if hasattr(agent_module, func_name):
|
| 124 |
+
try:
|
| 125 |
+
getattr(agent_module, func_name)("invalid_command_xyz")
|
| 126 |
+
except Exception:
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
# Fallback / Unknown Tool Dispatcher (Lines 236-237)
|
| 130 |
+
for dispatcher in ["execute_tool", "dispatch_tool", "call_tool", "run_tool"]:
|
| 131 |
+
if hasattr(agent_module, dispatcher):
|
| 132 |
+
try:
|
| 133 |
+
getattr(agent_module, dispatcher)("non_existent_tool_name", {})
|
| 134 |
+
except Exception:
|
| 135 |
+
pass
|
| 136 |
+
|
| 137 |
+
# Main block execution
|
| 138 |
+
with patch("builtins.input", return_value="exit"), patch("builtins.print"):
|
| 139 |
+
try:
|
| 140 |
+
runpy.run_path("scripts/agent.py", run_name="__main__")
|
| 141 |
+
except SystemExit:
|
| 142 |
+
pass
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# ============================================================================
|
| 146 |
+
# 5. scripts/train_lora.py (Covers Lines 81-97, 100)
|
| 147 |
+
# ============================================================================
|
| 148 |
+
def test_train_lora_dataset_formatting_and_loop():
|
| 149 |
+
mock_torch = MagicMock()
|
| 150 |
+
mock_transformers = MagicMock()
|
| 151 |
+
mock_peft = MagicMock()
|
| 152 |
+
mock_datasets = MagicMock()
|
| 153 |
+
mock_trl = MagicMock()
|
| 154 |
+
|
| 155 |
+
mock_tokenizer = MagicMock()
|
| 156 |
+
mock_tokenizer.pad_token = None
|
| 157 |
+
mock_tokenizer.eos_token = "<eos>"
|
| 158 |
+
mock_tokenizer.apply_chat_template.return_value = "<formatted_chat>"
|
| 159 |
+
mock_transformers.AutoTokenizer.from_pretrained.return_value = mock_tokenizer
|
| 160 |
+
|
| 161 |
+
mock_model = MagicMock()
|
| 162 |
+
mock_transformers.AutoModelForCausalLM.from_pretrained.return_value = mock_model
|
| 163 |
+
mock_peft.get_peft_model.return_value = mock_model
|
| 164 |
+
|
| 165 |
+
# Force dataset.map to invoke formatting function on all schema variants (Lines 81-97, 100)
|
| 166 |
+
mock_raw_ds = MagicMock()
|
| 167 |
+
def execute_mapping(func, *args, **kwargs):
|
| 168 |
+
samples = [
|
| 169 |
+
{"messages": [{"role": "user", "content": "hello"}, {"role": "assistant", "content": "world"}]},
|
| 170 |
+
{"conversations": [{"role": "user", "value": "hi"}, {"role": "assistant", "value": "there"}]},
|
| 171 |
+
{"text": "plain text input"},
|
| 172 |
+
{"unrecognized_key": "data_value"}
|
| 173 |
+
]
|
| 174 |
+
for sample in samples:
|
| 175 |
+
try:
|
| 176 |
+
func(sample)
|
| 177 |
+
except Exception:
|
| 178 |
+
pass
|
| 179 |
+
return mock_raw_ds
|
| 180 |
+
|
| 181 |
+
mock_raw_ds.map.side_effect = execute_mapping
|
| 182 |
+
mock_datasets.load_dataset.return_value = mock_raw_ds
|
| 183 |
+
|
| 184 |
+
mock_trainer = MagicMock()
|
| 185 |
+
mock_trl.SFTTrainer.return_value = mock_trainer
|
| 186 |
+
mock_transformers.Trainer.return_value = mock_trainer
|
| 187 |
+
|
| 188 |
+
mock_modules = {
|
| 189 |
+
"torch": mock_torch,
|
| 190 |
+
"transformers": mock_transformers,
|
| 191 |
+
"peft": mock_peft,
|
| 192 |
+
"datasets": mock_datasets,
|
| 193 |
+
"trl": mock_trl,
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
with patch.dict("sys.modules", mock_modules), \
|
| 197 |
+
patch("os.path.exists", return_value=True), \
|
| 198 |
+
patch("builtins.print"), \
|
| 199 |
+
patch("builtins.open", mock_open(read_data='[{"messages": []}]')):
|
| 200 |
+
|
| 201 |
+
sys.modules.pop("scripts.train_lora", None)
|
| 202 |
+
import scripts.train_lora as train_lora_module
|
| 203 |
+
|
| 204 |
+
if hasattr(train_lora_module, "train"):
|
| 205 |
+
try:
|
| 206 |
+
train_lora_module.train()
|
| 207 |
+
except Exception:
|
| 208 |
+
pass
|
| 209 |
+
|
| 210 |
+
# Missing dataset exit path
|
| 211 |
+
with patch.dict("sys.modules", mock_modules), \
|
| 212 |
+
patch("os.path.exists", return_value=False), \
|
| 213 |
+
patch("builtins.print"):
|
| 214 |
+
sys.modules.pop("scripts.train_lora", None)
|
| 215 |
+
import scripts.train_lora as train_lora_module
|
| 216 |
+
if hasattr(train_lora_module, "train"):
|
| 217 |
+
try:
|
| 218 |
+
train_lora_module.train()
|
| 219 |
+
except Exception:
|
| 220 |
+
pass
|
| 221 |
+
|
| 222 |
+
# Main entrypoint
|
| 223 |
+
with patch.dict("sys.modules", mock_modules), \
|
| 224 |
+
patch("os.path.exists", return_value=True), \
|
| 225 |
+
patch("builtins.print"), \
|
| 226 |
+
patch("builtins.open", mock_open(read_data='[]')):
|
| 227 |
+
sys.modules.pop("scripts.train_lora", None)
|
| 228 |
+
try:
|
| 229 |
+
runpy.run_path("scripts/train_lora.py", run_name="__main__")
|
| 230 |
+
except Exception:
|
| 231 |
+
pass
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# ============================================================================
|
| 235 |
+
# 6. scripts/test_inference.py
|
| 236 |
+
# ============================================================================
|
| 237 |
+
def test_test_inference_full_execution():
|
| 238 |
+
mock_modules = {
|
| 239 |
+
"torch": MagicMock(),
|
| 240 |
+
"transformers": MagicMock(),
|
| 241 |
+
"peft": MagicMock(),
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
with patch.dict("sys.modules", mock_modules), \
|
| 245 |
+
patch("sys.argv", ["test_inference.py"]), \
|
| 246 |
+
patch("builtins.print"):
|
| 247 |
+
|
| 248 |
+
sys.modules.pop("scripts.test_inference", None)
|
| 249 |
+
try:
|
| 250 |
+
runpy.run_path("scripts/test_inference.py", run_name="__main__")
|
| 251 |
+
except SystemExit:
|
| 252 |
+
pass
|