From 0738865081b67a253c9f7f438637e36e15ffb825 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 --- lib/deprecation_toolkit.rb | 5 ++++ lib/deprecation_toolkit/configuration.rb | 1 + .../file_name_formats/class_name.rb | 11 ++++++++ .../file_name_formats/file_path.rb | 14 ++++++++++ lib/deprecation_toolkit/read_write_helper.rb | 7 +---- .../behaviors/record_test.rb | 28 +++++++++++-------- 6 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 lib/deprecation_toolkit/file_name_formats/class_name.rb create mode 100644 lib/deprecation_toolkit/file_name_formats/file_path.rb 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..1435275 100644 --- a/lib/deprecation_toolkit/configuration.rb +++ b/lib/deprecation_toolkit/configuration.rb @@ -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 diff --git a/lib/deprecation_toolkit/file_name_formats/class_name.rb b/lib/deprecation_toolkit/file_name_formats/class_name.rb new file mode 100644 index 0000000..a1ecba0 --- /dev/null +++ b/lib/deprecation_toolkit/file_name_formats/class_name.rb @@ -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 diff --git a/lib/deprecation_toolkit/file_name_formats/file_path.rb b/lib/deprecation_toolkit/file_name_formats/file_path.rb new file mode 100644 index 0000000..f1df266 --- /dev/null +++ b/lib/deprecation_toolkit/file_name_formats/file_path.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module DeprecationToolkit + module FileNameFormats + class FilePath + def self.file_path(test) + puts Kernel.const_source_location(test.class.name)[0].sub(%r{^.*/(test/)}, '\1').sub(/\.[^.]+$/, "") + Kernel.const_source_location(test.class.name)[0].sub(%r{^.*/(test/)}, '\1').sub(/\.[^.]+$/, "") + rescue NameError + test.class.name.underscore + end + end + end +end diff --git a/lib/deprecation_toolkit/read_write_helper.rb b/lib/deprecation_toolkit/read_write_helper.rb index 30e7c58..c353a91 100644 --- a/lib/deprecation_toolkit/read_write_helper.rb +++ b/lib/deprecation_toolkit/read_write_helper.rb @@ -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") diff --git a/test/deprecation_toolkit/behaviors/record_test.rb b/test/deprecation_toolkit/behaviors/record_test.rb index 7fd5540..0a1e83d 100644 --- a/test/deprecation_toolkit/behaviors/record_test.rb +++ b/test/deprecation_toolkit/behaviors/record_test.rb @@ -31,21 +31,21 @@ 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 + puts Kernel.const_source_location(test.class.name)[0].sub(%r{^.*/(test/)}, '\1').sub(/\.[^.]+$/, "") + + + 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 @@ -132,7 +132,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