Skip to content

Instantly share code, notes, and snippets.

@pedrogimenez
Created December 10, 2020 13:44
Show Gist options
  • Save pedrogimenez/93e27f69776873c4f87c0b87f6c90c65 to your computer and use it in GitHub Desktop.
Save pedrogimenez/93e27f69776873c4f87c0b87f6c90c65 to your computer and use it in GitHub Desktop.
test
#!/usr/bin/env ruby --disable gems
# frozen_string_literal: true
require "pathname"
INCLUDE_BRANCH_COMMITS = "--include-branch-commits"
TEST_ARGV = ARGV.select { |arg| arg != INCLUDE_BRANCH_COMMITS }
changed_files = %x(git status --porcelain).scan(/^\s*\S+(?: +\S+ ->)? +(.*)$/).flatten
if ARGV.first == INCLUDE_BRANCH_COMMITS
changed_files |= %x(git diff --name-only $(git merge-base --fork-point origin/master)..).split("\n")
end
test_files = []
changed_files.grep(/\.rb\z/).each do |file|
if file =~ /_test\.rb\z/ && Pathname.new(file).expand_path.file?
test_files << file
next
end
test_file = file.sub(/^app/, "test")
.sub(%r{^engines/(\w+)/app}, 'engines/\1/test')
.sub(/\.rb$/, "_test.rb")
test_file = test_file.sub(/^(eager)?lib/, "test/lib")
test_file = test_file.sub(%r{^db/maintenance}, "test/unit")
if Pathname.new(test_file).expand_path.file?
test_files << test_file
end
end
test_files = test_files.uniq
if test_files.empty?
puts "No test files to run based on your changes."
else
puts "The following test files will be run:"
test_files.each { |file| puts "- #{file}" }
exit if ARGV.include?("--dry-run")
exec("RAILS_ENV=test bundle exec ruby -I:test -e \"ARGV.each{|f| require './' + f}\" #{test_files.join(' ')}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment