From 95a6148d3921d9455629189e4048f059767cb11e Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 20 Jun 2024 11:30:11 -0400 Subject: [PATCH 1/2] Exclude more paths by default Signed-off-by: Alexandre Terrasa --- lib/spoom/cli/deadcode.rb | 2 +- test/spoom/cli/deadcode_test.rb | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/spoom/cli/deadcode.rb b/lib/spoom/cli/deadcode.rb index c5e8518c..f68310a4 100644 --- a/lib/spoom/cli/deadcode.rb +++ b/lib/spoom/cli/deadcode.rb @@ -24,7 +24,7 @@ class Deadcode < Thor desc: "Allowed mime types" option :exclude, type: :array, - default: ["vendor/", "sorbet/"], + default: ["vendor/", "sorbet/", "tmp/", "log/", "node_modules/"], aliases: :x, desc: "Exclude paths" option :show_files, diff --git a/test/spoom/cli/deadcode_test.rb b/test/spoom/cli/deadcode_test.rb index 2fdb116d..70c45b36 100644 --- a/test/spoom/cli/deadcode_test.rb +++ b/test/spoom/cli/deadcode_test.rb @@ -31,6 +31,78 @@ def baz; end assert(result.status) end + def test_deadcode_from_exec_path_with_default_excludes + @project.write!("lib/foo.rb", <<~RUBY) + def foo; end + RUBY + + @project.write!("vendor/foo.rb", <<~RUBY) + def ignored1; end + RUBY + + @project.write!("sorbet/foo.rb", <<~RUBY) + def ignored2; end + RUBY + + @project.write!("tmp/foo.rb", <<~RUBY) + def ignored3; end + RUBY + + @project.write!("log/foo.rb", <<~RUBY) + def ignored4; end + RUBY + + @project.write!("node_modules/log.rb", <<~RUBY) + def ignored5; end + RUBY + + result = @project.spoom("deadcode --no-color") + assert_equal(<<~ERR, result.err) + Collecting files... + Indexing 1 files... + Analyzing 1 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_from_exec_path_with_custom_excludes + @project.write!("lib/foo.rb", <<~RUBY) + def foo; end + RUBY + + @project.write!("vendor/foo.rb", <<~RUBY) + def bar; end + RUBY + + @project.write!("tmp/foo.rb", <<~RUBY) + def ignored1; end + RUBY + + @project.write!("log/foo.rb", <<~RUBY) + def ignored2; end + RUBY + + result = @project.spoom("deadcode --no-color --exclude tmp/ log/") + assert_equal(<<~ERR, result.err) + Collecting files... + Indexing 2 files... + Analyzing 2 definitions against 0 references... + + Candidates: + bar vendor/foo.rb:1:0-1:12 + foo lib/foo.rb:1:0-1:12 + + Found 2 dead candidates + ERR + assert_empty(result.out) + refute(result.status) + end def test_deadcode_with_deadcode @project.write!("lib/foo.rb", <<~RUBY) def foo; end From 748946a24858babdd4adc9aadb721505e78c6540 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 20 Jun 2024 11:31:02 -0400 Subject: [PATCH 2/2] Fix exclude path composition when using custom paths Signed-off-by: Alexandre Terrasa --- lib/spoom/cli/deadcode.rb | 4 +++- test/spoom/cli/deadcode_test.rb | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/spoom/cli/deadcode.rb b/lib/spoom/cli/deadcode.rb index f68310a4..e6d1d869 100644 --- a/lib/spoom/cli/deadcode.rb +++ b/lib/spoom/cli/deadcode.rb @@ -58,7 +58,9 @@ def deadcode(*paths) collector = FileCollector.new( allow_extensions: options[:allowed_extensions], allow_mime_types: options[:allowed_mime_types], - exclude_patterns: options[:exclude].map { |p| Pathname.new(File.join(exec_path, p, "**")).cleanpath.to_s }, + exclude_patterns: paths.flat_map do |path| + options[:exclude].map { |excluded| Pathname.new(File.join(path, excluded, "**")).cleanpath.to_s } + end, ) collector.visit_paths(paths) files = collector.files.sort diff --git a/test/spoom/cli/deadcode_test.rb b/test/spoom/cli/deadcode_test.rb index 70c45b36..b1cb9227 100644 --- a/test/spoom/cli/deadcode_test.rb +++ b/test/spoom/cli/deadcode_test.rb @@ -103,6 +103,40 @@ def ignored2; end assert_empty(result.out) refute(result.status) end + + def test_deadcode_from_path_arguments_with_custom_excludes + @project.write!("app/lib/foo.rb", <<~RUBY) + def foo; end + RUBY + + @project.write!("app/vendor/foo.rb", <<~RUBY) + def bar; end + RUBY + + @project.write!("app/tmp/foo.rb", <<~RUBY) + def ignored1; end + RUBY + + @project.write!("app/log/foo.rb", <<~RUBY) + def ignored2; end + RUBY + + result = @project.spoom("deadcode --no-color app/ --exclude tmp/ log/") + assert_equal(<<~ERR, result.err) + Collecting files... + Indexing 2 files... + Analyzing 2 definitions against 0 references... + + Candidates: + bar app/vendor/foo.rb:1:0-1:12 + foo app/lib/foo.rb:1:0-1:12 + + Found 2 dead candidates + ERR + assert_empty(result.out) + refute(result.status) + end + def test_deadcode_with_deadcode @project.write!("lib/foo.rb", <<~RUBY) def foo; end