routes_coverage 0.5.2 → 0.6.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
  SHA256:
3
- metadata.gz: 8d242f3f02df2a36688c5237bd56ee2dbae55a6184ea0c8a299ef41fc7c3e42f
4
- data.tar.gz: 87fc2c2c37dba09b129fedd1792242f5a94e64651bf8ef744171196d0e166dcc
3
+ metadata.gz: b697ab555d0362fa18dae5161266efbeb99eff4c1f276f7d06039952a6fc2f7c
4
+ data.tar.gz: 5e0fbeb4d54958bfb46f307c42ef5a54ceec49af49f699943881c6a665cca251
5
5
  SHA512:
6
- metadata.gz: 1e3ac519c76267812ef5daf380315895a44e1ae12e396918d299546c04a23359cde49da0728fb0e6f9bda58014d20dd78bf981f08eaa1864e78e3185b9643582
7
- data.tar.gz: d10ea82c9e2847bcb280a87a775c391c1c00de44e8e631ae45f9b43d19ca38e075399262c43584c509c069effc8bc2b836cced2c90f3787539d6e8217ee78e64
6
+ metadata.gz: c810351185cd998b8370c357d28bb06736e1c3de4202e12aab641b7ab22ed182f9e9f2b31a4cefe54dd9cdd52fe6a20a0187b088649e6652107b9edfce6bad39
7
+ data.tar.gz: e7af918230387e897334154962115d6d6f9b6e38d1e870bd5070529cf88d8fc2708cf6c57baf1d9f1f66b14beacd62f5eb1ae21d33de41ed279e671e4dcf7449
data/README.md CHANGED
@@ -37,6 +37,7 @@ RSpec.configure do |config|
37
37
  config.routes_coverage.perform_report = ENV['ROUTES_COVERAGE'] # only generate report if env var is set
38
38
 
39
39
  config.routes_coverage.exclude_put_fallbacks = true # exclude non-hit PUT-requests where a matching PATCH exists
40
+ config.routes_coverage.include_from_controller_tests = true # include results from controller tests
40
41
  config.routes_coverage.exclude_patterns << %r{PATCH /reqs} # excludes all requests matching regex
41
42
  config.routes_coverage.exclude_namespaces << 'somenamespace' # excludes /somenamespace/*
42
43
 
@@ -70,7 +71,9 @@ or
70
71
  RoutesCoverage.settings.format = :full_text
