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 f647b4f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
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
1 change: 1 addition & 0 deletions lib/deprecation_toolkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class Configuration
config_accessor(:deprecation_path) { "test/deprecations" }
config_accessor(:test_runner) { :minitest }
config_accessor(:warnings_treated_as_deprecation) { [] }
config_accessor(:minitest_file_name_format) { FileNameFormats::ClassName }
end
end
11 changes: 11 additions & 0 deletions lib/deprecation_toolkit/file_name_formats/class_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module DeprecationToolkit
module FileNameFormats
class ClassName
def self.file_path(test)
test.class.name.underscore
end
end
end
end
18 changes: 18 additions & 0 deletions lib/deprecation_toolkit/file_name_formats/file_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module DeprecationToolkit
module FileNameFormats
class FilePath
def self.file_path(test)
full_path = Kernel.const_source_location(test.class.name)[0]
repository_root = File.expand_path(Dir.pwd)

puts full_path.sub(/^#{Regexp.escape(repository_root)}/, '')

full_path.sub(/^#{Regexp.escape(repository_root)}/, '')
rescue NameError
test.class.name.underscore
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 @@ -52,12 +52,7 @@ def recorded_deprecations_path(test)
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
Configuration.minitest_file_name_format.file_path(test)
end

Pathname(deprecation_folder).join("#{path}.yml")
Expand Down
25 changes: 13 additions & 12 deletions test/deprecation_toolkit/behaviors/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,18 @@ 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")
test ".trigger record deprecations records using file path when configuration is set to use file path" do
Configuration.minitest_file_name_format = DeprecationToolkit::FileNameFormats::FilePath

trigger_deprecation_toolkit_behavior
end
end
assert_deprecations_recorded("Foo", "Bar") do

private
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.minitest_file_name_format = DeprecationToolkit::FileNameFormats::ClassName
end

test ".trigger record deprecations for proc path" do
Expand Down Expand Up @@ -132,7 +129,11 @@ def assert_deprecations_recorded(*deprecation_triggered, to: @deprecation_path)
end

def recorded_deprecations(to: @deprecation_path)
YAML.load_file("#{to}/deprecation_toolkit/behaviors/record_test.yml").fetch(name)
if Configuration.minitest_file_name_format == DeprecationToolkit::FileNameFormats::ClassName
YAML.load_file("#{to}/deprecation_toolkit/behaviors/record_test.yml").fetch(name)
else
YAML.load_file("#{to}/test/deprecation_toolkit/behaviors/record_test.yml").fetch(name)
end
end
end
end
Expand Down

0 comments on commit f647b4f

Please sign in to comment.