Skip to content

Commit

Permalink
Proof-of-concept minitest integration.
Browse files Browse the repository at this point in the history
Rough around the edges, but shows it can be done.
  • Loading branch information
oneiros committed Jan 27, 2023
1 parent 375aed5 commit 5441904
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gemspec
gem 'rails', ENV['RAILS_VERSION'] || '6.0.3.7'
gem 'roda'
gem 'rspec-rails'
gem 'rspec'

group :test do
gem 'super_diff'
Expand Down
13 changes: 11 additions & 2 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require 'rspec/openapi/version'
require 'rspec/openapi/hooks' if ENV['OPENAPI']

require 'rspec/openapi/minitest' if defined?(Minitest)

if ENV['OPENAPI']
require 'rspec/openapi/hooks' if defined?(RSpec::Example)
end

module RSpec::OpenAPI
@path = 'doc/openapi.yaml'
Expand All @@ -13,6 +18,8 @@ module RSpec::OpenAPI
@security_schemes = []
@example_types = %i[request]
@response_headers = []
@path_records = Hash.new { |h, k| h[k] = [] }
@error_records = {}

class << self
attr_accessor :path,
Expand All @@ -25,6 +32,8 @@ class << self
:servers,
:security_schemes,
:example_types,
:response_headers
:response_headers,
:path_records,
:error_records
end
end
81 changes: 81 additions & 0 deletions lib/rspec/openapi/minitest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'rspec/openapi/components_updater'
require 'rspec/openapi/default_schema'
require 'rspec/openapi/record_builder'
require 'rspec/openapi/schema_builder'
require 'rspec/openapi/schema_file'
require 'rspec/openapi/schema_merger'
require 'rspec/openapi/schema_cleaner'

module RSpec
module OpenAPI
module Minitest
class Example < Struct.new(:context, :description, :metadata) ; end

module TestPatch
def self.prepended(base)
base.extend(ClassMethods)
end

def run(*args)
result = super
if ENV['OPENAPI'] && self.class.openapi?
path = RSpec::OpenAPI.path.yield_self { |p| p.is_a?(Proc) ? p.call(example) : p }
example = Example.new(self, name, {})
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example)
RSpec::OpenAPI.path_records[path] << record if record
end
result
end

def inspect
self.class.to_s
end

module ClassMethods
def openapi?
@openapi
end

def openapi!
@openapi = true
end
end
end
end
end
end

Minitest::Test.prepend RSpec::OpenAPI::Minitest::TestPatch

Minitest.after_run do
if ENV['OPENAPI']
title = File.basename(Dir.pwd)
RSpec::OpenAPI.path_records.each do |path, records|
RSpec::OpenAPI::SchemaFile.new(path).edit do |spec|
schema = RSpec::OpenAPI::DefaultSchema.build(title)
schema[:info].merge!(RSpec::OpenAPI.info)
RSpec::OpenAPI::SchemaMerger.merge!(spec, schema)
new_from_zero = {}
records.each do |record|
begin
record_schema = RSpec::OpenAPI::SchemaBuilder.build(record)
RSpec::OpenAPI::SchemaMerger.merge!(spec, record_schema)
RSpec::OpenAPI::SchemaMerger.merge!(new_from_zero, record_schema)
rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
RSpec::OpenAPI.error_records[e] = record # Avoid failing the build
end
end
RSpec::OpenAPI::SchemaCleaner.cleanup!(spec, new_from_zero)
RSpec::OpenAPI::ComponentsUpdater.update!(spec, new_from_zero)
end
end
if RSpec::OpenAPI.error_records.any?
error_message = <<~EOS
RSpec::OpenAPI got errors building #{RSpec::OpenAPI.error_records.size} requests
#{RSpec::OpenAPI.error_records.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
EOS
puts error_message
end
end
end
1 change: 0 additions & 1 deletion rspec-openapi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'actionpack', '>= 5.2.0'
spec.add_dependency 'rspec'
end

0 comments on commit 5441904

Please sign in to comment.