From 432c15bf9b0d0f9e5df1ab0966eacd0281d4298d Mon Sep 17 00:00:00 2001 From: stephsachrajda Date: Tue, 9 Jan 2024 19:10:34 -0700 Subject: [PATCH] Add using file path as a configuration option --- CHANGELOG.md | 2 + README.md | 25 ++++++++++++ lib/deprecation_toolkit.rb | 5 +++ lib/deprecation_toolkit/configuration.rb | 9 +++++ lib/deprecation_toolkit/read_write_helper.rb | 12 +----- .../behaviors/record_test.rb | 38 +++++++++++-------- 6 files changed, 65 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 083f564..67fa316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## main (unreleased) +* [#95](https://github.com/Shopify/deprecation_toolkit/pull/95): Allow configuration of deprecation file paths and file names. + ## 2.0.4 (2023-11-20) * [#90](https://github.com/Shopify/deprecation_toolkit/pull/90) & [#93](https://github.com/Shopify/deprecation_toolkit/pull/93): Stop using deprecated behavior from Active Support. (@etiennebarrie) diff --git a/README.md b/README.md index f902d85..ec79ce3 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,31 @@ This setting accepts an array of regular expressions. To match on all warnings, DeprecationToolkit::Configuration.warnings_treated_as_deprecation = [//] ``` +### 🔨 `#DeprecationToolkit::Configuration#deprecation_file_path_format` + +DeprecationToolkit allows you to choose the file path format for deprecation files. + +For Minitest, it defaults to using class name so the following code would correspond to `#{deprecation_path}/deprecation_toolkit/behaviors/raise_test.yml` + +```ruby +module DeprecationToolkit + module Behaviors + class RaiseTest < ActiveSupport::TestCase + end + end +end +``` + +For rspec if defaults to the file location with spec removed. `/spec/models/user_spec.rb` would correspond to `/models/user.yml`. + +If you have a specific use case you can configure this with a custom format using a proc. The proc is called with an instance of the test. + +```ruby +Configuration.deprecation_file_path_format = -> (test) do + Kernel.const_source_location(test.class.name)[0].sub(%r{^./test/}, "").sub(/_test.rb$/, "") +end +``` + ## RSpec By default Deprecation Toolkit uses Minitest as its test runner. To use Deprecation Toolkit with RSpec you'll have to configure it. diff --git a/lib/deprecation_toolkit.rb b/lib/deprecation_toolkit.rb index 407394a..ebba9ea 100644 --- a/lib/deprecation_toolkit.rb +++ b/lib/deprecation_toolkit.rb @@ -18,6 +18,11 @@ module Behaviors autoload :CIRecordHelper, "deprecation_toolkit/behaviors/ci_record_helper" end + module FileNameFormats + autoload :ClassName, "deprecation_toolkit/file_name_formats/class_name" + autoload :FilePath, "deprecation_toolkit/file_name_formats/file_path" + end + class << self def add_notify_behavior notify = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] diff --git a/lib/deprecation_toolkit/configuration.rb b/lib/deprecation_toolkit/configuration.rb index 3814905..cfa2f2f 100644 --- a/lib/deprecation_toolkit/configuration.rb +++ b/lib/deprecation_toolkit/configuration.rb @@ -12,5 +12,14 @@ class Configuration config_accessor(:deprecation_path) { "test/deprecations" } config_accessor(:test_runner) { :minitest } config_accessor(:warnings_treated_as_deprecation) { [] } + config_accessor(:deprecation_file_path_format) do + proc do |test| + if DeprecationToolkit::Configuration.test_runner == :rspec + test.example_group.file_path.sub(%r{^./spec/}, "").sub(/_spec.rb$/, "") + else + test.class.name.underscore + end + end + end end end diff --git a/lib/deprecation_toolkit/read_write_helper.rb b/lib/deprecation_toolkit/read_write_helper.rb index 30e7c58..eeb1cc5 100644 --- a/lib/deprecation_toolkit/read_write_helper.rb +++ b/lib/deprecation_toolkit/read_write_helper.rb @@ -48,17 +48,7 @@ def recorded_deprecations_path(test) Configuration.deprecation_path end - path = - if DeprecationToolkit::Configuration.test_runner == :rspec - test.example_group.file_path.sub(%r{^./spec/}, "").sub(/_spec.rb$/, "") - else - test_path = test_location(test) - if test_path == "unknown" - test.class.name.underscore - else - File.dirname(test.class.name.underscore) + "/" + File.basename(test_path, ".*") - end - end + path = Configuration.deprecation_file_path_format.call(test) Pathname(deprecation_folder).join("#{path}.yml") end diff --git a/test/deprecation_toolkit/behaviors/record_test.rb b/test/deprecation_toolkit/behaviors/record_test.rb index 7fd5540..2ec55b5 100644 --- a/test/deprecation_toolkit/behaviors/record_test.rb +++ b/test/deprecation_toolkit/behaviors/record_test.rb @@ -31,20 +31,24 @@ class RecordTest < ActiveSupport::TestCase end end - class TestClass < RecordTest - test ".trigger record deprecations using correct filename in subclasses" do - assert_deprecations_recorded("Foo", "Bar") do - deprecator.warn("Foo") - deprecator.warn("Bar") - - trigger_deprecation_toolkit_behavior - end + test ".trigger record deprecations records using file path when configuration is set to use file path" do + Configuration.deprecation_file_path_format = ->(test) do + Kernel.const_source_location(test.class.name)[0].sub(%r{^.*/(test/)}, '\1').sub(/\.[^.]+$/, "") end - private + assert_deprecations_recorded("Foo", "Bar", path: "test/deprecation_toolkit/behaviors/record_test.yml") do + deprecator.warn("Foo") + deprecator.warn("Bar") - def recorded_deprecations(to: @deprecation_path) - YAML.load_file("#{to}/deprecation_toolkit/behaviors/record_test/record_test.yml").fetch(name) + trigger_deprecation_toolkit_behavior + end + ensure + Configuration.deprecation_file_path_format = proc do |test| + if DeprecationToolkit::Configuration.test_runner == :rspec + test.example_group.file_path.sub(%r{^./spec/}, "").sub(/_spec.rb$/, "") + else + test.class.name.underscore + end end end @@ -123,16 +127,20 @@ def example; end private - def assert_deprecations_recorded(*deprecation_triggered, to: @deprecation_path) + def assert_deprecations_recorded( + *deprecation_triggered, + to: @deprecation_path, + path: "deprecation_toolkit/behaviors/record_test.yml" + ) yield triggered = deprecation_triggered.map { |msg| "DEPRECATION WARNING: #{msg}" } - assert_equal(recorded_deprecations(to: to), triggered) + assert_equal(recorded_deprecations(to: to, path: path), triggered) end - def recorded_deprecations(to: @deprecation_path) - YAML.load_file("#{to}/deprecation_toolkit/behaviors/record_test.yml").fetch(name) + def recorded_deprecations(to: @deprecation_path, path: "deprecation_toolkit/behaviors/record_test.yml") + YAML.load_file("#{to}/#{path}").fetch(name) end end end