traceroute 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3b6798196323becbcafa21da7194fc6f277b76c
4
- data.tar.gz: 5bad050aa0eeaac3caafec59897e5accabec0cb6
3
+ metadata.gz: 27509e75b8c7d78e3be8cabd152d8163c42dab05
4
+ data.tar.gz: 04b04e317cacc030b204c4a03d9d4673b51ffc76
5
5
  SHA512:
6
- metadata.gz: c7f1d5534e1c1faf86556feb9a070da0fbe80f56e6515bfec3c77a184d9ea62c77239468f9a5d41919090d5d0761eb1039919f28251dc221672aaeeec93aa5b0
7
- data.tar.gz: a703004cd3574d8d6f09a791eb6443e27326ee891c63d2790aa0b0a7a6edd5e37d19929f85d380f18367d88f82d83ef7884716cd3c2da93f0893b94d17e78db5
6
+ metadata.gz: 57a81990feedba666ccc07194640eb900fc8a705f26eef6119911691a02c685d01641dec81fe615f6ef895862aea7c46337aa1defd48924e87c2895b48b53a32
7
+ data.tar.gz: 0bc718b3c168f82c07e9206aa328c83f0f42375c22ecc9a127df6f3016d8ff0dc19669b87b475bb4d363b81ba02d703ea8a0f1384018a70119fe367cc6cd4fc5
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *.log
@@ -1,6 +1,6 @@
1
1
  = Traceroute
2
2
 
3
- A Rake task that helps you find dead routes and unused actions in your Rails 3/4 app.
3
+ A Rake task that helps you find dead routes and unused actions in your Rails 3+ app.
4
4
 
5
5
 
6
6
  == Features
@@ -10,7 +10,7 @@ This Rake task investigates your Rails application's routes definition, then sho
10
10
 
11
11
  == Supported versions
12
12
 
13
- * Ruby 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1 (trunk)
13
+ * Ruby 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1, 2.2 (trunk)
14
14
 
15
15
  * Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x
16
16
 
@@ -29,6 +29,9 @@ Then bundle:
29
29
  Just run the following command in your Rails app directory.
30
30
  % rake traceroute
31
31
 
32
+ If you want the rake task to fail when errors are found.
33
+
34
+ % FAIL_ON_ERROR=1 rake traceroute
32
35
 
33
36
  == What's gonna happen then?
34
37
 
@@ -75,6 +78,10 @@ OMG super helpful, isn't it?
75
78
  A: I'm afraid you're using the wrong operating system.
76
79
 
77
80
 
81
+ == Notes
82
+
83
+ Routes prefix with `rails/` are ignored. These are generated by rails in development mode. We don't want to fail the build on these.
84
+
78
85
  == Questions, Feedback
79
86
 
80
87
  Feel free to message me on Github (amatsuda) or Twitter (@a_matsuda) ☇3☇3☇3
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
@@ -1,35 +1,11 @@
1
1
  desc 'Prints out unused routes and unreachable action methods'
2
2
  task :traceroute => :environment do
3
- Rails.application.eager_load!
4
- ::Rails::InfoController rescue NameError
5
- ::Rails::WelcomeController rescue NameError
6
- ::Rails::MailersController rescue NameError
7
- Rails.application.reload_routes!
3
+ traceroute = Traceroute.new Rails.application
4
+ traceroute.load_everything!
8
5
 
9
- routes = Rails.application.routes.routes.reject {|r| r.name.nil? && r.requirements.blank?}
6
+ defined_action_methods = traceroute.defined_action_methods
10
7
 
