diff --git a/lib/spoom/cli/deadcode.rb b/lib/spoom/cli/deadcode.rb index c5e8518c..e6d1d869 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, @@ -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 2fdb116d..b1cb9227 100644 --- a/test/spoom/cli/deadcode_test.rb +++ b/test/spoom/cli/deadcode_test.rb @@ -31,6 +31,112 @@ 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_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