Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dedicated build in rails environment loading #1129

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Mutant
end # Mutant

require 'mutant/bootstrap'
require 'mutant/bootstrap/rails'
require 'mutant/version'
require 'mutant/env'
require 'mutant/util'
Expand Down
3 changes: 3 additions & 0 deletions lib/mutant/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module Bootstrap
#
# rubocop:disable Metrics/MethodLength
def self.apply(world, config)
# TOOD: Have this behind a conditional.
config = Rails.call(world, config)

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

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

# 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`
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So expressions of the form $Constant.$method would expect to call a method on an object that returns the actual subclasses.

And: $Constant{,.$method} would match both the constant and the classes returned by the subclass.

We'd than still need an expression for the "entirety" of rails owned constants which could be: <Rails> or similar.

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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is likely way more that should be included in here.

].map { |klass| config.expression_parser.apply(klass.name).from_right }
end

end # Rails
end # Bootstrap
end # Mutant