Skip to content

Commit

Permalink
Loading extractor and dependencies into dependencies from the environ…
Browse files Browse the repository at this point in the history
…ment

* Make work minitest with new extractors
* Apply rubocop offences
  • Loading branch information
Aleksei committed Apr 10, 2024
1 parent ec94086 commit dcf0d90
Show file tree
Hide file tree
Showing 8 changed files with 1,125 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'rspec/openapi/version'
require 'rspec/openapi/components_updater'
require 'rspec/openapi/default_schema'
Expand All @@ -13,8 +14,9 @@
require 'rspec/openapi/key_transformer'
require 'rspec/openapi/extractors'
require 'rspec/openapi/extractors/rack'
require 'rspec/openapi/extractors/rails'
require 'rspec/openapi/extractors/hanami'

require 'rspec/openapi/extractors/hanami' if Bundler.load.specs.map(&:name).include?('hanami')
require 'rspec/openapi/extractors/rails' if Bundler.load.specs.map(&:name).include?('rails')

require 'rspec/openapi/minitest_hooks' if Object.const_defined?('Minitest')
require 'rspec/openapi/rspec_hooks' if ENV['OPENAPI'] && Object.const_defined?('RSpec')
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/extractors.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Create namespace
module RSpec::OpenAPI::Extractors
end
24 changes: 10 additions & 14 deletions lib/rspec/openapi/extractors/hanami.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'dry/inflector'
require 'hanami'
require 'hanami/router/inspector'

# Hanami::Router::Inspector original code
class Inspector
Expand Down Expand Up @@ -38,22 +38,18 @@ def call(verb, path)
InspectorAnalyzer = Inspector.new

# Monkey-patch hanami-router
module Hanami
class Slice
module ClassMethods
def router(inspector: InspectorAnalyzer)
raise SliceLoadError, "#{self} must be prepared before loading the router" unless prepared?

@_mutex.synchronize do
@_router ||= load_router(inspector: inspector)
end
end
module Hanami::Slice::ClassMethods
def router(inspector: InspectorAnalyzer)
raise SliceLoadError, "#{self} must be prepared before loading the router" unless prepared?

@_mutex.synchronize do
@_router ||= load_router(inspector: inspector)
end
end
end

# Extractor for hanami
class << RSpec::OpenAPI::Extractors::Hanami = Object.new

# @param [RSpec::ExampleGroups::*] context
# @param [RSpec::Core::Example] example
# @return Array
Expand All @@ -69,8 +65,8 @@ def request_attributes(request, example)
path = request.path

route = Hanami.app.router.recognize(request.path, method: request.method)
# binding.irb unless route.params.empty?
raw_path_params = route.params.filter { |key, value| number_or_nil(value) }

raw_path_params = route.params.filter { |_key, value| number_or_nil(value) }

result = InspectorAnalyzer.call(request.method, add_id(path, route))

Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/extractors/rack.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# Extractor for rack
class << RSpec::OpenAPI::Extractors::Rack = Object.new
# @param [RSpec::ExampleGroups::*] context
# @param [RSpec::Core::Example] example
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/extractors/rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# Extractor for rails
class << RSpec::OpenAPI::Extractors::Rails = Object.new
# @param [RSpec::ExampleGroups::*] context
# @param [RSpec::Core::Example] example
Expand Down
16 changes: 15 additions & 1 deletion lib/rspec/openapi/minitest_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ 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)
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: find_extractor)
RSpec::OpenAPI.path_records[path] << record if record
end
result
end

def find_extractor
if Bundler.load.specs.map(&:name).include?('rails') && defined?(Rails) &&
Rails.respond_to?(:application) && Rails.application
RSpec::OpenAPI::Extractors::Rails
elsif Bundler.load.specs.map(&:name).include?('hanami') && defined?(Hanami) &&
Hanami.respond_to?(:app) && Hanami.app?
RSpec::OpenAPI::Extractors::Hanami

Check warning on line 28 in lib/rspec/openapi/minitest_hooks.rb

View check run for this annotation

Codecov / codecov/patch

lib/rspec/openapi/minitest_hooks.rb#L28

Added line #L28 was not covered by tests
# elsif defined?(Roda)
# some Roda extractor
else
RSpec::OpenAPI::Extractors::Rack
end
end
end

module ActivateOpenApiClassMethods
Expand Down
6 changes: 4 additions & 2 deletions lib/rspec/openapi/rspec_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
end

def find_extractor
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
if Bundler.load.specs.map(&:name).include?('rails') && defined?(Rails) &&
Rails.respond_to?(:application) && Rails.application
RSpec::OpenAPI::Extractors::Rails
elsif defined?(Hanami)
elsif Bundler.load.specs.map(&:name).include?('hanami') && defined?(Hanami) &&
Hanami.respond_to?(:app) && Hanami.app?
RSpec::OpenAPI::Extractors::Hanami
# elsif defined?(Roda)
# some Roda extractor
Expand Down
Loading

0 comments on commit dcf0d90

Please sign in to comment.