Skip to content

Instantly share code, notes, and snippets.

@pcreux pcreux/rtt
Last active Nov 3, 2017

What would you like to do?
Run tests for all changed app, lib and test files
#!/usr/bin/env ruby
# Run unit test for all added or changed app, lib and test files.
# Ex:
# * `rtt` will run tests for all files changed since the last commit
# * `rtt master` will run tests for all files different from master
if ARGV[0]
files = `git diff --name-only $(git merge-base HEAD #{ARGV[0]})..$(git rev-parse --abbrev-ref HEAD)`.split("\n")
files = `git status -s`.split("\n").map { |f| f.split(' ').last }
files += { |f| f[/\/\z/] }.map { |dir| Dir["#{dir}**/*.rb"] }.flatten
test_files = { |f| f["test/"] }
app_files = { |f| f["app/"] }
lib_files = { |f| f["lib/"] }
guessed_test_files = { |f| f.gsub('app/', 'test/').gsub('.rb', '_test.rb') }
guessed_test_files += { |f| f.gsub('lib/', 'test/lib/').gsub('.rb', '_test.rb') }
all_test_files = test_files + guessed_test_files
existing_test_files = { |f| f[/_test.rb\z/] }.select { |f| File.exists?(f) }
if existing_test_files.empty?
puts "No tests to run..."
exit 0
puts "Running tests:"
existing_test_files.each do |f|
puts " - #{f}"
puts ""
cmd = (['bin/rails test'] + existing_test_files).join(' ')
puts cmd
system "zsh -c '#{cmd}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.