I enjoy creating small automations using Ruby and Bash. While it might be simpler to use Bash, I like #Ruby so much that I want to use it more.
Here are two examples of how to run all the tests that have changed in the current branch: an example for Minitest and one for RSpec.
The Minitest example
files = []
files += `git diff --name-only main...HEAD`.split("\n")
files += `git diff --cached --name-only`.split("\n")
files += `git diff --name-only`.split("\n")
files = files.grep(/_test\.rb$/).sort.uniq.reject(&:empty?)
if files.empty?
puts "No spec files found."
exit
end
puts "Files to execute:"
puts files.map { |f| " #{f}" }
exec "bundle exec bin/rails test #{files.join(' ')}"
The RSpec example
files = []
files += `git diff --name-only main...HEAD`.split("\n")
files += `git diff --cached --name-only`.split("\n")
files += `git diff --name-only`.split("\n")
files = files.grep(/_spec\.rb$/).sort.uniq.reject(&:empty?)
if files.empty?
puts "No spec files found."
exit
end
puts "Files to execute:"
puts files.map { |f| " #{f}" }
exec "bundle exec rspec #{files.join(' ')}"
You can, of course, modify this script to become an MCP, enabling tools like Claude Code or Cursor to run tests at the end of an implementation. During multiple rounds of test runs and fixes, when focusing on each test individually LLMs may pass the most recent test they fixed but fail previous ones.
Top comments (2)
Tooling for this already exists. The guard gem watches for file changes can do lots of actions using plugins.
github.com/guard/guard.
Guard-rspec is the test runner
Yes, but this is not the same. This will execute also any test file that is not changed currently but was committed in a previous commit on the current branch. It might not even be related to any changes made in the curren staging area.
Maybe guard can be configured like this but for me I prefer to execute this script when I finished all my commits, the branch is ready to be reviewed and I just want to make sure that everything that I changed works.
It is also useful when solving conflicts and run again all tests files that are changed in the current branch.
It is a bit dummier than guard as it will only execute the changed test files and will not execute related test files.