module ParallelTests::Tasks
def check_for_pending_migrations
def check_for_pending_migrations ["db:abort_if_pending_migrations", "app:db:abort_if_pending_migrations"].each do |abort_migrations| if Rake::Task.task_defined?(abort_migrations) Rake::Task[abort_migrations].invoke break end end end
def load_lib
def load_lib $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..')) require "parallel_tests" end
def parse_args(args)
def parse_args(args) # order as given by user args = [args[:count], args[:pattern], args[:options], args[:pass_through]] # count given or empty ? # parallel:spec[2,models,options] # parallel:spec[,models,options] count = args.shift if args.first.to_s =~ /^\d*$/ num_processes = (count.to_s.empty? ? nil : Integer(count)) pattern = args.shift options = args.shift pass_through = args.shift [num_processes, pattern.to_s, options.to_s, pass_through.to_s] end
def purge_before_load
def purge_before_load if Gem::Version.new(Rails.version) > Gem::Version.new('4.2.0') Rake::Task.task_defined?('db:purge') ? 'db:purge' : 'app:db:purge' end end
def rails_env
def rails_env 'test' end
def rake_bin
def rake_bin # Prevent 'Exec format error' Errno::ENOEXEC on Windows return "rake" if RUBY_PLATFORM =~ /mswin|mingw|cygwin/ binstub_path = File.join('bin', 'rake') return binstub_path if File.exist?(binstub_path) "rake" end
def run_in_parallel(cmd, options = {})
def run_in_parallel(cmd, options = {}) load_lib count = " -n #{options[:count]}" unless options[:count].to_s.empty? # Using the relative path to find the binary allow to run a specific version of it executable = File.expand_path('../../bin/parallel_test', __dir__) non_parallel = (options[:non_parallel] ? ' --non-parallel' : '') command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} --exec '#{cmd}'#{count}#{non_parallel}" abort unless system(command) end
def suppress_output(command, ignore_regex)
- do not use ' since run_in_parallel uses them to quote stuff
- defining a new rake task like silence_schema would force users to load parallel_tests in test env
- pipefail is not supported in (zsh)
- pipefail makes pipe fail with exitstatus of first failed command
- sed changes 1 exitstatus to 0
- grep changes 0 exitstatus to 1 if nothing matches
- sed does not support | without -r
normally I'd not do this, but it has been lots of fun and a great learning experience :)
removing certain lines from the output without changing the exit-status
this is a crazy-complex solution for a very simple problem:
def suppress_output(command, ignore_regex) activate_pipefail = "set -o pipefail" remove_ignored_lines = %{(grep -v "#{ignore_regex}" || test 1)} if File.executable?('/bin/bash') && system('/bin/bash', '-c', "#{activate_pipefail} 2>/dev/null && test 1") # We need to shell escape single quotes (' becomes '"'"') because # run_in_parallel wraps command in single quotes %{/bin/bash -c '"'"'#{activate_pipefail} && (#{command}) | #{remove_ignored_lines}'"'"'} else command end end
def suppress_schema_load_output(command)
def suppress_schema_load_output(command) ParallelTests::Tasks.suppress_output(command, "^ ->\\|^-- ") end