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 unused views report #560

Merged
merged 1 commit into from
Oct 19, 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
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