2019-08-16 20:09:33 +03:00
|
|
|
# frozen_string_literal: true
|
2021-07-02 00:01:38 +03:00
|
|
|
|
2024-09-26 00:48:23 +03:00
|
|
|
require "allocation_stats"
|
2020-06-22 16:31:07 +03:00
|
|
|
require "simplecov"
|
2020-07-27 23:46:40 +03:00
|
|
|
require "simplecov-console"
|
2023-05-23 18:24:40 +03:00
|
|
|
require "rails/version"
|
2020-06-22 16:31:07 +03:00
|
|
|
|
2020-09-22 01:56:14 +03:00
|
|
|
if ENV["MEASURE_COVERAGE"]
|
2020-09-22 01:54:36 +03:00
|
|
|
SimpleCov.start do
|
2023-05-23 18:24:40 +03:00
|
|
|
command_name "minitest-rails#{Rails::VERSION::STRING}-ruby#{RUBY_VERSION}"
|
2020-08-22 01:23:38 +03:00
|
|
|
|
2020-09-22 01:54:36 +03:00
|
|
|
formatter SimpleCov::Formatter::Console
|
|
|
|
end
|
2020-06-22 16:31:07 +03:00
|
|
|
end
|
|
|
|
|
2019-08-16 20:09:33 +03:00
|
|
|
require "bundler/setup"
|
|
|
|
require "pathname"
|
|
|
|
require "minitest/autorun"
|
|
|
|
|
2022-05-11 17:06:40 +03:00
|
|
|
if ENV["RAISE_ON_WARNING"]
|
|
|
|
module Warning
|
|
|
|
PROJECT_ROOT = File.expand_path("..", __dir__).freeze
|
|
|
|
|
|
|
|
def self.warn(message)
|
|
|
|
called_by = caller_locations(1, 1).first.path
|
|
|
|
return super unless called_by&.start_with?(PROJECT_ROOT) && !called_by.start_with?("#{PROJECT_ROOT}/vendor")
|
|
|
|
|
|
|
|
raise "Warning: #{message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 23:53:32 +03:00
|
|
|
# Configure Rails Environment
|
2020-07-02 09:39:25 +03:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
|
|
|
|
2022-02-15 17:51:48 +03:00
|
|
|
require "view_component/deprecation"
|
|
|
|
ViewComponent::Deprecation.behavior = :silence
|
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
require File.expand_path("sandbox/config/environment.rb", __dir__)
|
2019-08-16 20:09:33 +03:00
|
|
|
require "rails/test_help"
|
|
|
|
|
2022-12-12 20:50:53 +03:00
|
|
|
require "capybara/cuprite"
|
|
|
|
|
2023-10-19 23:11:01 +03:00
|
|
|
# Rails registers its own driver named "cuprite" which will overwrite the one we
|
|
|
|
# register here. Avoid the problem by registering the driver with a distinct name.
|
|
|
|
Capybara.register_driver(:vc_cuprite) do |app|
|
2022-12-12 20:50:53 +03:00
|
|
|
# Add the process_timeout option to prevent failures due to the browser
|
|
|
|
# taking too long to start up.
|
|
|
|
Capybara::Cuprite::Driver.new(app, {process_timeout: 60, timeout: 30})
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reduce extra logs produced by puma booting up
|
|
|
|
Capybara.server = :puma, {Silent: true}
|
|
|
|
# Increase the max wait time to appease test failures due to timeouts.
|
2023-02-17 22:49:57 +03:00
|
|
|
Capybara.default_max_wait_time = 30
|
2022-12-12 20:50:53 +03:00
|
|
|
|
2022-08-15 19:33:36 +03:00
|
|
|
def with_config_option(option_name, new_value, config_entrypoint: Rails.application.config.view_component)
|
|
|
|
old_value = config_entrypoint.public_send(option_name)
|
2024-01-02 20:22:25 +03:00
|
|
|
config_entrypoint.public_send(:"#{option_name}=", new_value)
|
2022-08-09 16:37:16 +03:00
|
|
|
yield
|
|
|
|
ensure
|
2024-01-02 20:22:25 +03:00
|
|
|
config_entrypoint.public_send(:"#{option_name}=", old_value)
|
2022-08-09 16:37:16 +03:00
|
|
|
end
|
|
|
|
|
2021-02-17 04:36:40 +03:00
|
|
|
# Sets custom preview paths in tests.
|
|
|
|
#
|
|
|
|
# @param new_value [Array<String>] List of preview paths
|
|
|
|
# @yield Test code to run
|
|
|
|
# @return [void]
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_preview_paths(new_value, &block)
|
|
|
|
with_config_option(:preview_paths, new_value, &block)
|
2021-02-17 04:36:40 +03:00
|
|
|
end
|
|
|
|
|
2020-05-06 07:17:44 +03:00
|
|
|
def with_preview_route(new_value)
|
|
|
|
old_value = Rails.application.config.view_component.preview_route
|
|
|
|
Rails.application.config.view_component.preview_route = new_value
|
|
|
|
app.reloader.reload!
|
|
|
|
yield
|
2022-08-09 16:37:16 +03:00
|
|
|
ensure
|
2020-05-06 07:17:44 +03:00
|
|
|
Rails.application.config.view_component.preview_route = old_value
|
|
|
|
app.reloader.reload!
|
2019-12-09 19:07:12 +03:00
|
|
|
end
|
2020-06-11 06:32:00 +03:00
|
|
|
|
2020-10-04 04:33:05 +03:00
|
|
|
def with_preview_controller(new_value)
|
|
|
|
old_value = Rails.application.config.view_component.preview_controller
|
|
|
|
Rails.application.config.view_component.preview_controller = new_value
|
|
|
|
app.reloader.reload!
|
|
|
|
yield
|
2022-08-09 16:37:16 +03:00
|
|
|
ensure
|
2020-10-04 04:33:05 +03:00
|
|
|
Rails.application.config.view_component.preview_controller = old_value
|
|
|
|
app.reloader.reload!
|
|
|
|
end
|
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_custom_component_path(new_value, &block)
|
|
|
|
with_config_option(:view_component_path, new_value, &block)
|
2021-06-21 23:53:54 +03:00
|
|
|
end
|
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_custom_component_parent_class(new_value, &block)
|
|
|
|
with_config_option(:component_parent_class, new_value, &block)
|
2021-09-17 20:30:48 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_application_component_class
|
2022-07-06 21:28:24 +03:00
|
|
|
Object.const_set(:ApplicationComponent, Class.new(Object))
|
2021-09-17 20:30:48 +03:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Object.send(:remove_const, :ApplicationComponent)
|
|
|
|
end
|
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_generate_option(config_option, value)
|
|
|
|
old_value = Rails.application.config.view_component.generate[config_option]
|
|
|
|
Rails.application.config.view_component.generate[config_option] = value
|
2022-01-10 22:47:59 +03:00
|
|
|
yield
|
|
|
|
ensure
|
2022-08-09 16:37:16 +03:00
|
|
|
Rails.application.config.view_component.generate[config_option] = old_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_generate_sidecar(enabled, &block)
|
|
|
|
with_generate_option(:sidecar, enabled, &block)
|
2022-01-10 22:47:59 +03:00
|
|
|
end
|
|
|
|
|
2023-05-15 20:20:42 +03:00
|
|
|
def with_template_caching
|
|
|
|
old_cache_template_loading = ActionView::Base.cache_template_loading
|
|
|
|
ActionView::Base.cache_template_loading = true
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActionView::Base.cache_template_loading = old_cache_template_loading
|
|
|
|
end
|
|
|
|
|
2021-05-10 17:52:19 +03:00
|
|
|
def with_new_cache
|
2022-07-06 21:28:24 +03:00
|
|
|
old_cache = ViewComponent::CompileCache.cache
|
|
|
|
ViewComponent::CompileCache.cache = Set.new
|
|
|
|
old_cache_template_loading = ActionView::Base.cache_template_loading
|
|
|
|
ActionView::Base.cache_template_loading = false
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActionView::Base.cache_template_loading = old_cache_template_loading
|
|
|
|
ViewComponent::CompileCache.cache = old_cache
|
2021-05-10 17:52:19 +03:00
|
|
|
end
|
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def without_template_annotations(&block)
|
2021-05-10 17:58:56 +03:00
|
|
|
if ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)
|
|
|
|
old_value = ActionView::Base.annotate_rendered_view_with_filenames
|
|
|
|
ActionView::Base.annotate_rendered_view_with_filenames = false
|
2021-12-17 01:44:25 +03:00
|
|
|
app.reloader.reload! if defined?(app)
|
2021-05-10 17:58:56 +03:00
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
with_new_cache(&block)
|
2021-05-10 17:58:56 +03:00
|
|
|
|
|
|
|
ActionView::Base.annotate_rendered_view_with_filenames = old_value
|
2021-12-17 01:44:25 +03:00
|
|
|
app.reloader.reload! if defined?(app)
|
2021-05-10 17:58:56 +03:00
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
2021-05-10 17:52:19 +03:00
|
|
|
end
|
|
|
|
|
2020-06-11 06:32:00 +03:00
|
|
|
def modify_file(file, content)
|
|
|
|
filename = Rails.root.join(file)
|
|
|
|
old_content = File.read(filename)
|
|
|
|
begin
|
|
|
|
File.open(filename, "wb+") { |f| f.write(content) }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
File.open(filename, "wb+") { |f| f.write(old_content) }
|
|
|
|
end
|
|
|
|
end
|
2020-04-18 06:05:53 +03:00
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_default_preview_layout(layout, &block)
|
|
|
|
with_config_option(:default_preview_layout, layout, &block)
|
2020-04-18 06:05:53 +03:00
|
|
|
end
|
2020-08-01 18:14:57 +03:00
|
|
|
|
2022-08-09 16:37:16 +03:00
|
|
|
def with_render_monkey_patch_config(enabled, &block)
|
|
|
|
with_config_option(:render_monkey_patch_enabled, enabled, &block)
|
2020-08-01 18:14:57 +03:00
|
|
|
end
|
2022-01-06 00:41:17 +03:00
|
|
|
|
2024-09-11 23:43:58 +03:00
|
|
|
def with_compiler_development_mode(mode)
|
|
|
|
previous_mode = ViewComponent::Compiler.development_mode
|
|
|
|
ViewComponent::Compiler.development_mode = mode
|
2022-01-06 00:41:17 +03:00
|
|
|
yield
|
|
|
|
ensure
|
2024-09-11 23:43:58 +03:00
|
|
|
ViewComponent::Compiler.development_mode = previous_mode
|
2022-01-06 00:41:17 +03:00
|
|
|
end
|
2024-01-04 22:25:54 +03:00
|
|
|
|
|
|
|
def capture_warnings(&block)
|
|
|
|
[].tap do |warnings|
|
|
|
|
Kernel.stub(:warn, ->(msg) { warnings << msg }) do
|
|
|
|
block.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-09-26 00:48:23 +03:00
|
|
|
|
|
|
|
def assert_allocations(count_map, &block)
|
|
|
|
trace = AllocationStats.trace(&block)
|
|
|
|
total = trace.allocations.all.size
|
|
|
|
count = count_map[RUBY_VERSION]
|
|
|
|
|
|
|
|
assert_equal count, total, "Expected #{count} allocations, got #{total} allocations for Ruby #{RUBY_VERSION}"
|
|
|
|
end
|