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

Report unused ignore lines in config #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions lib/tasks/traceroute.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@ task :traceroute => :environment do
unreachable_action_methods.each {|action| puts " #{action}"}
end

unused_action_checks = traceroute.unused_ignored_unreachable_action_methods
unless unused_action_checks.empty?
puts "Unused action method ignores present (#{unused_action_checks.count}):"
unused_action_checks.each {|action| puts " #{action}"}
end

unused_route_checks = traceroute.unused_ignored_unused_routes
unless unused_route_checks.empty?
puts "Unused route ignores present (#{unused_route_checks.count}):"
unused_route_checks.each {|route| puts " #{route}"}
end

if ENV['FAIL_ON_ERROR'] && ((!ENV['UNREACHABLE_ACTION_METHODS_ONLY'] && unused_routes.any?) || (!ENV['UNUSED_ROUTES_ONLY'] && unreachable_action_methods.any?))
fail "Unused routes or unreachable action methods detected."
end

if ENV['FAIL_ON_ERROR'] && (unused_action_checks.any? || unused_route_checks.any?)
fail "Unused routes or unreachable action ignore lines detected in config."
end
end

namespace :traceroute do
Expand Down
37 changes: 33 additions & 4 deletions lib/traceroute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def initialize(app)
@app = app

@ignored_unreachable_actions = []
@ignored_unused_routes = [/^\/cable$/]
@ignored_unused_routes = default_unused_routes.dup

@ignored_unused_routes << %r{^#{@app.config.assets.prefix}} if @app.config.respond_to? :assets
@used_ignored_unreachable_actions = []
@used_ignored_unused_routes = []

config_filename = %w(.traceroute.yaml .traceroute.yml .traceroute).detect {|f| File.exist?(f)}
if config_filename && (config = YAML.load_file(config_filename))
Expand Down Expand Up @@ -50,14 +51,22 @@ def unreachable_action_methods
defined_action_methods - routed_actions
end

def unused_ignored_unused_routes
@ignored_unused_routes - @used_ignored_unused_routes - default_unused_routes
end

def unused_ignored_unreachable_action_methods
@ignored_unreachable_actions - @used_ignored_unreachable_actions
end

def defined_action_methods
@defined_action_methods ||= [ActionController::Base, (ActionController::API if defined?(ActionController::API))].compact.map do |klass|
klass.descendants.map do |controller|
controller.action_methods.reject {|a| (a =~ /\A(_conditional)?_callback_/) || (a == '_layout_from_proc')}.map do |action|
"#{controller.controller_path}##{action}"
end
end.flatten
end.flatten.reject {|r| @ignored_unreachable_actions.any? { |m| r.match(m) } }
end.compact.flatten.reject {|r| check_match_and_store_used(r, @ignored_unreachable_actions, @used_ignored_unreachable_actions) }
end

def routed_actions
Expand All @@ -71,7 +80,7 @@ def routed_actions
else
((String === r.path) && r.path.to_s) || r.path.spec.to_s # unknown routes
end
end.compact.flatten.reject {|r| @ignored_unused_routes.any? { |m| r.match(m) } }
end.compact.flatten.reject { |r| check_match_and_store_used(r, @ignored_unused_routes, @used_ignored_unused_routes) }
end

def routes
Expand All @@ -96,4 +105,24 @@ def collect_routes(routes)

routes
end

private

def check_match_and_store_used(item, ignores, used)
ignores.any? { |m|
if item.match(m)
used << m
true
end
}
end

def default_unused_routes
@default_unused_routes ||=
begin
defaults = [/^\/cable$/]
defaults << %r{^#{@app.config.assets.prefix}} if @app.config.respond_to? :assets
defaults.freeze
end
end
end
12 changes: 12 additions & 0 deletions test/yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def setup
File.open ".traceroute.yaml", "w" do |file|
file.puts 'ignore_unreachable_actions:'
file.puts '- ^jasmine_rails\/'
file.puts '- foo_unused'
file.puts 'ignore_unused_routes:'
file.puts '- ^users'
file.puts '- bar_unused'
end

DummyApp::Application.routes.draw do
Expand Down Expand Up @@ -58,6 +60,16 @@ def test_unreachable_actions_are_ignored
def test_used_routes_are_ignored
assert_routed_actions 'admin/shops#index', 'api/books#index'
end

def test_unused_ignored_unused_routes_are_reported
@traceroute.routed_actions # Trigger the check
assert_equal @traceroute.unused_ignored_unused_routes, [/bar_unused/]
end

def test_unused_ignored_unreachable_action_methods_are_reported
@traceroute.defined_action_methods # Trigger the check
assert_equal @traceroute.unused_ignored_unreachable_action_methods, [/foo_unused/]
end
end

class EmptyFileTest < Minitest::Test
Expand Down