Skip to content

Commit

Permalink
Merge pull request #569 from Shopify/at-fix-deadcode-path-exclusion
Browse files Browse the repository at this point in the history
Fix dead code path exclusion when passing custom paths
  • Loading branch information
Morriar authored Jun 20, 2024
2 parents b4ad777 + 748946a commit 4557128
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/spoom/cli/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions test/spoom/cli/deadcode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4557128

Please sign in to comment.