Skip to content

Commit

Permalink
Merge pull request #560 from fatkodima/fix-unused-views
Browse files Browse the repository at this point in the history
Fix unused views report
  • Loading branch information
danmayer authored Oct 19, 2024
2 parents b9d98e7 + 9f81098 commit 0577b10
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/coverband.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "redis"
require "coverband/version"
require "coverband/at_exit"
require "coverband/configuration"
require "coverband/utils/relative_file_converter"
require "coverband/utils/absolute_file_converter"
require "coverband/adapters/base"
Expand All @@ -27,6 +26,7 @@
require "coverband/integrations/background"
require "coverband/integrations/background_middleware"
require "coverband/integrations/rack_server_check"
require "coverband/configuration"

Coverband::Adapters::RedisStore = Coverband::Adapters::HashRedisStore if ENV["COVERBAND_HASH_REDIS_STORE"]

Expand Down
5 changes: 4 additions & 1 deletion lib/coverband/collectors/view_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ class ViewTracker < AbstractTracker

REPORT_ROUTE = "views_tracker"
TITLE = "Views"
VIEWS_PATTERNS = %w[.erb$ .haml$ .slim$]

def initialize(options = {})
@project_directory = File.expand_path(Coverband.configuration.root)
@roots = options.fetch(:roots) { Coverband.configuration.all_root_patterns }
@roots = @roots.split(",") if @roots.is_a?(String)

super

@ignore_patterns -= VIEWS_PATTERNS.map { |ignore_str| Regexp.new(ignore_str) }
end

def railtie!
Expand Down Expand Up @@ -84,7 +87,7 @@ def unused_keys(used_views = nil)
recently_used_views = used_keys.keys
unused_views = all_keys - recently_used_views
# since layouts don't include format we count them used if they match with ANY formats
unused_views.reject { |view| view.include?("/layouts/") && recently_used_views.any? { |used_view| view.include?(used_view) } }
unused_views = unused_views.reject { |view| view.include?("/layouts/") && recently_used_views.any? { |used_view| view.include?(used_view) } }
unused_views.reject { |view| @ignore_patterns.any? { |pattern| view.match?(pattern) } }
end

Expand Down
2 changes: 1 addition & 1 deletion lib/coverband/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Configuration
# Heroku when building assets runs code from a dynamic directory
# /tmp was added to avoid coverage from /tmp/build directories during
# heroku asset compilation
IGNORE_DEFAULTS = %w[vendor/ .erb$ .slim$ /tmp internal:prelude db/schema.rb]
IGNORE_DEFAULTS = %w[vendor/ /tmp internal:prelude db/schema.rb] + Collectors::ViewTracker::VIEWS_PATTERNS

# Add in missing files which were never loaded
# we need to know what all paths to check for unloaded files
Expand Down
8 changes: 4 additions & 4 deletions test/coverband/collectors/view_tracker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ def setup
Coverband::Collectors::ViewTracker.expects(:supported_version?).returns(true)
store = fake_store
file_path = "#{File.expand_path(Coverband.configuration.root)}/file"
target = [file_path, "not_used"]
target = [file_path, "not_used.html.erb"]
tracker = Coverband::Collectors::ViewTracker.new(store: store, roots: "dir", target: target)
tracker.track_key(identifier: file_path)
tracker.save_report
assert_equal ["not_used"], tracker.unused_keys
assert_equal ["not_used.html.erb"], tracker.unused_keys
end

test "report hides partials marked in ignore config" do
Coverband::Collectors::ViewTracker.expects(:supported_version?).returns(true)
store = fake_store
file_path = "#{File.expand_path(Coverband.configuration.root)}/app/views/anything/ignore_me.html.erb"
target = [file_path, "not_used"]
target = [file_path, "not_used.html.erb"]
tracker = Coverband::Collectors::ViewTracker.new(store: store, roots: "dir", target: target)
tracker.track_key(identifier: file_path)
tracker.save_report
assert_equal ["not_used"], tracker.unused_keys
assert_equal ["not_used.html.erb"], tracker.unused_keys
assert_equal [], tracker.used_keys.keys
end

Expand Down
7 changes: 4 additions & 3 deletions test/coverband/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setup

test "ignore works with equal" do
Coverband::Collectors::Coverage.instance.reset_instance
expected = ["vendor/", ".erb$", ".slim$", "/tmp", "internal:prelude", "db/schema.rb", "config/environments"].map { |str| Regexp.new(str) }
expected = ["vendor/", "/tmp", "internal:prelude", "db/schema.rb", ".erb$", ".haml$", ".slim$", "config/environments"].map { |str| Regexp.new(str) }
assert_equal expected, Coverband.configuration.ignore
end

Expand All @@ -28,11 +28,12 @@ def setup
end
Coverband::Collectors::Coverage.instance.reset_instance
expected = ["vendor/",
".erb$",
".slim$",
"/tmp",
"internal:prelude",
"db/schema.rb",
".erb$",
".haml$",
".slim$",
"config/environments",
"config/initializers"].map { |str| Regexp.new(str) }
assert_equal expected, Coverband.configuration.ignore
Expand Down

0 comments on commit 0577b10

Please sign in to comment.