Class: OllamaChat::Tools::RunTests
- Inherits:
-
Object
- Object
- OllamaChat::Tools::RunTests
- Includes:
- Concern, Utils::PathValidator
- Defined in:
- lib/ollama_chat/tools/run_tests.rb
Overview
Tool for executing RSpec / Test‑Unit test suites.
The tool is registered under the name run_tests and exposes a single function that accepts a path (file or directory) and an optional coverage flag. The implementation simply spawns the configured test runner (RSpec or Minitest) and streams its output back to the caller.
Class Method Summary collapse
-
.register_name ⇒ String
The registered name for this tool.
Instance Method Summary collapse
-
#check_path(path, config) ⇒ String
private
The check_path method determines the appropriate test directory path based on existing directories and validates it against a whitelist.
-
#execute(tool_call, **opts) ⇒ String
Execute the tool with the provided arguments.
-
#run_tests(path, coverage) ⇒ Array(String, true, false)
private
Run the test suite using the configured test runner.
-
#test_runner ⇒ String
private
Resolve the test runner executable from configuration.
-
#tool ⇒ Tool
Build the OpenAI function schema for the tool.
Methods included from Utils::PathValidator
Methods included from Concern
Class Method Details
.register_name ⇒ String
Returns the registered name for this tool.
12 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 12 def self.register_name = 'run_tests' |
Instance Method Details
#check_path(path, config) ⇒ String (private)
The check_path method determines the appropriate test directory path based on existing directories and validates it against a whitelist.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 74 def check_path(path, config) if path.blank? if File.exist?('./spec') path = './spec' elsif File.exist?('./test') path = './test' elsif File.exist?('./tests') path = './tests' else raise ArgumentError, 'path could not be determined' end end assert_valid_path(path, config.tools.functions.run_tests.allowed?) File.exist?(path) or raise Errno::ENOENT, 'path %s does not exist' % path.inspect end |
#execute(tool_call, **opts) ⇒ String
Execute the tool with the provided arguments.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 50 def execute(tool_call, **opts) config = opts[:chat].config path = tool_call.function.arguments.path coverage = tool_call.function.arguments.coverage || false check_path(path, config) output, success = run_tests(path, coverage) { success:, path:, output:, status: success ? 'passed' : 'failed' }.to_json rescue => e { error: e.class, message: e. }.to_json end |
#run_tests(path, coverage) ⇒ Array(String, true, false) (private)
Run the test suite using the configured test runner.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 95 def run_tests(path, coverage) output = +'' env = ENV.to_h | { 'START_SIMPLECOV' => coverage ? '1' : '0' } IO.popen(env, "#{test_runner} #{path} 2>&1", ?r) do |io| while line = io.gets STDOUT.puts line output << line end end return output, $?.success? end |
#test_runner ⇒ String (private)
Resolve the test runner executable from configuration.
110 111 112 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 110 def test_runner OC::OLLAMA::CHAT::TOOLS::TEST_RUNNER end |
#tool ⇒ Tool
Build the OpenAI function schema for the tool.
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 |
# File 'lib/ollama_chat/tools/run_tests.rb', line 16 def tool Tool.new( type: 'function', function: Tool::Function.new( name: 'run_tests', description: <<~EOT, Test Runner - Runs all tests/specs under *path* the path were the tests/specs are located. `coverage=false` by default; set to true for a coverage report. Returns JSON with test counts and, if requested, coverage percentage. EOT parameters: Tool::Function::Parameters.new( type: 'object', properties: { path: Tool::Function::Parameters::Property.new( type: 'string', description: 'Path to file or directory to run tests for (path has to be allowed!)' ), coverage: Tool::Function::Parameters::Property.new( type: 'boolean', description: 'True if coverage data should be created, (default: false)' ) }, required: [] ) ) ) end |