From 2527d79d8ec351d32ab04c56662f7802120fa3c4 Mon Sep 17 00:00:00 2001 From: Aleksei <9ceb2990-3744-4629-82f3-19bc5c80b3a2@mackevich.addymail.com> Date: Thu, 11 Apr 2024 15:38:55 +0500 Subject: [PATCH] Extract find_extractor --- lib/rspec/openapi.rb | 1 + lib/rspec/openapi/minitest_hooks.rb | 18 +----------------- lib/rspec/openapi/rspec_hooks.rb | 18 +----------------- lib/rspec/openapi/shared_hooks.rb | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 34 deletions(-) create mode 100644 lib/rspec/openapi/shared_hooks.rb diff --git a/lib/rspec/openapi.rb b/lib/rspec/openapi.rb index bbd42de..ad60760 100644 --- a/lib/rspec/openapi.rb +++ b/lib/rspec/openapi.rb @@ -12,6 +12,7 @@ require 'rspec/openapi/schema_cleaner' require 'rspec/openapi/schema_sorter' require 'rspec/openapi/key_transformer' +require 'rspec/openapi/shared_hooks' require 'rspec/openapi/extractors' require 'rspec/openapi/extractors/rack' diff --git a/lib/rspec/openapi/minitest_hooks.rb b/lib/rspec/openapi/minitest_hooks.rb index f3c4bdc..15682d5 100644 --- a/lib/rspec/openapi/minitest_hooks.rb +++ b/lib/rspec/openapi/minitest_hooks.rb @@ -13,27 +13,11 @@ def run(*args) human_name = name.sub(/^test_/, '').gsub('_', ' ') example = Example.new(self, human_name, {}, file_path) path = RSpec::OpenAPI.path.then { |p| p.is_a?(Proc) ? p.call(example) : p } - record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: find_extractor) + record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: SharedHooks.find_extractor) RSpec::OpenAPI.path_records[path] << record if record end result end - - def find_extractor - names = Bundler.load.specs.map(&:name) - - if names.include?('rails') && defined?(Rails) && - Rails.respond_to?(:application) && Rails.application - RSpec::OpenAPI::Extractors::Rails - elsif names.include?('hanami') && defined?(Hanami) && - Hanami.respond_to?(:app) && Hanami.app? - RSpec::OpenAPI::Extractors::Hanami - # elsif defined?(Roda) - # some Roda extractor - else - RSpec::OpenAPI::Extractors::Rack - end - end end module ActivateOpenApiClassMethods diff --git a/lib/rspec/openapi/rspec_hooks.rb b/lib/rspec/openapi/rspec_hooks.rb index 494c1b5..753f345 100644 --- a/lib/rspec/openapi/rspec_hooks.rb +++ b/lib/rspec/openapi/rspec_hooks.rb @@ -5,7 +5,7 @@ RSpec.configuration.after(:each) do |example| if RSpec::OpenAPI.example_types.include?(example.metadata[:type]) && example.metadata[:openapi] != false path = RSpec::OpenAPI.path.then { |p| p.is_a?(Proc) ? p.call(example) : p } - record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: find_extractor) + record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: SharedHooks.find_extractor) RSpec::OpenAPI.path_records[path] << record if record end end @@ -19,19 +19,3 @@ RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure) end end - -def find_extractor - names = Bundler.load.specs.map(&:name) - - if names.include?('rails') && defined?(Rails) && - Rails.respond_to?(:application) && Rails.application - RSpec::OpenAPI::Extractors::Rails - elsif names.include?('hanami') && defined?(Hanami) && - Hanami.respond_to?(:app) && Hanami.app? - RSpec::OpenAPI::Extractors::Hanami - # elsif defined?(Roda) - # some Roda extractor - else - RSpec::OpenAPI::Extractors::Rack - end -end diff --git a/lib/rspec/openapi/shared_hooks.rb b/lib/rspec/openapi/shared_hooks.rb new file mode 100644 index 0000000..d3496b9 --- /dev/null +++ b/lib/rspec/openapi/shared_hooks.rb @@ -0,0 +1,17 @@ +module SharedHooks + def self.find_extractor + names = Bundler.load.specs.map(&:name) + + if names.include?('rails') && defined?(Rails) && + Rails.respond_to?(:application) && Rails.application + RSpec::OpenAPI::Extractors::Rails + elsif names.include?('hanami') && defined?(Hanami) && + Hanami.respond_to?(:app) && Hanami.app? + RSpec::OpenAPI::Extractors::Hanami + # elsif defined?(Roda) + # some Roda extractor + else + RSpec::OpenAPI::Extractors::Rack + end + end +end