Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of minitest file paths and file names #95

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) 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
7 changes: 1 addition & 6 deletions lib/deprecation_toolkit/read_write_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +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.class.name.underscore
end
path = Configuration.deprecation_file_path_format.call(test)

Pathname(deprecation_folder).join("#{path}.yml")
end
Expand Down
33 changes: 29 additions & 4 deletions test/deprecation_toolkit/behaviors/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ class RecordTest < ActiveSupport::TestCase
end
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

assert_deprecations_recorded("Foo", "Bar", path: "test/deprecation_toolkit/behaviors/record_test.yml") do
deprecator.warn("Foo")
deprecator.warn("Bar")

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

test ".trigger record deprecations for proc path" do
Configuration.deprecation_path = proc do
File.join(@deprecation_path, "prefix")
Expand Down Expand Up @@ -106,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
Expand Down