Module: Mutineer::RailsRunEnv

Defined in:
lib/mutineer/rails_run_env.rb

Overview

Pins the environment Rails and Spring read during a mutant run.

Rails reads PARALLEL_WORKERS inside parallelize and uses it instead of the workers: argument. A value of 1 keeps that run in one process. --test-command also sets DISABLE_SPRING so the Spring preloader does not start. RailsRunEnv.pin_process! prints the replacement line once. The caller puts the process value back with RailsRunEnv.restore_process! when the run ends.

Constant Summary collapse

PARALLEL_WORKERS =

Value of PARALLEL_WORKERS that keeps Rails test workers off.

"1"
DISABLE_SPRING =

Value of DISABLE_SPRING that stops the Spring preloader.

"1"

Class Method Summary collapse

Class Method Details

.pin_child!(env, spring: false) ⇒ void

This method returns an undefined value.

Forces PARALLEL_WORKERS on a child environment. spring also sets DISABLE_SPRING. The hash is the spawn delta or the full daemon environment. This does not change the parent process.

Parameters:

  • env (Hash{String => String, nil})
  • spring (Boolean) (defaults to: false) —

    set DISABLE_SPRING when true.



49
50
51
52
53
# File 'lib/mutineer/rails_run_env.rb', line 49

def self.pin_child!(env, spring: false)
  env["PARALLEL_WORKERS"] = PARALLEL_WORKERS
  env["DISABLE_SPRING"] = DISABLE_SPRING if spring
  nil
end

.pin_process! ⇒ void

This method returns an undefined value.

Sets PARALLEL_WORKERS to PARALLEL_WORKERS in this process. Prints one line when the previous value is set and is not already 1.



22
23
24
25
26
27
# File 'lib/mutineer/rails_run_env.rb', line 22

def self.pin_process!
  current = ENV["PARALLEL_WORKERS"]
  warn(replacement_line(current)) if replace?(current)
  ENV["PARALLEL_WORKERS"] = PARALLEL_WORKERS
  nil
end

.replace?(current) ⇒ Boolean (private)

True when Mutineer must replace a PARALLEL_WORKERS value the user set.

Parameters:

  • current (String, nil)

Returns:

  • (Boolean)


59
60
61
# File 'lib/mutineer/rails_run_env.rb', line 59

def self.replace?(current)
  !current.nil? && current != PARALLEL_WORKERS
end

.replacement_line(current) ⇒ String (private)

The one line printed when replace? is true.

Parameters:

  • current (String)

Returns:

  • (String)


67
68
69
70
# File 'lib/mutineer/rails_run_env.rb', line 67

def self.replacement_line(current)
  "[mutineer] PARALLEL_WORKERS was #{current.inspect}; using 1 so each mutant's tests run in one process. " \
    "Unset it or set it to 1 to drop this line."
end

.restore_process!(previous) ⇒ void

This method returns an undefined value.

Puts the caller's PARALLEL_WORKERS back. nil means it was unset.

Parameters:

  • previous (String, nil)


33
34
35
36
37
38
39
40
# File 'lib/mutineer/rails_run_env.rb', line 33

def self.restore_process!(previous)
  if previous.nil?
    ENV.delete("PARALLEL_WORKERS")
  else
    ENV["PARALLEL_WORKERS"] = previous
  end
  nil
end