Skip to content

Commit 577e1b3

Browse files
authored
Merge pull request #381 from ahx/2.10.0
2.10.0 - Add Test::Configuration#skip_coverage to skip test coverage for specific paths + request methods and all responses - Deprecate setting minimum_coverage value. Use skip_response_coverage, ignored_unknown_status to configure coverage instead. - Update openapi_parameters to make parsing array query parameters more consistent. Now parsing empty array query parameter like `ids=&` or `ids&` both result in an empty array value (`[]`) instead of `nil` or `""`. - Fix Test::Coverage.result returning < 100 even if plan is fully covered
2 parents f90c85c + 8279ad0 commit 577e1b3

11 files changed

Lines changed: 74 additions & 19 deletions

File tree

.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
--format documentation
1+
--format progress
22
--color
33
--require spec_helper

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
## Unreleased
44

5+
## 2.10.0
6+
7+
- Add Test::Configuration#skip_coverage to skip test coverage for specific paths + request methods and all responses
8+
- Deprecate setting minimum_coverage value. Use skip_response_coverage, ignored_unknown_status to configure coverage instead.
59
- Update openapi_parameters to make parsing array query parameters more consistent.
6-
Now parsing an empty array query parameter like `ids=&` or `ids&` both result in an empty array value (`[]`) instead of `nil` or `""`.
10+
Now parsing empty array query parameter like `ids=&` or `ids&` both result in an empty array value (`[]`) instead of `nil` or `""`.
711
- Fix Test::Coverage.result returning < 100 even if plan is fully covered
812

913
## 2.9.3

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
openapi_first (2.9.3)
4+
openapi_first (2.10.0)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.6.1, < 2.0)

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,49 @@ Here is how to set it up:
7070
end
7171
```
7272
- Or inject a Module to wrap (prepend) the `call` method of your Rack app Class.
73-
73+
7474
NOTE: This is still work in progress. It works with basic Sinatra apps, but does not work with Hanami or Rails out of the box, yet. PRs welcome 🤗
75-
75+
7676
```ruby
7777
OpenapiFirst::Test.observe(MyApplication)
7878
```
7979
3. Run your tests. The Coverage feature will tell you about missing or invalid requests/responses.
8080

8181
(✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using `OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.
8282

83-
OpenapiFirst' request validation raises an error when a request is not defined. You can deactivate this during testing:
83+
### Configure test coverage
84+
85+
OpenapiFirst::Test raises an error when a request is not defined. You can deactivate this with:
8486

8587
```ruby
8688
OpenapiFirst::Test.setup do |test|
89+
# …
8790
test.ignore_unknown_requests = true
8891
end
8992
```
9093

94+
Exclude certain _responses_ from coverage with `skip_coverage`:
95+
96+
```ruby
97+
OpenapiFirst::Test.setup do |test|
98+
# …
99+
test.skip_response_coverage do |response_definition|
100+
response_definition.status == '5XX'
101+
end
102+
end
103+
```
104+
105+
Skip coverage for a request and all responses alltogether of a route with `skip_coverage`:
106+
107+
```ruby
108+
OpenapiFirst::Test.setup do |test|
109+
# …
110+
test.skip_coverage do |path, request_method|
111+
path == '/bookings/{bookingId}' && requests_method == 'DELETE'
112+
end
113+
end
114+
```
115+
91116
## Rack Middlewares
92117

93118
### Request validation

benchmarks/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
openapi_first (2.9.3)
4+
openapi_first (2.10.0)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.6.1, < 2.0)

lib/openapi_first/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def self.setup
4141

4242
configuration.registry.each { |name, oad| register(oad, as: name) }
4343
configuration.apps.each { |name, app| observe(app, api: name) }
44-
Coverage.start(skip_response: configuration.skip_response_coverage)
44+
Coverage.start(skip_response: configuration.skip_response_coverage, skip_route: configuration.skip_coverage)
4545

4646
if definitions.empty?
4747
raise NotRegisteredError,

lib/openapi_first/test/configuration.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def initialize
99
@coverage_formatter = Coverage::TerminalFormatter
1010
@coverage_formatter_options = {}
1111
@skip_response_coverage = nil
12+
@skip_coverage = nil
1213
@response_raise_error = true
1314
@ignored_unknown_status = [404]
1415
@report_coverage = true
@@ -29,9 +30,9 @@ def observe(app, api: :default)
2930
@apps[api] = app
3031
end
3132

32-
attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error, :minimum_coverage,
33+
attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error,
3334
:ignore_unknown_requests
34-
attr_reader :registry, :apps, :report_coverage, :ignored_unknown_status
35+
attr_reader :registry, :apps, :report_coverage, :ignored_unknown_status, :minimum_coverage
3536

3637
# Configure report coverage
3738
# @param [Boolean, :warn] value Whether to report coverage or just warn.
@@ -44,11 +45,24 @@ def report_coverage=(value)
4445
@report_coverage = value
4546
end
4647

48+
# @deprecated Use skip_response_coverage, ignored_unknown_status or skip_coverage to configure coverage
49+
def minimum_coverage=(value)
50+
warn 'OpenapiFirst::Test::Configuration#minimum_coverage= is deprecated. ' \
51+
'Use skip_response_coverage, ignored_unknown_status to configure coverage instead.'
52+
@minimum_coverage = value
53+
end
54+
4755
def skip_response_coverage(&block)
4856
return @skip_response_coverage unless block_given?
4957

5058
@skip_response_coverage = block
5159
end
60+
61+
def skip_coverage(&block)
62+
return @skip_coverage unless block_given?
63+
64+
@skip_coverage = block
65+
end
5266
end
5367
end
5468
end

lib/openapi_first/test/coverage.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class << self
1919

2020
def install = Test.install
2121

22-
def start(skip_response: nil)
22+
def start(skip_response: nil, skip_route: nil)
2323
@current_run = Test.definitions.values.to_h do |oad|
24-
plan = Plan.for(oad, skip_response:)
24+
plan = Plan.for(oad, skip_response:, skip_route:)
2525
[oad.key, plan]
2626
end
2727
end

lib/openapi_first/test/coverage/plan.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ module Coverage
1212
class Plan
1313
class UnknownRequestError < StandardError; end
1414

15-
def self.for(oad, skip_response: nil)
15+
def self.for(oad, skip_response: nil, skip_route: nil)
1616
plan = new(definition_key: oad.key, filepath: oad.filepath)
17-
oad.routes.each do |route|
17+
routes = oad.routes
18+
routes = routes.reject { |route| skip_route[route.path, route.request_method] } if skip_route
19+
routes.each do |route|
1820
responses = skip_response ? route.responses.reject(&skip_response) : route.responses
1921
plan.add_route request_method: route.request_method,
2022
path: route.path,

lib/openapi_first/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module OpenapiFirst
4-
VERSION = '2.9.3'
4+
VERSION = '2.10.0'
55
end

0 commit comments

Comments
 (0)