diff --git a/lib/spoom/deadcode/plugins/minitest.rb b/lib/spoom/deadcode/plugins/minitest.rb index 36dfec67..9097b449 100644 --- a/lib/spoom/deadcode/plugins/minitest.rb +++ b/lib/spoom/deadcode/plugins/minitest.rb @@ -8,28 +8,20 @@ class Minitest < Base extend T::Sig ignore_classes_named(/Test$/) - ignore_classes_inheriting_from("Minitest::Test") - MINITEST_METHODS = T.let( - Set.new([ - "after_all", - "around", - "around_all", - "before_all", - "setup", - "teardown", - ]), - T::Set[String], + ignore_methods_named( + "after_all", + "around", + "around_all", + "before_all", + "setup", + "teardown", ) sig { override.params(definition: Model::Method).void } def on_define_method(definition) - return unless definition.name.start_with?("test_") || MINITEST_METHODS.include?(definition.name) - - owner = definition.owner - return unless owner.is_a?(Model::Class) - - @index.ignore(definition) if ignored_subclass?(owner) + file = definition.location.file + @index.ignore(definition) if file.match?(%r{test/.*test\.rb$}) && definition.name.match?(/^test_/) end sig { override.params(send: Send).void } diff --git a/test/spoom/deadcode/plugins/minitest_test.rb b/test/spoom/deadcode/plugins/minitest_test.rb index 1b7009de..3fc8ce57 100644 --- a/test/spoom/deadcode/plugins/minitest_test.rb +++ b/test/spoom/deadcode/plugins/minitest_test.rb @@ -10,63 +10,32 @@ module Plugins class MinitestTest < TestWithProject include Test::Helpers::DeadcodeHelper - def test_ignores_test_class_based_on_name - @project.write!("foo_test.rb", <<~RB) - class FooTest < NotAMinitestClass; end - RB - - assert_ignored(index_with_plugins, "FooTest") - end - - def test_ignore_minitest_classes_based_on_superclasses + def test_ignore_minitest_classes_based_on_name @project.write!("foo.rb", <<~RB) class C1Test < Minitest::Test; end RB @project.write!("test/foo.rb", <<~RB) - class C2Test < ::Minitest::Test; end + class C2Test < SomeTest; end RB @project.write!("test/foo_test.rb", <<~RB) class C3Test < ::Minitest::Test; end class C4Test < C3Test; end + class C5Test; end RB index = index_with_plugins assert_ignored(index, "C1Test") assert_ignored(index, "C2Test") - assert_alive(index, "C3Test") + assert_alive(index, "C3Test") # used as C4Test superclass assert_ignored(index, "C4Test") + assert_ignored(index, "C5Test") end - def test_does_not_ignore_minitest_methods_if_not_in_minitest_test_subclass + def test_ignore_minitest_methods @project.write!("test/foo_test.rb", <<~RB) - class FooTest < Test - def after_all; end - def around; end - def around_all; end - def before_all; end - def setup; end - def teardown; end - def test_something; end - - def some_other_test; end - end - RB - - index = index_with_plugins - refute_ignored(index, "after_all") - refute_ignored(index, "around") - refute_ignored(index, "around_all") - refute_ignored(index, "before_all") - refute_ignored(index, "setup") - refute_ignored(index, "teardown") - refute_ignored(index, "test_something") - end - - def test_ignore_minitest_methods_if_in_minitest_test_subclass - @project.write!("test/foo_test.rb", <<~RB) - class FooTest < Minitest::Test + class FooTest def after_all; end def around; end def around_all; end