Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- OpenapiFirst::Test.report_coverage now includes fractional digits when returning a coverage value to avoid reporting "0% / no requests made" even though some requests have been made.

## 2.4.0

- Support less verbose test setup without the need to call `OpenapiFirst::Test.report_coverage`, which will be called `at_exit`:
Expand Down
13 changes: 11 additions & 2 deletions lib/openapi_first/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@ def self.minitest?(base)
class Setup
def initialize
@minimum_coverage = 0
@coverage_formatter = Coverage::TerminalFormatter
@coverage_formatter_options = {}
yield self
end

def register(*)
Test.register(*)
end

attr_accessor :minimum_coverage
def skip_response_coverage_for_status(*statuses); end

attr_accessor :minimum_coverage, :coverage_formatter_options, :coverage_formatter

# This called at_exit
def handle_exit
coverage = Coverage.result.coverage
# :nocov:
puts 'API Coverage did not detect any API requests for the registered API descriptions' if coverage.zero?
Test.report_coverage if coverage.positive?
if coverage.positive?
Test.report_coverage(
formatter: coverage_formatter,
**coverage_formatter_options
)
end
return unless minimum_coverage > coverage

puts "API Coverage fails with exit 2, because API coverage of #{coverage}%" \
Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_first/test/coverage/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def coverage
return 0 if done.zero?

all = tasks.count
(done / (all.to_f / 100)).to_i
(done / (all.to_f / 100))
end

def tasks
Expand Down
2 changes: 1 addition & 1 deletion spec/test/coverage/plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@

plan.track_request(valid_request)
plan.track_response(valid_response)
expect(plan.coverage).to eq(66)
expect(plan.coverage).to eq(66.66666666666667)
plan.track_response(valid_400_response)

expect(plan.coverage).to eq(100)
Expand Down
6 changes: 3 additions & 3 deletions spec/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
# Response not tracked

described_class.report_coverage
expect(output.string).to include('The overal API validation coverage of this run is: 50%')
expect(output.string).to include('The overal API validation coverage of this run is: 50.0%')
end

it 'reports 100% if all requests/responses have been tracked' do
Expand All @@ -95,7 +95,7 @@
definition.validate_response(valid_request, response, raise_error: true)

described_class.report_coverage
expect(output.string).to include('The overal API validation coverage of this run is: 100%')
expect(output.string).to include('The overal API validation coverage of this run is: 100.0%')
end

context 'when passing verbose: true' do
Expand All @@ -113,7 +113,7 @@
expected_output = [
'✓ POST /roll',
' ✓ 200(application/json)',
'The overal API validation coverage of this run is: 100%'
'The overal API validation coverage of this run is: 100.0%'
]
expect(output.string).to include(*expected_output)
end
Expand Down