2019-08-16 20:09:33 +03:00
|
|
|
# frozen_string_literal: true
|
2021-07-02 00:01:38 +03:00
|
|
|
|
2020-06-22 16:31:07 +03:00
|
|
|
require "simplecov"
|
2020-07-27 23:46:40 +03:00
|
|
|
require "simplecov-console"
|
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
|
|
|
|
command_name "rails#{ENV["RAILS_VERSION"]}-ruby#{ENV["RUBY_VERSION"]}" if ENV["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 "pp"
|
|
|
|
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-08-09 16:37:16 +03:00
|
|
|
def with_config_option(option_name, new_value)
|
|
|
|
old_value = Rails.application.config.view_component.public_send(option_name)
|
|
|
|
Rails.application.config.view_component.public_send("#{option_name}=", new_value)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Rails.application.config.view_component.public_send("#{option_name}=", old_value)
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
def with_compiler_mode(mode)
|
|
|
|
previous_mode = ViewComponent::Compiler.mode
|
|
|
|
ViewComponent::Compiler.mode = mode
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ViewComponent::Compiler.mode = previous_mode
|
|
|
|
end
|