Class: AgentC::Tools::RunRailsTest

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/agent_c/tools/run_rails_test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace_dir: nil, env: {}) ⇒ RunRailsTest

Returns a new instance of RunRailsTest.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/agent_c/tools/run_rails_test.rb', line 33

def initialize(
  workspace_dir: nil,
  env: {},
  **
)
  raise ArgumentError, "workspace_dir is required" unless workspace_dir
  @env = env
  @workspace_dir = workspace_dir
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



32
33
34
# File 'lib/agent_c/tools/run_rails_test.rb', line 32

def env
  @env
end

#workspace_dirObject (readonly)

Returns the value of attribute workspace_dir.



32
33
34
# File 'lib/agent_c/tools/run_rails_test.rb', line 32

def workspace_dir
  @workspace_dir
end

Instance Method Details

#execute(path:, test_method_name: nil, disable_spring: false, **params) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/agent_c/tools/run_rails_test.rb', line 43

def execute(path:, test_method_name: nil, disable_spring: false, **params)

  # Spring hangs, need to timeout unresponsive shells
  disable_spring = true

  if params.any?
    return "The following params were passed but are not allowed: #{params.keys.join(",")}"
  end

  unless Paths.allowed?(workspace_dir, path)
    return "Path: #{path} not acceptable. Must be a child of directory: #{workspace_dir}."
  end

  workspace_path = Paths.relative_to_dir(workspace_dir, path)

  env_string = env.is_a?(Hash) ? env.map { |k, v| "#{k}=#{Shellwords.escape(v)}"}.join(" ") : env

  env_string += " DISABLE_SPRING=1" if disable_spring

  cmd = <<~TXT.chomp
    cd #{workspace_dir} && \
    #{env_string} bundle exec rails test #{path} #{test_method_name && "--name='#{test_method_name}'"}
  TXT

  lines = []
  result = nil
  Bundler.with_unbundled_env do
    result = shell.run(cmd) do |stream, line|
      lines << "[#{stream}] #{line}"
    end
  end

  return <<~TXT
    Command exited #{result.success? ? "successfully" : "with non-zero exit code"}
    ---
    #{lines.join("\n")}
  TXT
end

#shellObject



82
83
84
# File 'lib/agent_c/tools/run_rails_test.rb', line 82

def shell
  AgentC::Utils::Shell
end