Repository navigation
Expand file tree
/
Copy pathRakefile
More file actions
255 lines (222 loc) · 6.87 KB
/
Copy pathRakefile
File metadata and controls
255 lines (222 loc) · 6.87 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
require "rspec/core/rake_task"
require "yard"
require "yard/mattr_accessor_handler"
require "rails/version"
require "tmpdir"
require "simplecov"
require "simplecov-console"
RSpec::Core::RakeTask.new(:spec)
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"].exclude("test/isolated/**/*")
end
# Each test boots its own application, so these can't share a process with the sandbox.
Rake::TestTask.new(:isolated_test) do |t|
t.libs << "lib"
t.test_files = FileList["test/isolated/**/*_test.rb"]
end
Rake::TestTask.new(:engine_test) do |t|
t.libs << "test_engine"
t.libs << "test_engine/lib"
t.test_files = FileList["test_engine/**/*_test.rb"]
end
desc("Runs benchmarks against components")
task(:partial_benchmark) do
ruby "./performance/partial_benchmark.rb"
end
task(:translatable_benchmark) do
ruby "./performance/translatable_benchmark.rb"
end
task(:slots_benchmark) do
ruby "./performance/slots_benchmark.rb"
end
task(:inline_components_benchmark) do
ruby "./performance/inline_benchmark.rb"
end
namespace(:coverage) do
task(:report) do
require "simplecov"
require "simplecov-console"
SimpleCov.minimum_coverage(100)
SimpleCov.collate(Dir["{coverage,simplecov-resultset-*}/.resultset.json"], "rails") do
formatter(SimpleCov::Formatter::Console)
end
end
end
namespace(:docs) do
task(:build) do
YARD::Rake::YardocTask.new do |t|
t.options = ["--no-output", "-q"]
end
Rake::Task["yard"].execute
registry = YARD::RegistryStore.new
registry.load!(".yardoc")
meths = registry
.get("ViewComponent::Base")
.meths
.select do |method|
!method.tag(:private) &&
method.path.include?("ViewComponent::Base") &&
method.visibility == :public &&
# Ignore methods we mark as internal by prefixing with underscores
!method[:name].to_s.start_with?("_")
end
.sort_by { |method| method[:name] }
instance_methods_to_document = meths.select { |method| method.scope != :class }
class_methods_to_document = meths.select { |method| method.scope == :class }
configuration_methods_to_document = registry.get("ViewComponent::Config").meths.select(&:reader?)
test_helper_methods_to_document = registry
.get("ViewComponent::TestHelpers")
.meths
.sort_by { |method| method[:name] }
.select do |method|
!method.tag(:private) &&
method.visibility == :public
end
require "rails"
require "action_controller"
require "view_component"
require "docs/docs_builder_component"
error_keys = registry.keys.select { |key| key.to_s.include?("Error::MESSAGE") }.map(&:to_s)
docs = ActionController::Base
.renderer
.render(
ViewComponent::DocsBuilderComponent.new(
sections: [
ViewComponent::DocsBuilderComponent::Section.new(
heading: "Class methods",
methods: class_methods_to_document
),
ViewComponent::DocsBuilderComponent::Section.new(
heading: "Instance methods",
methods: instance_methods_to_document
),
ViewComponent::DocsBuilderComponent::Section.new(
heading: "Configuration",
methods: configuration_methods_to_document,
show_types: false
),
ViewComponent::DocsBuilderComponent::Section.new(
heading: "ViewComponent::TestHelpers",
methods: test_helper_methods_to_document
),
ViewComponent::DocsBuilderComponent::Section.new(
heading: "Errors",
error_klasses: error_keys
)
]
)
)
.chomp
if ENV["RAILS_ENV"] != "test"
File.open("docs/api.md", "w") do |f|
f.puts(docs)
end
end
end
end
desc("Verify the app boots with eager_load=true (catches missing requires)")
task(:eager_load_check) do
puts("Checking eager loading...")
# Boot a minimal Rails app with eager_load enabled. Using a bare app (no
# components) ensures Zeitwerk doesn't accidentally autoload
# ViewComponent::Base before processing the engine's controllers — exactly
# the scenario that triggers NameError in a fresh host application.
result = system(
{"RAILS_ENV" => "test"},
"bundle",
"exec",
"ruby",
"-e",
<<~RUBY
require "rails"
require "action_controller/railtie"
require "view_component"
class EagerLoadCheckApp < Rails::Application
config.eager_load = true
config.secret_key_base = "test"
config.hosts.clear
end
EagerLoadCheckApp.initialize!
RUBY
)
abort("Eager loading check failed!") unless result
puts("Eager loading check passed")
end
# Gems that are used, but not loaded by `rake all_tests`.
# Older Rails needs redis for eager loading; Rails main uses redis-client.
GEMS_NOT_LOADED_BY_TESTS = %w[
appraisal
appraisal-run
audition
erb_lint
m
rubocop-md
rubocop-rubyfmt
standard
yard-lint
benchmark-ips
bundler
rails
propshaft
puma
sprockets-rails
redis
]
.freeze
desc("Fail if a gem in the Gemfile or gemspec wasn't loaded by the test suite")
task(:unused_gems_check) do
loaded = Dir[File.join(ENV.fetch("LOADED_GEMS_DIR"), "*.txt")].flat_map { File.readlines(_1, chomp: true) }
loaded |= Gem
.loaded_specs
.values
.select { |spec| $LOADED_FEATURES.any? { _1.start_with?("#{spec.full_gem_path}/") } }
.map(&:name)
declared = Bundler.definition.dependencies.map(&:name) - ["view_component"]
declared |= Gem::Specification.load("view_component.gemspec").runtime_dependencies.map(&:name)
unused = declared - loaded - GEMS_NOT_LOADED_BY_TESTS
if unused.any?
abort(
"Gems not loaded by the test suite, remove them or add them to GEMS_NOT_LOADED_BY_TESTS: #{unused.sort.join(", ")}"
)
end
puts("Unused gems check passed")
end
task(:all_tests) do
ENV["RAILS_ENV"] = "test"
ENV["LOADED_GEMS_DIR"] = Dir.mktmpdir
ENV["RUBYOPT"] = "#{ENV["RUBYOPT"]} -r#{File.expand_path("script/record_loaded_gems.rb", __dir__)}"
if ENV["MEASURE_COVERAGE"]
SimpleCov.start do
command_name("rails#{Rails::VERSION::STRING}-ruby#{RUBY_VERSION}")
enable_coverage(:branch)
formatter(SimpleCov::Formatter::Console)
end
end
puts("Checking eager loading")
Rake::Task["eager_load_check"].invoke
puts
puts
puts("Running Minitests")
Rake::Task["test"].invoke
puts
puts
puts("Running isolated Minitests")
Rake::Task["isolated_test"].invoke
puts
puts
puts("Running Minitests for Rails Engine compatibility")
Rake::Task["engine_test"].invoke
puts
puts
puts("Running RSpecs")
Rake::Task["spec"].invoke
puts
puts
Rake::Task["unused_gems_check"].invoke
end
task(default: [:all_tests])