Skip to content

Commit

Permalink
Revert Improve deadcode plugin for Minitest #579
Browse files Browse the repository at this point in the history
Since we only index `.rb` files, the relationship between
`ActiveJob::TestCase` and `ActiveSupport::TestCase` and other intermediate
classes that may only exist in the RBI files isn't seen by Spoom...

Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Jul 30, 2024
1 parent 1a612ab commit 84bb2df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
26 changes: 9 additions & 17 deletions lib/spoom/deadcode/plugins/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
45 changes: 7 additions & 38 deletions test/spoom/deadcode/plugins/minitest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 84bb2df

Please sign in to comment.