Module: Mutineer::ReloadGuards

Defined in:
lib/mutineer/reload_guards.rb

Overview

Lets the reload strategy run a Rails class body a second time.

Reload loads the mutant file over a class that is already defined, so the class body runs again. Two Rails macros refuse that:

  • enum raises because the enum's methods already exist (#378).
  • skip_callback (and skip_before_action, which calls it) raises because the first run already removed the callback (#384).

While ReloadGuards.during_load runs, an enum the class had before the load skips its conflict check, and skip_callback does not raise for a callback the first run already removed (see CallbackReskipping). The original file ran the same macros once with the full checks, so the second run repeats work Rails already accepted. Outside the load, for example while the tests run, both macros check as usual.

ReloadGuards.during_load also loads the original file first when the app autoloads it and nothing has loaded it yet (#383). Ruby would do that anyway when the mutant reopens the constant, but inside the load, after ConcernBlocks.forget, so the concern's original included block would come back and the mutant's block would raise MultipleIncludedBlocks.

A second run of the class body also adds each block callback, lambda callback and validator again. Rails replaces a repeated callback only when it names a method. So ReloadGuards.during_load removes the original's copy of each one the load added again (see RepeatedCallbacks, #390). Otherwise a validates or a before_save do ... end would run twice in every mutant of the file, and a test that counts errors or side effects would give a false kill.

Call it only in a forked child that loads the mutant: it prepends onto Rails modules in that process. Without Rails it only runs the block.

Stdlib-only, so the app-side daemon can load it.

Defined Under Namespace

Modules: CallbackReskipping, EnumRedeclaration, RepeatedCallbacks

Constant Summary collapse

CALLBACK_KINDS =

The callback kinds skip_callback takes before its filters.

%i[before after around].freeze

Class Method Summary collapse

Class Method Details

.callbacks_loaded? ⇒ Boolean

defined? does not load an autoloaded constant, but using it does.

Returns:

  • (Boolean) —

    true when ActiveSupport::Callbacks is loaded, not only set to autoload.



948
949
950
# File 'lib/mutineer/reload_guards.rb', line 948

def self.callbacks_loaded?
  !!defined?(::ActiveSupport::Callbacks) && !::ActiveSupport.autoload?(:Callbacks)
end

.during_load(source_file, mutant_path = nil) { ... } ⇒ Object

Loads the original file if the app autoloads it, then runs the block that loads the mutant with the guards on. After the block, removes the original's copy of each callback and validator the load added again.

Parameters:

  • source_file (String) —

    the file the mutant replaces.

  • mutant_path (String, nil) (defaults to: nil) —

    the file the block loads.

Yields:

  • the mutant's load.

Returns:

  • (Object) —

    what the block returns.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mutineer/reload_guards.rb', line 90

def self.during_load(source_file, mutant_path = nil)
  load_original(source_file)
  install!
  before = RepeatedCallbacks.snapshot
  @enums_before_load = {}
  @loading = true
  result = yield
  RepeatedCallbacks.remove(before, [source_file, mutant_path].compact)
  result
ensure
  @loading = false
end

.enum_existed?(klass, enum_name) ⇒ Boolean

Whether klass had the enum before this load. Rails records a new enum after its first conflict check, so the answer is read once, at the first check of each enum, and kept for the rest of the load.

Parameters:

  • klass (Class) —

    the model declaring the enum.

  • enum_name (Symbol, String) —

    the enum being declared.

Returns:

  • (Boolean)


901
902
903
904
# File 'lib/mutineer/reload_guards.rb', line 901

def self.enum_existed?(klass, enum_name)
  key = [klass, enum_name.to_s]
  @enums_before_load.fetch(key) { @enums_before_load[key] = klass.defined_enums.key?(key.last) }
end

.install! ⇒ void

This method returns an undefined value.

Prepends the guards once, onto the Rails modules this process has loaded. A module Rails only set to autoload stays unloaded, so the mutant run sees the same Ruby as the clean run.



935
936
937
938
939
940
941
942
# File 'lib/mutineer/reload_guards.rb', line 935

def self.install!
  if defined?(::ActiveRecord::Enum) && !::ActiveRecord.autoload?(:Enum)
    prepend_once(::ActiveRecord::Enum, EnumRedeclaration)
  end
  return unless callbacks_loaded?

  prepend_once(::ActiveSupport::Callbacks::ClassMethods, CallbackReskipping)
end

.load_original(source_file) ⇒ void

This method returns an undefined value.

Asks each Rails autoloader for the constant source_file defines. A loader that does not manage the file raises Zeitwerk::Error; the next one is asked. A managed file that does not define the constant Zeitwerk expects raises Zeitwerk::NameError, a NameError; the mutant load goes on as before. A file already loaded only costs a constant lookup. Autoload roots are real paths, so the loaders get the real path.

Parameters:

  • source_file (String) —

    the file the mutant replaces.



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'lib/mutineer/reload_guards.rb', line 968

def self.load_original(source_file)
  return unless defined?(::Rails) && ::Rails.respond_to?(:autoloaders) && defined?(::Zeitwerk::Error)
  # A library can load railties with no Rails app; `Rails.autoloaders` then raises.
  return unless ::Rails.respond_to?(:application) && ::Rails.application

  ::Rails.autoloaders.each do |loader|
    next unless loader.respond_to?(:load_file)

    begin
      loader.load_file(RepeatedCallbacks.real_path(source_file))
      return
    rescue ::Zeitwerk::Error, ::NameError
      next
    end
  end
end

.loading? ⇒ Boolean

True while a mutant file loads.

Returns:

  • (Boolean)


78
79
80
# File 'lib/mutineer/reload_guards.rb', line 78

def self.loading?
  @loading == true
end

.prepend_once(target, guard) ⇒ void

This method returns an undefined value.

Parameters:

  • target (Module) —

    the Rails module.

  • guard (Module) —

    the guard to put in front of it.



955
956
957
# File 'lib/mutineer/reload_guards.rb', line 955

def self.prepend_once(target, guard)
  target.prepend(guard) unless target.ancestors.include?(guard)
end

.repeated_skip?(klass, name, *args) ⇒ Boolean

True when every filter of this skip is a method klass has, or a callback of that kind on chain name that the superclass still has. The kind is optional and defaults to :before, as in Rails. With no filters there is nothing to compare, so false.

Parameters:

  • klass (Class) —

    the class calling skip_callback.

  • name (Symbol) —

    the callback chain, such as :save.

  • args (Array) —

    the optional kind, then the filters.

Returns:

  • (Boolean)


918
919
920
921
922
923
924
925
926
927
928
# File 'lib/mutineer/reload_guards.rb', line 918

def self.repeated_skip?(klass, name, *args)
  kind = CALLBACK_KINDS.include?(args.first) ? args.shift : :before
  return false if args.empty?

  parent = klass.superclass
  chain = parent.respond_to?(:__callbacks) ? parent.__callbacks[name] : nil
  args.all? do |filter|
    (filter.is_a?(Symbol) && (klass.method_defined?(filter) || klass.private_method_defined?(filter))) ||
      chain&.any? { |callback| callback.kind == kind && callback.filter == filter }
  end
end