Raise when ViewComponent causes a warning (#1352)

* Raise when ViewComponent causes a warning

This adds support for a new env variable, `RAISE_ON_WARNING` which when
set to true causes warnings emitted in the test environment to raise
an exception.

This also enables RAISE_ON_WARNING in CI.

* Fix warning raised in slotable_v2 module

This removes a call to `ruby2_keywords` that emits a warning.

* Fix warning due to uninitialized global buffer

* Support vendor path

* Fix failure now that the test fails properly

* Add changelog entry

* Update docs/CHANGELOG.md

* Update docs/CHANGELOG.md

Co-authored-by: Joel Hawksley <joel@hawksley.org>
This commit is contained in:
Blake Williams 2022-05-11 10:06:40 -04:00 коммит произвёл GitHub
Родитель dcd84fd584
Коммит ef816df0b0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 20 добавлений и 2 удалений

1
.github/workflows/ci.yml поставляемый
Просмотреть файл

@ -63,6 +63,7 @@ jobs:
bundle update
bundle exec rake
env:
RAISE_ON_WARNING: 1
MEASURE_COVERAGE: true
RAILS_VERSION: ${{ matrix.rails_version }}
VIEW_COMPONENT_USE_GLOBAL_OUTPUT_BUFFER: ${{ matrix.output_buffer == 'global_buffer' && 'true' || 'false' }}

Просмотреть файл

@ -9,6 +9,11 @@ title: Changelog
## main
* Resolve warning in slots API.
* Raise in the test environment when ViewComponent code emits a warning.
*Blake Williams*
## 2.54.0
* Add `with_*` slot API for defining slots. Note: we plan to deprecate the non `with_*` API for slots in an upcoming release.

Просмотреть файл

@ -70,7 +70,7 @@ module ViewComponent
@view_context = view_context
self.__vc_original_view_context ||= view_context
@output_buffer = ActionView::OutputBuffer.new unless @global_buffer_in_use
@output_buffer = ActionView::OutputBuffer.new unless defined?(@global_buffer_in_use) && @global_buffer_in_use
@lookup_context ||= view_context.lookup_context

Просмотреть файл

@ -155,7 +155,6 @@ module ViewComponent
set_slot(slot_name, nil, **args, &block)
end
end
ruby2_keywords(:"with_#{slot_name}") if respond_to?(:ruby2_keywords, true)
# Instantiates and and adds multiple slots forwarding the first
# argument to each slot constructor

Просмотреть файл

@ -16,6 +16,19 @@ require "pp"
require "pathname"
require "minitest/autorun"
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
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"