Skip to content

Commit

Permalink
Merge pull request #579 from Shopify/at-deadcode-minitest
Browse files Browse the repository at this point in the history
Improve deadcode plugin for Minitest
  • Loading branch information
Morriar authored Jul 11, 2024
2 parents 768f88a + b0f50e1 commit b78cd0e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
40 changes: 29 additions & 11 deletions lib/spoom/deadcode/plugins/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@ module Plugins
class Minitest < Base
extend T::Sig

ignore_classes_named(/Test$/)

ignore_methods_named(
"after_all",
"around",
"around_all",
"before_all",
"setup",
"teardown",
ignore_classes_inheriting_from("Minitest::Test")

MINITEST_METHODS = T.let(
Set.new([
"after_all",
"around",
"around_all",
"before_all",
"setup",
"teardown",
]),
T::Set[String],
)

sig { override.params(definition: Model::Method).void }
def on_define_method(definition)
file = definition.location.file
@index.ignore(definition) if file.match?(%r{test/.*test\.rb$}) && definition.name.match?(/^test_/)
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)
end

sig { override.params(send: Send).void }
def on_send(send)
case send.name
when "assert_predicate", "refute_predicate"
name = send.args[1]&.slice
return unless name

@index.reference_method(name.delete_prefix(":"), send.location)
end
end
end
end
Expand Down
63 changes: 55 additions & 8 deletions test/spoom/deadcode/plugins/minitest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,57 @@ module Plugins
class MinitestTest < TestWithProject
include Test::Helpers::DeadcodeHelper

def test_ignore_minitest_classes_based_on_name
def test_ignore_minitest_classes_based_on_superclasses
@project.write!("foo.rb", <<~RB)
class C1Test < Minitest::Test; end
RB

@project.write!("test/foo.rb", <<~RB)
class C2Test < SomeTest; end
class C2Test < ::Minitest::Test; end
RB

@project.write!("test/foo_test.rb", <<~RB)
class C3Test; end
class C4; end
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_ignored(index, "C3Test")
refute_ignored(index, "C4")
assert_alive(index, "C3Test")
assert_ignored(index, "C4Test")
refute_ignored(index, "C5Test")
end

def test_ignore_minitest_methods
def test_does_not_ignore_minitest_methods_if_not_in_minitest_test_subclass
@project.write!("test/foo_test.rb", <<~RB)
class FooTest
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
def after_all; end
def around; end
def around_all; end
Expand All @@ -57,6 +84,26 @@ def some_other_test; end
refute_ignored(index, "some_other_test")
end

def test_ignore_minitest_predicates
@project.write!("test/foo_test.rb", <<~RB)
class FooTest
def alive1; end
def alive2; end
def dead; end
def test_something
assert_predicate(foo, :alive1)
refute_predicate(foo, :alive2)
end
end
RB

index = index_with_plugins
assert_alive(index, "alive1")
assert_alive(index, "alive2")
assert_dead(index, "dead")
end

private

sig { returns(Deadcode::Index) }
Expand Down

0 comments on commit b78cd0e

Please sign in to comment.