1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# frozen_string_literal: true
require "abstract_unit"
require "active_support/core_ext/module/remove_method"
require "active_support/testing/stream"
require "active_support/testing/method_call_assertions"
require "rails/generators"
require "rails/generators/test_case"
module Rails
class << self
remove_possible_method :root
def root
@root ||= Pathname.new(File.expand_path("../fixtures", __dir__))
end
end
end
Rails.application.config.root = Rails.root
Rails.application.config.generators.templates = [File.join(Rails.root, "lib", "templates")]
# Call configure to load the settings from
# Rails.application.config.generators to Rails::Generators
Rails.application.load_generators
require "active_record"
require "action_dispatch"
require "action_view"
module GeneratorsTestHelper
include ActiveSupport::Testing::Stream
include ActiveSupport::Testing::MethodCallAssertions
GemfileEntry = Struct.new(:name, :version, :comment, :options, :commented_out) do
def initialize(name, version, comment, options = {}, commented_out = false)
super
end
end
def self.included(base)
base.class_eval do
destination File.join(Rails.root, "tmp")
setup :prepare_destination
begin
base.tests Rails::Generators.const_get(base.name.sub(/Test$/, ""))
rescue
end
end
end
def with_secondary_database_configuration
original_configurations = ActiveRecord::Base.configurations
ActiveRecord::Base.configurations = {
test: {
secondary: {
database: "db/secondary.sqlite3",
migrations_paths: "db/secondary_migrate",
},
},
}
yield
ensure
ActiveRecord::Base.configurations = original_configurations
end
def copy_routes
routes = File.expand_path("../../lib/rails/generators/rails/app/templates/config/routes.rb.tt", __dir__)
destination = File.join(destination_root, "config")
FileUtils.mkdir_p(destination)
FileUtils.cp routes, File.join(destination, "routes.rb")
end
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 = {})
erb = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
ERB.new(File.read(file), trim_mode: "-", eoutvar: "@output_buffer")
else
ERB.new(File.read(file), nil, "-", "@output_buffer")
end
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
end
|