Skip to content

Commit

Permalink
Add rails specific environment boostrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mbj committed Dec 2, 2020
1 parent cfa6b69 commit 8582a57
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/mutant/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Bootstrap
#
# rubocop:disable Metrics/MethodLength
def self.apply(world, config)
# TOOD: Have this behind a conditional.
config = Rails.call(world, config)

env = Env
Expand Down
38 changes: 29 additions & 9 deletions lib/mutant/bootstrap/rails.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
module Mutant
module Bootstrap
module Rails
def self.call(world, config)
class Rails
include Procto.call, Concord.new(:world, :config)

def call
world.stdout.puts('Loading mutant config from rails environment')

# TODO move to world.
# TODO add env to world.
#
# Make actual env configurable
ENV['RAILS_ENV'] = 'test'

world.kernel.require('./config/environment.rb')

::Rails.application.eager_load!

# TODO: preload engines, these *love* to pack things
# in the main project
#
# TODO: allow custom preload hooks.

add_rails_matchers
end

private

def add_rails_matchers
# This logic sucks, instead rails should be come a match expression
# possibly allow match expressions like: `ActionController.subclasses`
return config if config.matcher.match_expressions.any?

config.with(
matcher: config.matcher.with(match_expressions: rails_expressions)
)
end

def rails_expressions
expressions = [
ApplicationController,
*ApplicationController.subclasses,
ApplicationRecord,
*ApplicationRecord.subclasses
].map { |klass| config.expression_parser.apply(klass.name).from_right }

config.with(
matcher: config.matcher.with(
match_expressions: config.matcher.match_expressions | expressions
)
)
end

end # Rails
end # Bootstrap
end # Mutant
7 changes: 4 additions & 3 deletions lib/mutant/matcher/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ def add(attribute, value)
# @return [Config]
def merge(other)
self.class.new(
to_h
.map { |name, value| [name, value + other.public_send(name)] }
.to_h
ignore_expressions: other.ignore_expressions + ignore_expressions,
match_expressions: other.match_expressions,
start_expressions: other.start_expressions,
subject_filters: other.subject_filters + subject_filters
)
end

Expand Down

0 comments on commit 8582a57

Please sign in to comment.