From c317deb2dccea3c727c59d658ec36b36c53389f3 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 4 Jul 2024 15:48:28 -0400 Subject: [PATCH] Load custom plugins when running `spoom deadcode` Signed-off-by: Alexandre Terrasa --- lib/spoom/cli/deadcode.rb | 1 + test/spoom/cli/deadcode_test.rb | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/spoom/cli/deadcode.rb b/lib/spoom/cli/deadcode.rb index 6a0ec22c..a7ff0252 100644 --- a/lib/spoom/cli/deadcode.rb +++ b/lib/spoom/cli/deadcode.rb @@ -74,6 +74,7 @@ def deadcode(*paths) end plugin_classes = Spoom::Deadcode.plugins_from_gemfile_lock(context) + plugin_classes.merge(Spoom::Deadcode.load_custom_plugins(context)) if options[:show_plugins] $stderr.puts "\nLoaded #{blue(plugin_classes.size.to_s)} plugins\n" plugin_classes.each do |plugin| diff --git a/test/spoom/cli/deadcode_test.rb b/test/spoom/cli/deadcode_test.rb index b1cb9227..0acabc06 100644 --- a/test/spoom/cli/deadcode_test.rb +++ b/test/spoom/cli/deadcode_test.rb @@ -267,6 +267,46 @@ def test_deadcode_show_plugins_default assert(result.status) end + def test_deadcode_load_custom_plugins + @project.write!("lib/foo.rb", <<~RB) + def foo; end + def foo_1; end + def foo_2; end + RB + + @project.write!(".spoom/deadcode/plugins/foo.rb", <<~RB) + class Foo < Spoom::Deadcode::Plugins::Base + def on_define_method(definition) + index.ignore(definition) if definition.name =~ /^foo_/ + end + end + RB + + result = @project.spoom("deadcode --show-plugins --no-color") + assert_equal(<<~ERR, result.err) + Collecting files... + + Loaded 7 plugins + Spoom::Deadcode::Plugins::Namespaces + Spoom::Deadcode::Plugins::Ruby + Spoom::Deadcode::Plugins::Minitest + Spoom::Deadcode::Plugins::Rake + Spoom::Deadcode::Plugins::Sorbet + Spoom::Deadcode::Plugins::Thor + Foo + + Indexing 1 files... + Analyzing 3 definitions against 0 references... + + Candidates: + foo lib/foo.rb:1:0-1:12 + + Found 1 dead candidates + ERR + assert_empty(result.out) + refute(result.status) + end + def test_deadcode_parse_erb @project.write!("view.erb", <<~ERB) <%= foo do %>