11
- if Rails.application.config.respond_to?(:assets)
12
- exclusion_regexp = %r{^#{Rails.application.config.assets.prefix}}
13
-
14
- routes.reject! do |route|
15
- path = (defined?(ActionDispatch::Journey::Route) || defined?(Journey::Route)) ? route.path.spec.to_s : route.path
16
- path =~ exclusion_regexp
17
- end
18
- end
19
-
20
- defined_action_methods = ActionController::Base.descendants.map do |controller|
21
- controller.action_methods.reject {|a| (a =~ /\A(_conditional)?_callback_/) || (a == '_layout_from_proc')}.map do |action|
22
- "#{controller.controller_path}##{action}"
23
- end
24
- end.flatten
25
-
26
- routed_actions = routes.map do |r|
27
- if r.requirements[:controller].blank? && r.requirements[:action].blank? && (r.path == '/:controller(/:action(/:id(.:format)))')
28
- %Q["#{r.path}" This is a legacy wild controller route that's not recommended for RESTful applications.]
29
- else
30
- "#{r.requirements[:controller]}##{r.requirements[:action]}"
31
- end
32
- end
8
+ routed_actions = traceroute.routed_actions
33
9
 
34
10
  unused_routes = routed_actions - defined_action_methods
35
11
  unreachable_action_methods = defined_action_methods - routed_actions
@@ -38,4 +14,8 @@ task :traceroute => :environment do
38
14
  unused_routes.each {|route| puts " #{route}"}
39
15
  puts "Unreachable action methods (#{unreachable_action_methods.count}):"
40
16
  unreachable_action_methods.each {|action| puts " #{action}"}
17
+
18
+ unless (unused_routes.empty? && unreachable_action_methods.empty?) || ENV['FAIL_ON_ERROR'].blank?
19
+ fail "Unused routes or unreachable action methods detected."
20
+ end
41
21
  end
@@ -1,7 +1,59 @@
1
- module Traceroute
1
+ class Traceroute
2
+ VERSION = Gem.loaded_specs['traceroute'].version.to_s
2
3
  class Railtie < ::Rails::Railtie
3
4
  rake_tasks do
4
5
  load File.join(File.dirname(__FILE__), 'tasks/traceroute.rake')
5
6
  end
6
7
  end
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def load_everything!
14
+ @app.eager_load!
15
+ ::Rails::InfoController rescue NameError
16
+ ::Rails::WelcomeController rescue NameError
17
+ ::Rails::MailersController rescue NameError
18
+ @app.reload_routes!
19
+
20
+ Rails::Engine.subclasses.each(&:eager_load!)
21
+ end
22
+
23
+ def defined_action_methods
24
+ ActionController::Base.descendants.map do |controller|
25
+ controller.action_methods.reject {|a| (a =~ /\A(_conditional)?_callback_/) || (a == '_layout_from_proc')}.map do |action|
26
+ "#{controller.controller_path}##{action}"
27
+ end
28
+ end.flatten.reject {|r| r.start_with? 'rails/'}
29
+ end
30
+
31
+ def routed_actions
32
+ routes.map do |r|
33
+ if r.requirements[:controller].blank? && r.requirements[:action].blank? && (r.path == '/:controller(/:action(/:id(.:format)))')
34
+ %Q["#{r.path}" This is a legacy wild controller route that's not recommended for RESTful applications.]
35
+ else
36
+ "#{r.requirements[:controller]}##{r.requirements[:action]}"
37
+ end
38
+ end.reject {|r| r.start_with? 'rails/'}
39
+ end
40
+
41
+ private
42
+ def routes
43
+ routes = @app.routes.routes.reject {|r| r.name.nil? && r.requirements.blank?}
44
+
45
+ routes.reject! {|r| r.app.is_a?(ActionDispatch::Routing::Mapper::Constraints) && r.app.app.respond_to?(:call)}
46
+
47
+ routes.reject! {|r| r.app.is_a?(ActionDispatch::Routing::Redirect)}
48
+
49
+ if @app.config.respond_to?(:assets)
50
+ exclusion_regexp = %r{^#{@app.config.assets.prefix}}
51
+
52
+ routes.reject! do |route|
53
+ path = (defined?(ActionDispatch::Journey::Route) || defined?(Journey::Route)) ? route.path.spec.to_s : route.path
54
+ path =~ exclusion_regexp
55
+ end
56
+ end
57
+ routes
58
+ end
7
59
  end
@@ -0,0 +1,27 @@
1
+ require 'rails'
2
+ require 'action_controller/railtie'
3
+
4
+ require 'traceroute'
5
+
6
+ module DummyApp
7
+ class Application < Rails::Application
8
+ config.root = File.expand_path('..', __FILE__)
9
+ config.eager_load = false
10
+ config.secret_key_base = '1234567890'
11
+ end
12
+ end
13
+
14
+ class ApplicationController < ActionController::Base; end
15
+
16
+ class UsersController < ApplicationController
17
+ def index; end
18
+ def index2; end
19
+ def show; end
20
+ end
21
+
22
+ module Admin; class ShopsController < ApplicationController
23
+ def index; end
24
+ def create; end
25
+ end; end
26
+
27
+ DummyApp::Application.initialize!
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'rails'
4
+ require 'traceroute'
5
+ require_relative 'app'
@@ -0,0 +1,100 @@
1
+ require_relative 'test_helper'
2
+
3
+ class TracerouteTest < Minitest::Test
4
+ def setup
5
+ @traceroute = Traceroute.new Rails.application
6
+ @traceroute.load_everything!
7
+ end
8
+
9
+ def test_defined_action_methods
10
+ assert_equal ['users#index', 'users#index2', 'users#show', 'admin/shops#index', 'admin/shops#create'], @traceroute.defined_action_methods
11
+ end
12
+
13
+ def test_routed_actions
14
+ assert_empty @traceroute.routed_actions
15
+ end
16
+ end
17
+
18
+ class RoutedActionsTest < Minitest::Test
19
+ def setup
20
+ DummyApp::Application.routes.draw do
21
+ resources :users, :only => [:index, :show, :new, :create]
22
+
23
+ namespace :admin do
24
+ resources :shops, :only => :index
25
+ end
26
+ end
27
+ @traceroute = Traceroute.new Rails.application
28
+ end
29
+
30
+ def teardown
31
+ DummyApp::Application.routes.clear!
32
+ end
33
+
34
+ def test_routed_actions
35
+ assert_equal ['admin/shops#index', 'users#index', 'users#show', 'users#new', 'users#create'].sort, @traceroute.routed_actions.sort
36
+ end
37
+ end
38
+
39
+ class TracerouteRakeTests < Minitest::Test
40
+ def setup
41
+ require 'rake'
42
+ load "./lib/tasks/traceroute.rake"
43
+ end
44
+
45
+ def test_dont_fail_when_envvar_not_set
46
+ traceroute = Traceroute.new Rails.application
47
+ traceroute.load_everything!
48
+
49
+ ENV['FAIL_ON_ERROR'] = ""
50
+ Rake::Task[:traceroute].execute
51
+ end
52
+
53
+ def test_rake_task_fails_when_unreachable_action_method_detected
54
+ traceroute = Traceroute.new Rails.application
55
+ traceroute.load_everything!
56
+
57
+ begin
58
+ ENV['FAIL_ON_ERROR']="1"
59
+ Rake::Task[:traceroute].execute
60
+ rescue => e
61
+ assert_includes e.message, "Unused routes or unreachable action methods detected."
62
+ end
63
+ end
64
+
65
+ def test_rake_task_fails_when_unused_route_detected
66
+ DummyApp::Application.routes.draw do
67
+ resources :users, :only => [:index, :show, :new, :create] do
68
+ member do
69
+ get :index2
70
+ end
71
+ end
72
+
73
+ namespace :admin do
74
+ resources :shops, :only => [:index, :create]
75
+ end
76
+
77
+ namespace :rails do
78
+ resources :mailers, only: ["index"] do
79
+ member do
80
+ get :preview
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ traceroute = Traceroute.new Rails.application
87
+
88
+ begin
89
+ ENV['FAIL_ON_ERROR'] = "1"
90
+ Rake::Task[:traceroute].execute
91
+ rescue => e
92
+ assert_includes e.message, "Unused routes or unreachable action methods detected."
93
+ end
94
+ end
95
+
96
+ def teardown
97
+ Rake::Task.clear
98
+ DummyApp::Application.routes.clear!
99
+ end
100
+ end
@@ -1,10 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "traceroute/version"
4
3
 
5
4
  Gem::Specification.new do |s|
6
5
  s.name = "traceroute"
7
- s.version = Traceroute::VERSION
6
+ s.version = '0.5.0'
8
7
  s.platform = Gem::Platform::RUBY
9
8
  s.authors = ['Akira Matsuda']
10
9
  s.email = ['ronnie@dio.jp']
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traceroute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-07 00:00:00.000000000 Z
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.0
27
27
  description: This Rake task investigates the application's routes definition, then
@@ -33,13 +33,15 @@ extensions: []
33
33
  extra_rdoc_files:
34
34
  - README.rdoc
35
35
  files:
36
- - ".gitignore"
36
+ - .gitignore
37
37
  - Gemfile
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - lib/tasks/traceroute.rake
41
41
  - lib/traceroute.rb
42
- - lib/traceroute/version.rb
42
+ - test/app.rb
43
+ - test/test_helper.rb
44
+ - test/traceroute_test.rb
43
45
  - traceroute.gemspec
44
46
  homepage: https://github.com/amatsuda/traceroute
45
47
  licenses:
@@ -51,19 +53,22 @@ require_paths:
51
53
  - lib
52
54
  required_ruby_version: !ruby/object:Gem::Requirement
53
55
  requirements:
54
- - - ">="
56
+ - - '>='
55
57
  - !ruby/object:Gem::Version
56
58
  version: '0'
57
59
  required_rubygems_version: !ruby/object:Gem::Requirement
58
60
  requirements:
59
- - - ">="
61
+ - - '>='
60
62
  - !ruby/object:Gem::Version
61
63
  version: '0'
62
64
  requirements: []
63
65
  rubyforge_project: traceroute
64
- rubygems_version: 2.2.2
66
+ rubygems_version: 2.4.5
65
67
  signing_key:
66
68
  specification_version: 4
67
69
  summary: A Rake task that helps you find the dead routes and actions for your Rails
68
70
  3 app
69
- test_files: []
71
+ test_files:
72
+ - test/app.rb
73
+ - test/test_helper.rb
74
+ - test/traceroute_test.rb
@@ -1,3 +0,0 @@
1
- module Traceroute
2
- VERSION = '0.4.0'
3
- end