lib/parallel_tests/tasks.rb in parallel_tests-3.4.0 vs lib/parallel_tests/tasks.rb in parallel_tests-3.5.0

- old
+ new

@@ -1,5 +1,6 @@ +# frozen_string_literal: true require 'rake' require 'shellwords' module ParallelTests module Tasks @@ -25,16 +26,17 @@ 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 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", __FILE__) - command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} --exec '#{cmd}'#{count}#{' --non-parallel' if options[:non_parallel]}" + 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 # this is a crazy-complex solution for a very simple problem: # removing certain lines from the output without changing the exit-status @@ -48,16 +50,16 @@ # - defining a new rake task like silence_schema would force users to load parallel_tests in test env # - do not use ' since run_in_parallel uses them to quote stuff # - simple system "set -o pipefail" returns nil even though set -o pipefail exists with 0 def suppress_output(command, ignore_regex) activate_pipefail = "set -o pipefail" - remove_ignored_lines = %Q{(grep -v "#{ignore_regex}" || test 1)} + 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 - %Q{/bin/bash -c '"'"'#{activate_pipefail} && (#{command}) | #{remove_ignored_lines}'"'"'} + %{/bin/bash -c '"'"'#{activate_pipefail} && (#{command}) | #{remove_ignored_lines}'"'"'} else command end end @@ -81,11 +83,11 @@ # 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_i unless count.to_s.empty? + 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] @@ -94,30 +96,32 @@ end end namespace :parallel do desc "Setup test databases via db:setup --> parallel:setup[num_cpus]" - task :setup, :count do |_,args| + task :setup, :count do |_, args| command = "#{ParallelTests::Tasks.rake_bin} db:setup RAILS_ENV=#{ParallelTests::Tasks.rails_env}" ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args) end desc "Create test databases via db:create --> parallel:create[num_cpus]" - task :create, :count do |_,args| + task :create, :count do |_, args| ParallelTests::Tasks.run_in_parallel( - "#{ParallelTests::Tasks.rake_bin} db:create RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args) + "#{ParallelTests::Tasks.rake_bin} db:create RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args + ) end desc "Drop test databases via db:drop --> parallel:drop[num_cpus]" - task :drop, :count do |_,args| + task :drop, :count do |_, args| ParallelTests::Tasks.run_in_parallel( "#{ParallelTests::Tasks.rake_bin} db:drop RAILS_ENV=#{ParallelTests::Tasks.rails_env} " \ - "DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args) + "DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args + ) end desc "Update test databases by dumping and loading --> parallel:prepare[num_cpus]" - task(:prepare, [:count]) do |_,args| + task(:prepare, [:count]) do |_, args| ParallelTests::Tasks.check_for_pending_migrations if defined?(ActiveRecord::Base) && [:ruby, :sql].include?(ActiveRecord::Base.schema_format) # fast: dump once, load in parallel type = (ActiveRecord::Base.schema_format == :ruby ? "schema" : "structure") Rake::Task["db:#{type}:dump"].invoke @@ -126,75 +130,78 @@ ActiveRecord::Base.remove_connection if ActiveRecord::Base.configurations.any? Rake::Task["parallel:load_#{type}"].invoke(args[:count]) else # slow: dump and load in in serial - args = args.to_hash.merge(:non_parallel => true) # normal merge returns nil + args = args.to_hash.merge(non_parallel: true) # normal merge returns nil task_name = Rake::Task.task_defined?('db:test:prepare') ? 'db:test:prepare' : 'app:db:test:prepare' ParallelTests::Tasks.run_in_parallel("#{ParallelTests::Tasks.rake_bin} #{task_name}", args) next end end # when dumping/resetting takes too long desc "Update test databases via db:migrate --> parallel:migrate[num_cpus]" - task :migrate, :count do |_,args| + task :migrate, :count do |_, args| ParallelTests::Tasks.run_in_parallel( - "#{ParallelTests::Tasks.rake_bin} db:migrate RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args) + "#{ParallelTests::Tasks.rake_bin} db:migrate RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args + ) end desc "Rollback test databases via db:rollback --> parallel:rollback[num_cpus]" - task :rollback, :count do |_,args| + task :rollback, :count do |_, args| ParallelTests::Tasks.run_in_parallel( - "#{ParallelTests::Tasks.rake_bin} db:rollback RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args) + "#{ParallelTests::Tasks.rake_bin} db:rollback RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args + ) end # just load the schema (good for integration server <-> no development db) desc "Load dumped schema for test databases via db:schema:load --> parallel:load_schema[num_cpus]" - task :load_schema, :count do |_,args| + task :load_schema, :count do |_, args| command = "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \ "db:schema:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1" ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args) end # load the structure from the structure.sql file desc "Load structure for test databases via db:structure:load --> parallel:load_structure[num_cpus]" - task :load_structure, :count do |_,args| + task :load_structure, :count do |_, args| ParallelTests::Tasks.run_in_parallel( "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \ - "db:structure:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args) + "db:structure:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args + ) end desc "Load the seed data from db/seeds.rb via db:seed --> parallel:seed[num_cpus]" - task :seed, :count do |_,args| + task :seed, :count do |_, args| ParallelTests::Tasks.run_in_parallel( - "#{ParallelTests::Tasks.rake_bin} db:seed RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args) + "#{ParallelTests::Tasks.rake_bin} db:seed RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args + ) end desc "Launch given rake command in parallel" task :rake, :command, :count do |_, args| ParallelTests::Tasks.run_in_parallel( "RAILS_ENV=#{ParallelTests::Tasks.rails_env} #{ParallelTests::Tasks.rake_bin} " \ - "#{args.command}", args) + "#{args.command}", args + ) end ['test', 'spec', 'features', 'features-spinach'].each do |type| desc "Run #{type} in parallel with parallel:#{type}[num_cpus]" - task type, [:count, :pattern, :options, :pass_through] do |t, args| + task type, [:count, :pattern, :options, :pass_through] do |_t, args| ParallelTests::Tasks.check_for_pending_migrations ParallelTests::Tasks.load_lib count, pattern, options, pass_through = ParallelTests::Tasks.parse_args(args) test_framework = { 'spec' => 'rspec', 'test' => 'test', 'features' => 'cucumber', - 'features-spinach' => 'spinach', + 'features-spinach' => 'spinach' }[type] - if test_framework == 'spinach' - type = 'features' - end + type = 'features' if test_framework == 'spinach' # Using the relative path to find the binary allow to run a specific version of it executable = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'parallel_test') command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} #{type} " \ "--type #{test_framework} " \