Module: ParallelSpecs::Tasks

Defined in:
lib/parallel_specs/tasks.rb

Class Method Summary collapse

Class Method Details

.check_for_pending_migrationsObject



61
62
63
64
65
66
67
68
# File 'lib/parallel_specs/tasks.rb', line 61

def check_for_pending_migrations
  %w[db:abort_if_pending_migrations app:db:abort_if_pending_migrations].each do |task_name|
    if Rake::Task.task_defined?(task_name)
      Rake::Task[task_name].invoke
      break
    end
  end
end

.configured_databasesObject



92
93
94
95
96
# File 'lib/parallel_specs/tasks.rb', line 92

def configured_databases
  return [] unless defined?(ActiveRecord) && active_record_61_or_greater?

  @configured_databases ||= ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
end

.define_task_unless_defined(task_name, *args, &block) ⇒ Object



108
109
110
111
112
# File 'lib/parallel_specs/tasks.rb', line 108

def define_task_unless_defined(task_name, *args, &block)
  return if Rake::Task.task_defined?("parallel:#{task_name}")

  Rake::Task.define_task(task_name.to_sym, *args, &block)
end

.for_each_database(&block) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/parallel_specs/tasks.rb', line 98

def for_each_database(&block)
  block&.call(nil)
  return unless defined?(ActiveRecord::Tasks::DatabaseTasks)
  return unless ActiveRecord::Tasks::DatabaseTasks.respond_to?(:for_each)

  ActiveRecord::Tasks::DatabaseTasks.for_each(configured_databases) do |name|
    block&.call(name)
  end
end

.purge_before_loadObject



70
71
72
73
74
# File 'lib/parallel_specs/tasks.rb', line 70

def purge_before_load
  return unless defined?(ActiveRecord) && ActiveRecord.version > Gem::Version.new("4.2.0")

  Rake::Task.task_defined?("db:purge") ? "db:purge" : "app:db:purge"
end

.rails_envObject



10
11
12
# File 'lib/parallel_specs/tasks.rb', line 10

def rails_env
  ENV["PARALLEL_SPECS_RAILS_ENV"] || "test"
end

.run_command(env, command) ⇒ Object



28
29
30
# File 'lib/parallel_specs/tasks.rb', line 28

def run_command(env, command)
  system(env, *command)
end

.run_in_parallel(command, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/parallel_specs/tasks.rb', line 14

def run_in_parallel(command, options = {})
  command = command.compact
  num_processes = task_process_count(options[:count])

  thread_count = options[:non_parallel] ? 1 : num_processes
  results = Parallel.map(0...num_processes, in_threads: thread_count) do |process_number|
    env = worker_env(process_number, num_processes)
    expanded_command = expand_worker_env(command, env)
    run_command(env, expanded_command)
  end

  abort unless results.all?
end

.schema_format_based_on_rails_versionObject



76
77
78
79
80
81
82
# File 'lib/parallel_specs/tasks.rb', line 76

def schema_format_based_on_rails_version
  if active_record_7_or_greater?
    ActiveRecord.schema_format
  else
    ActiveRecord::Base.schema_format
  end
end

.schema_type_based_on_rails_versionObject



84
85
86
87
88
89
90
# File 'lib/parallel_specs/tasks.rb', line 84

def schema_type_based_on_rails_version
  if active_record_61_or_greater? || schema_format_based_on_rails_version == :ruby
    "schema"
  else
    "structure"
  end
end

.suppress_output(command, ignore_regex) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parallel_specs/tasks.rb', line 44

def suppress_output(command, ignore_regex)
  command.compact!
  activate_pipefail = "set -o pipefail"
  remove_ignored_lines = %{(grep -v #{Shellwords.escape(ignore_regex)} || true)}

  if system("/bin/bash", "-c", "#{activate_pipefail} 2>/dev/null")
    shell_command = "#{activate_pipefail} && (#{Shellwords.shelljoin(command)}) | #{remove_ignored_lines}"
    ["/bin/bash", "-c", shell_command]
  else
    command
  end
end

.suppress_schema_load_output(command) ⇒ Object



57
58
59
# File 'lib/parallel_specs/tasks.rb', line 57

def suppress_schema_load_output(command)
  suppress_output(command, '^   ->\\|^-- ')
end

.test_env_number(process_number) ⇒ Object



40
41
42
# File 'lib/parallel_specs/tasks.rb', line 40

def test_env_number(process_number)
  process_number.zero? ? "" : (process_number + 1).to_s
end

.worker_env(process_number, num_processes) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/parallel_specs/tasks.rb', line 32

def worker_env(process_number, num_processes)
  {
    "TEST_ENV_NUMBER" => test_env_number(process_number),
    "PARALLEL_SPECS_GROUPS" => num_processes.to_s,
    "DISABLE_SPRING" => "1"
  }
end