Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dead code path exclusion when passing custom paths #569

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading