Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active March 30, 2021 22:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/27f7ef9b3b807e72c56810acfc980833 to your computer and use it in GitHub Desktop.
Save pcreux/27f7ef9b3b807e72c56810acfc980833 to your computer and use it in GitHub Desktop.
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