Skip to content

Commit 66e3826

Browse files
committed
More rubocop-suggested refactoring
1 parent c1b6604 commit 66e3826

21 files changed

+125
-92
lines changed

.rubocop.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ require:
55

66
AllCops:
77
NewCops: enable
8+
# 2.0 was dropped in rubocop 0.50
9+
# TargetRubyVersion: 2.0
810
Exclude:
911
- gemfiles/*
1012

1113
Rails/RakeEnvironment: { Enabled: false }
1214
Bundler/OrderedGems: { Enabled: false }
1315

16+
Minitest/MultipleAssertions: { Enabled: false }
17+
1418
# maybe later
1519
Style/StringLiterals: { Enabled: false }
1620
Style/Documentation: { Enabled: false }
21+
22+
# we allow older rubies
23+
Gemspec/RequiredRubyVersion: { Enabled: false }
24+
25+
# rails app is for tests
26+
Rails/RenderInline: { Enabled: false }
27+
Rails/ApplicationController: { Enabled: false }
28+
29+
Metrics/BlockLength:
30+
Exclude:
31+
- spec/*
32+
33+
Metrics/MethodLength: { Max: 25 }
34+
Metrics/AbcSize: { Enabled: false }
35+
Metrics/CyclomaticComplexity: { Max: 10 }
36+
Metrics/PerceivedComplexity: { Max: 10 }

lib/routes_coverage.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def self.perform_report
8484
put_route.verb == "PUT" # rails 5
8585
) &&
8686
put_route.name.nil? &&
87-
@@route_hit_count[put_route] == 0 &&
87+
@@route_hit_count[put_route].zero? &&
8888
all_routes.any? do |patch_route|
8989
(
9090
patch_route.verb == /^PATCH$/ ||
@@ -97,21 +97,18 @@ def self.perform_report
9797
end
9898
end
9999

100-
all_result = Result.new(
101-
all_routes,
102-
@@route_hit_count,
103-
settings
104-
)
100+
all_result = Result.new(all_routes, @@route_hit_count, settings)
105101

106102
groups = settings.groups.map do |group_name, matcher|
107103
group_routes = all_routes.select do |route|
108104
if matcher.respond_to?(:call)
109105
matcher.call(route)
110106
elsif matcher.is_a?(Hash)
111107
matcher.all? do |key, value|
112-
if key == :path
108+
case key
109+
when :path
113110
route.path.spec.to_s =~ value
114-
elsif key == :constraints
111+
when :constraints
115112
value.all? do |constraint_name, constraint_value|
116113
if constraint_value.present?
117114
route.constraints[constraint_name] && route.constraints[constraint_name].match?(constraint_value)
@@ -126,12 +123,7 @@ def self.perform_report
126123
end
127124
end
128125

129-
[group_name,
130-
Result.new(
131-
group_routes,
132-
@@route_hit_count.slice(group_routes),
133-
settings
134-
)]
126+
[group_name, Result.new(group_routes, @@route_hit_count.slice(group_routes), settings)]
135127
end.to_h
136128

137129
if groups.size > 1
@@ -151,7 +143,7 @@ def self.perform_report
151143
end
152144

153145
puts
154-
puts settings.formatter_class.new(all_result, groups, settings).format
146+
puts settings.formatter_class.new(all_result, groups, settings).format # rubocop:disable Rails/Output
155147
end
156148

157149
def self._touch_route(route)

lib/routes_coverage/formatters/full_text.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class FullText < SummaryText
66
class RouteFormatter
77
attr_reader :buffer
88

9-
def initialize(result = nil, _settings = nil, output_hits = false)
9+
def initialize(result = nil, _settings = nil, output_hits: false)
1010
@buffer = []
1111
@result = result
1212
@output_hits = output_hits
@@ -95,7 +95,7 @@ def hit_routes_details
9595
pending_routes = collect_routes(result.pending_routes)
9696

9797
<<~TXT
98-
#{routes_section(RouteFormatter.new(result, settings, true), 'Covered routes:', hit_routes)}
98+
#{routes_section(RouteFormatter.new(result, settings, output_hits: true), 'Covered routes:', hit_routes)}
9999
100100
#{routes_section(RouteFormatter.new(result, settings), 'Pending routes:', pending_routes)}
101101
TXT

lib/routes_coverage/middleware.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module RoutesCoverage
24
class Middleware
35
def initialize(app)

lib/routes_coverage/result.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def collect_all_routes
2222
def collect_routes(routes)
2323
routes.collect do |route|
2424
ActionDispatch::Routing::RouteWrapper.new(route)
25-
end.reject(&:internal?).collect do |route|
25+
end.reject(&:internal?).collect do |route| # rubocop:disable Style/MultilineBlockChain
2626
collect_engine_routes(route)
2727

2828
{ name: route.name,
@@ -40,7 +40,7 @@ def collect_routes(routes)
4040
require 'rails/application/route_inspector'
4141
class Inspector < Rails::Application::RouteInspector
4242
NEW_RAILS = false
43-
def collect_all_routes(routes)
43+
def collect_all_routes(routes) # rubocop:disable Lint/DuplicateMethods
4444
res = collect_routes(routes)
4545
@engines.each do |engine_name, engine_routes|
4646
res += engine_routes.map do |er|
@@ -50,7 +50,7 @@ def collect_all_routes(routes)
5050
res
5151
end
5252

53-
def collect_routes(routes)
53+
def collect_routes(routes) # rubocop:disable Lint/DuplicateMethods
5454
routes = routes.collect do |route|
5555
route_reqs = route.requirements
5656

@@ -96,10 +96,10 @@ def expected_routes
9696
namespaces_regex = Regexp.union(@settings.exclude_namespaces.map { |n| %r{^/#{n}} })
9797

9898
routes_groups = all_routes.group_by do |r|
99-
!!(
99+
(
100100
("#{r.verb.to_s[8..-3]} #{r.path.spec}".strip =~ filter_regex) ||
101101
(r.path.spec.to_s =~ namespaces_regex)
102-
)
102+
).present?
103103
end
104104

105105
@excluded_routes = routes_groups[true] || []

routes_coverage.gemspec

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
1+
# frozen_string_literal: true
2+
3+
lib = File.expand_path('lib', __dir__)
34
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
45
require 'routes_coverage/version'
56

@@ -9,20 +10,23 @@ Gem::Specification.new do |spec|
910
spec.authors = ["Vasily Fedoseyev"]
1011
spec.email = ["vasilyfedoseyev@gmail.com"]
1112

12-
spec.summary = %q{Provides coverage report for your rails routes}
13-
spec.description = %q{Generates coverage report for routes hit by your request/integration/feature tests including capybara ones}
13+
spec.summary = "Provides coverage report for your rails routes"
14+
spec.description = "Generates coverage report for routes hit by your request/integration/feature tests "\
15+
"including capybara ones"
1416
spec.homepage = "https://github.com/Vasfed/routes_coverage"
1517
spec.license = "MIT"
1618

17-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
19+
spec.required_ruby_version = ">= 2.0"
20+
21+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
1822
f.match(%r{^(test|spec|features|assets|bin|gemfiles)/}) ||
1923
f.start_with?('.') ||
2024
%w[Appraisals Gemfile Rakefile].include?(f)
2125
end
2226
spec.require_paths = ["lib"]
2327

28+
spec.add_development_dependency 'appraisal'
2429
spec.add_development_dependency "bundler", "~> 1.14"
25-
spec.add_development_dependency "rake", "~> 10.0"
2630
spec.add_development_dependency "minitest"
27-
spec.add_development_dependency 'appraisal'
31+
spec.add_development_dependency "rake", "~> 10.0"
2832
end

spec/fixtures/constraints_test.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12

23
ENV['RAILS_ENV'] = 'test'
34
require_relative 'dummy_app'
@@ -11,24 +12,23 @@ def self.matches?(request)
1112

1213
DummyApplication.routes.draw do
1314
get 'rec', to: 'dummy#index',
14-
constraints: -> (request) { request.params[:TYPE] == '1' },
15+
constraints: ->(request) { request.params[:TYPE] == '1' },
1516
as: :route1
1617
get 'rec', to: 'dummy#update',
17-
constraints: -> (request) { request.params[:TYPE] == '2' },
18+
constraints: ->(request) { request.params[:TYPE] == '2' },
1819
as: :route2
1920
get 'rec/:TYPE', to: 'dummy#current', constraints: CustomConstraintsClass, as: :route3
2021
end
2122

2223
DummyApplication.config.action_dispatch.show_exceptions = false
2324

24-
RoutesCoverage.configure do|config|
25+
RoutesCoverage.configure do |config|
2526
config.format = :full_text
2627
end
2728

28-
2929
class DummyRequestTest < ActionDispatch::IntegrationTest
3030
def test_coverage_enabled
31-
assert_equal RoutesCoverage.enabled?, true
31+
assert(RoutesCoverage.enabled?)
3232
end
3333

3434
def test_patch
@@ -45,6 +45,4 @@ def test_patch
4545
get '/rec?TYPE=4'
4646
end
4747
end
48-
49-
5048
end

spec/fixtures/dummy_app.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12

23
require 'rails'
34
require "action_controller/railtie"
@@ -7,7 +8,7 @@ class DummyApplication < Rails::Application
78
config.eager_load = false
89
config.secret_token = 'df5394d6c6fa8fdc95cf883df725b8b8' unless Rails.version >= '5'
910
config.secret_key_base = 'df5394d6c6fa8fdc95cf883df725b8b6'
10-
config.active_support.test_order = :sorted #if config.active_support.respond_to?(:test_order)
11+
config.active_support.test_order = :sorted # if config.active_support.respond_to?(:test_order)
1112

1213
config.active_support.deprecation = :stderr
1314
# config.action_dispatch.show_exceptions = false #raise instead

spec/fixtures/dummy_html.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require_relative 'dummy_test'
1+
# frozen_string_literal: true
22

3+
require_relative 'dummy_test'
34

45
RoutesCoverage.configure do |config|
56
config.format = :html

spec/fixtures/dummy_routes.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'dummy_app'
24
DummyApplication.routes.draw do
3-
resources :reqs, only:[:index, :update], controller: :dummy do
5+
resources :reqs, only: %i[index update], controller: :dummy do
46
post :current, on: :collection
57
end
68

79
namespace :somespace do
8-
resources :foo, only:[:index]
10+
resources :foo, only: [:index]
911
end
1012

1113
namespace :otherspace do
12-
resources :bar, only:[:index]
14+
resources :bar, only: [:index]
1315
end
1416

15-
resources :subdomain_route, only:[:index], constraints: { subdomain: 'subdomain' }
17+
resources :subdomain_route, only: [:index], constraints: { subdomain: 'subdomain' }
1618

1719
get 'r*catch_almost_all_route', to: 'dummy#not_found_error'
1820
end

0 commit comments

Comments
 (0)