2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/testing/stream"
|
2019-08-02 00:24:21 -04:00
|
|
|
require "active_support/testing/method_call_assertions"
|
2016-08-06 13:16:09 -04:00
|
|
|
require "rails/generators"
|
|
|
|
require "rails/generators/test_case"
|
2009-11-02 21:08:33 -05:00
|
|
|
|
2020-11-23 17:43:01 -05:00
|
|
|
Rails.application.config.generators.templates = [File.expand_path("../fixtures/lib/templates", __dir__)]
|
2009-11-02 21:08:33 -05:00
|
|
|
|
2010-06-02 02:45:03 -04:00
|
|
|
# Call configure to load the settings from
|
|
|
|
# Rails.application.config.generators to Rails::Generators
|
2011-05-25 03:43:24 -04:00
|
|
|
Rails.application.load_generators
|
2010-01-29 11:41:10 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "active_record"
|
|
|
|
require "action_dispatch"
|
|
|
|
require "action_view"
|
2009-07-03 08:55:37 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
module GeneratorsTestHelper
|
2015-01-15 01:42:33 -05:00
|
|
|
include ActiveSupport::Testing::Stream
|
2019-08-02 00:24:21 -04:00
|
|
|
include ActiveSupport::Testing::MethodCallAssertions
|
2015-01-15 01:42:33 -05:00
|
|
|
|
2018-12-31 13:46:30 -05:00
|
|
|
GemfileEntry = Struct.new(:name, :version, :comment, :options, :commented_out) do
|
|
|
|
def initialize(name, version, comment, options = {}, commented_out = false)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2020-11-23 17:43:01 -05:00
|
|
|
destination File.expand_path("../fixtures/tmp", __dir__)
|
2010-01-18 18:07:11 -05:00
|
|
|
setup :prepare_destination
|
2009-06-25 04:44:46 -04:00
|
|
|
|
2020-11-23 17:43:01 -05:00
|
|
|
setup { Rails.application.config.root = Pathname("../fixtures").expand_path(__dir__) }
|
|
|
|
|
|
|
|
setup { @original_rakeopt, ENV["RAKEOPT"] = ENV["RAKEOPT"], "--silent" }
|
|
|
|
teardown { ENV["RAKEOPT"] = @original_rakeopt }
|
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
begin
|
2020-05-24 14:15:38 -04:00
|
|
|
base.tests Rails::Generators.const_get(base.name.delete_suffix("Test"))
|
2010-01-18 18:07:11 -05:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
2010-01-03 18:31:53 -05:00
|
|
|
end
|
2010-03-19 13:11:37 -04:00
|
|
|
|
2020-11-22 12:50:24 -05:00
|
|
|
def run_generator_instance
|
|
|
|
capture(:stdout) do
|
|
|
|
generator.invoke_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-02 12:55:57 -04:00
|
|
|
def with_database_configuration(database_name = "secondary")
|
2018-09-28 20:25:59 -04:00
|
|
|
original_configurations = ActiveRecord::Base.configurations
|
2018-09-28 13:36:06 -04:00
|
|
|
ActiveRecord::Base.configurations = {
|
|
|
|
test: {
|
2020-08-02 12:55:57 -04:00
|
|
|
"#{database_name}": {
|
|
|
|
database: "db/#{database_name}.sqlite3",
|
|
|
|
migrations_paths: "db/#{database_name}_migrate",
|
2018-09-28 13:36:06 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
yield
|
|
|
|
ensure
|
2018-09-28 20:25:59 -04:00
|
|
|
ActiveRecord::Base.configurations = original_configurations
|
2018-09-28 13:36:06 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 13:11:37 -04:00
|
|
|
def copy_routes
|
2017-11-13 15:23:28 -05:00
|
|
|
routes = File.expand_path("../../lib/rails/generators/rails/app/templates/config/routes.rb.tt", __dir__)
|
2010-03-19 13:11:37 -04:00
|
|
|
destination = File.join(destination_root, "config")
|
|
|
|
FileUtils.mkdir_p(destination)
|
2017-11-13 15:23:28 -05:00
|
|
|
FileUtils.cp routes, File.join(destination, "routes.rb")
|
2010-03-19 13:11:37 -04:00
|
|
|
end
|
2018-12-31 13:46:30 -05:00
|
|
|
|
|
|
|
def copy_gemfile(*gemfile_entries)
|
|
|
|
locals = gemfile_locals.merge(gemfile_entries: gemfile_entries)
|
|
|
|
gemfile = File.expand_path("../../lib/rails/generators/rails/app/templates/Gemfile.tt", __dir__)
|
|
|
|
gemfile = evaluate_template(gemfile, locals)
|
|
|
|
destination = File.join(destination_root)
|
|
|
|
File.write File.join(destination, "Gemfile"), gemfile
|
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate_template(file, locals = {})
|
2021-02-04 15:12:44 -05:00
|
|
|
erb = ERB.new(File.read(file), trim_mode: "-", eoutvar: "@output_buffer")
|
2018-12-31 13:46:30 -05:00
|
|
|
context = Class.new do
|
|
|
|
locals.each do |local, value|
|
|
|
|
class_attribute local, default: value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
erb.result(context.new.instance_eval("binding"))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def gemfile_locals
|
|
|
|
{
|
|
|
|
skip_active_storage: true,
|
|
|
|
depend_on_bootsnap: false,
|
|
|
|
depend_on_listen: false,
|
|
|
|
spring_install: false,
|
|
|
|
depends_on_system_test: false,
|
|
|
|
options: ActiveSupport::OrderedOptions.new,
|
|
|
|
}
|
|
|
|
end
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|