Skip to content

Instantly share code, notes, and snippets.

@pcreux pcreux/rtt
Last active Nov 3, 2017

Embed
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")
else
files = `git status -s`.split("\n").map { |f| f.split(' ').last }
end
files += files.select { |f| f[/\/\z/] }.map { |dir| Dir["#{dir}**/*.rb"] }.flatten
test_files = files.select { |f| f["test/"] }
app_files = files.select { |f| f["app/"] }
lib_files = files.select { |f| f["lib/"] }
guessed_test_files = app_files.map { |f| f.gsub('app/', 'test/').gsub('.rb', '_test.rb') }
guessed_test_files += lib_files.map { |f| f.gsub('lib/', 'test/lib/').gsub('.rb', '_test.rb') }
all_test_files = test_files + guessed_test_files
existing_test_files = all_test_files.uniq.select { |f| f[/_test.rb\z/] }.select { |f| File.exists?(f) }
if existing_test_files.empty?
puts "No tests to run..."
exit 0
end
puts "Running tests:"
existing_test_files.each do |f|
puts " - #{f}"
end
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.