Skip to content

Commit 113aa78

Browse files
authored
Merge pull request #423 from ahx/fix-configure
Don't reset the global configuration during configuration
2 parents c04fbd2 + c8c1f7e commit 113aa78

7 files changed

Lines changed: 32 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Before this change `GET /things/24/` matched `/things/{id}:`, but it no longer does.
88
- Breaking: Failure type `:response_not_found` was split into two more specific types `:response_content_type_not_found` and `:response_status_not_found`. This should be mostly internal stuff. So if your custom error response used `response_not_found`, you will have to adapt.
99
- `OpenapiFirst::Test.app` now returns an instance of `OpenapiFirst::Test::App`, instead of `Rack::Builer` and delegates methods other than `#call` to the original app. This wrapper adds validated requests, responses to the rack env at `env[OpenapiFirst::Test::REQUEST]`, `env[OpenapiFirst::Test::RESPONSE]`. This makes it possible to test Rails engines. Thanks to Josh! See [#410](https://github.com/ahx/openapi_first/issues/410).
10+
- `OpenapiFirst::Test` now falls back to using globally registered OADs if nothing was registered inside `OpenapiFirst::Test.setup`.
1011

1112
### Added
1213
- The Coverage feature in `OpenapiFirst::Test` now supports parallel tests via a DRB client/sever. Thanks to Richard! See [#394](https://github.com/ahx/openapi_first/issues/394).

lib/openapi_first.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def self.configuration
3030
# @return [Configuration]
3131
# @yield [Configuration]
3232
def self.configure
33-
@configuration = Configuration.new
34-
yield @configuration if block_given?
33+
yield configuration if block_given?
3534
end
3635

3736
ERROR_RESPONSES = {} # rubocop:disable Style/MutableConstant

lib/openapi_first/test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def self.minitest?(base)
2525
false
2626
end
2727

28+
def self.definitions
29+
super.empty? ? OpenapiFirst.definitions : super
30+
end
31+
2832
def self.configuration
2933
@configuration ||= Configuration.new
3034
end
@@ -39,15 +43,16 @@ def self.setup
3943
install
4044
yield configuration
4145

42-
configuration.registry.each { |name, oad| register(oad, as: name) }
43-
configuration.apps.each { |name, apps| apps.each { |app| observe(app, api: name) } }
4446
Coverage.start(skip_response: configuration.skip_response_coverage, skip_route: configuration.skip_coverage)
4547

4648
if definitions.empty?
4749
raise NotRegisteredError,
4850
'No API descriptions have been registered. ' \
4951
'Please register your API description via ' \
50-
"OpenapiFirst::Test.setup { |test| test.register('myopenapi.yaml') }"
52+
"`OpenapiFirst.register('myopenapi.yaml)` or " \
53+
'in a block passed to `OpenapiFirst::Test.setup` like this: ' \
54+
"`OpenapiFirst::Test.setup { |test| test.register('myopenapi.yaml') }` " \
55+
5156
end
5257

5358
@exit_handler = method(:handle_exit)

lib/openapi_first/test/configuration.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@ def initialize
1515
@ignore_unknown_response_status = false
1616
@report_coverage = true
1717
@ignore_unknown_requests = false
18-
@registry = {}
19-
@apps = {}
2018
end
2119

2220
# Register OADs, but don't load them just yet
2321
# @param [OpenapiFirst::OAD] oad The OAD to register
2422
# @param [Symbol] as The name to register the OAD under
2523
def register(oad, as: :default)
26-
@registry[as] = oad
24+
Test.register(oad, as:)
2725
end
2826

2927
# Observe a rack app
3028
def observe(app, api: :default)
31-
(@apps[api] ||= []) << app
29+
Observe.observe(app, api:)
3230
end
3331

3432
attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error,
3533
:ignore_unknown_requests, :ignore_unknown_response_status, :minimum_coverage
36-
attr_reader :registry, :apps, :report_coverage, :ignored_unknown_status
34+
attr_reader :report_coverage, :ignored_unknown_status
3735

3836
# Configure report coverage
3937
# @param [Boolean, :warn] value Whether to report coverage or just warn.

spec/openapi_first_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
end
3838
end
3939

40+
describe '.configure' do
41+
it 'does not reset .configuration' do
42+
old_config_instance = described_class.configuration
43+
described_class.configure do |_|
44+
24
45+
end
46+
expect(described_class.configuration).to be(old_config_instance)
47+
end
48+
end
49+
4050
describe '.load' do
4151
begin
4252
require 'multi_json'

spec/spec_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
end
2929

3030
config.after(:each) do
31-
OpenapiFirst.configure
3231
OpenapiFirst::Test.definitions.clear
3332
OpenapiFirst.definitions.clear
3433
OpenapiFirst::Test.uninstall

spec/test_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ def call(_env)
135135
end.to raise_error ArgumentError
136136
end
137137

138+
it 'returns the globally registered OADs if nothing was registered inside the block' do
139+
oad = OpenapiFirst.load('./spec/data/dice.yaml')
140+
OpenapiFirst.register(oad, as: :dice)
141+
142+
described_class.setup { |_test| } # rubocop:disable Lint/EmptyBlock
143+
144+
expect(described_class.definitions).to be(OpenapiFirst.definitions)
145+
end
146+
138147
it 'raises an error if no API description was registered' do
139148
expect do
140149
described_class.setup { |_test| } # rubocop:disable Lint/EmptyBlock

0 commit comments

Comments
 (0)