71
72
  ```
72
73
 
73
-
74
+ Note that coverage from `include_from_controller_tests` (disabled by default) is not a true routes coverage.
75
+ Rounting is not tested in controller tests (which are deprecated in Rails 5),
76
+ but sometimes you may already have a lot of controller tests and an intent to improve green-path/business level coverage
74
77
 
75
78
  ## Development
76
79
 
@@ -9,27 +9,9 @@ module RoutesCoverage
9
9
  def call(original_env)
10
10
  # router changes env/request during recognition so need a copy:
11
11
  env = original_env.dup
12
- req = ::Rails.application.routes.request_class.new env
13
- ::Rails.application.routes.router.recognize(req) do |route, parameters5, parameters4|
14
- parameters = parameters5 || parameters4
15
- dispatcher = route.app
16
- if dispatcher.respond_to?(:dispatcher?)
17
- req.path_parameters = parameters
18
- dispatcher = nil unless dispatcher.matches?(req) # && dispatcher.dispatcher?
19
- else # rails < 4.2
20
- dispatcher = route.app
21
- req.env['action_dispatch.request.path_parameters'] =
22
- (env['action_dispatch.request.path_parameters'] || {}).merge(parameters)
23
- while dispatcher.is_a?(ActionDispatch::Routing::Mapper::Constraints)
24
- dispatcher = (dispatcher.app if dispatcher.matches?(env))
25
- end
26
- end
27
- next unless dispatcher
12
+ req = ::Rails.application.routes.request_class.new(env)
13
+ RoutesCoverage._touch_request(req)
28
14
 
29
- RoutesCoverage._touch_route(route)
30
- # there may be multiple matching routes - we should match only first
31
- break
32
- end
33
15
  # TODO: detect 404s? and maybe other route errors?
34
16
  @app.call(original_env)
35
17
  end
@@ -90,20 +90,20 @@ module RoutesCoverage
90
90
  attr_reader :all_routes, :route_hit_counts
91
91
 
92
92
  def expected_routes
93
- return @expected_routes if @expected_routes
94
-
95
- filter_regex = Regexp.union(@settings.exclude_patterns)
96
- namespaces_regex = Regexp.union(@settings.exclude_namespaces.map { |n| %r{^/#{n}} })
93
+ @expected_routes ||= begin
94
+ filter_regex = Regexp.union(@settings.exclude_patterns)
95
+ namespaces_regex = Regexp.union(@settings.exclude_namespaces.map { |n| %r{^/#{n}} })
96
+
97
+ routes_groups = all_routes.group_by do |r|
98
+ (
99
+ ("#{r.verb.to_s[8..-3]} #{r.path.spec}".strip =~ filter_regex) ||
100
+ (r.path.spec.to_s =~ namespaces_regex)
101
+ ).present?
102
+ end
97
103
 
98
- routes_groups = all_routes.group_by do |r|
99
- (
100
- ("#{r.verb.to_s[8..-3]} #{r.path.spec}".strip =~ filter_regex) ||
101
- (r.path.spec.to_s =~ namespaces_regex)
102
- ).present?
104
+ @excluded_routes = routes_groups[true] || []
105
+ routes_groups[false] || []
103
106
  end
104
-
105
- @excluded_routes = routes_groups[true] || []
106
- @expected_routes = routes_groups[false] || []
107
107
  end
108
108
 
109
109
  def pending_routes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RoutesCoverage
4
- VERSION = "0.5.2"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -10,17 +10,43 @@ require "routes_coverage/formatters/full_text"
10
10
  require "routes_coverage/formatters/html"
11
11
 
12
12
  module RoutesCoverage
13
+ module ActionControllerTestCase
14
+ def process(action, *args)
15
+ return super unless RoutesCoverage.settings.include_from_controller_tests
16
+
17
+ super.tap { RoutesCoverage._touch_request(@request) }
18
+ end
19
+ end
20
+
21
+ module ActionControllerTestCaseKvargs
22
+ def process(action, **kvargs)
23
+ return super unless RoutesCoverage.settings.include_from_controller_tests
24
+
25
+ super.tap { RoutesCoverage._touch_request(@request) }
26
+ end
27
+ end
28
+
13
29
  class Railtie < ::Rails::Railtie
14
30
  railtie_name :routes_coverage
15
31
 
16
32
  initializer "request_coverage.inject_test_middleware" do
17
33
  ::Rails.application.middleware.use RoutesCoverage::Middleware if RoutesCoverage.enabled?
34
+
35
+ ActiveSupport.on_load(:action_controller_test_case) do |klass|
36
+ if Rails.version >= '5.1'
37
+ klass.prepend RoutesCoverage::ActionControllerTestCaseKvargs
38
+ else
39
+ klass.prepend RoutesCoverage::ActionControllerTestCase
40
+ end
41
+ end
18
42
  end
19
43
  end
20
44
 
21
45
  class Settings
22
46
  attr_reader :exclude_patterns, :exclude_namespaces, :groups
23
- attr_accessor :exclude_put_fallbacks, :perform_report, :minimum_coverage, :round_precision, :format
47
+ attr_accessor :perform_report, :format, :minimum_coverage, :round_precision,
48
+ :exclude_put_fallbacks,
49
+ :include_from_controller_tests
24
50
 
25
51
  def initialize
26
52
  @exclude_patterns = []
@@ -160,6 +186,30 @@ module RoutesCoverage
160
186
  end.to_h
161
187
  end
162
188
 
189
+ # NB: router changes env/request during recognition
190
+ def self._touch_request(req)
191
+ ::Rails.application.routes.router.recognize(req) do |route, parameters5, parameters4|
192
+ parameters = parameters5 || parameters4
193
+ dispatcher = route.app
194
+ if dispatcher.respond_to?(:dispatcher?)
195
+ req.path_parameters = parameters
196
+ dispatcher = nil unless dispatcher.matches?(req) # && dispatcher.dispatcher?
197
+ else # rails < 4.2
198
+ dispatcher = route.app
199
+ req.env['action_dispatch.request.path_parameters'] =
200
+ (env['action_dispatch.request.path_parameters'] || {}).merge(parameters)
201
+ while dispatcher.is_a?(ActionDispatch::Routing::Mapper::Constraints)
202
+ dispatcher = (dispatcher.app if dispatcher.matches?(env))
203
+ end
204
+ end
205
+ next unless dispatcher
206
+
207
+ RoutesCoverage._touch_route(route)
208
+ # there may be multiple matching routes - we should match only first
209
+ break
210
+ end
211
+ end
212
+
163
213
  def self._touch_route(route)
164
214
  reset! unless route_hit_count
165
215
  route_hit_count[route] += 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routes_coverage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasily Fedoseyev