Skip to content

Commit

Permalink
Add using file path as a configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
stephsachrajda committed Jan 11, 2024
1 parent b931f8f commit d6dd524
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/deprecation_toolkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions lib/deprecation_toolkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Proc.new 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
12 changes: 1 addition & 11 deletions lib/deprecation_toolkit/read_write_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions test/deprecation_toolkit/behaviors/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ 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

Expand Down Expand Up @@ -123,16 +128,16 @@ 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
Expand Down

0 comments on commit d6dd524

Please sign in to comment.