Sha256: 4b3379d6a38a4d581ef33e9079cd7919de39f04d4647508069d2e2abcbcf759a

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'rspec'
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'

path_records = Hash.new { |h, k| h[k] = [] }
error_records = {}

RSpec.configuration.after(:each) do |example|
  if RSpec::OpenAPI.example_types.include?(example.metadata[:type]) && example.metadata[:openapi] != false
    path = RSpec::OpenAPI.path.yield_self { |path| path.is_a?(Proc) ? path.call(example) : path }
    record = RSpec::OpenAPI::RecordBuilder.build(self, example: example)
    path_records[path] << record if record
  end
end

RSpec.configuration.after(:suite) do
  title = File.basename(Dir.pwd)
  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.reverse_merge!(spec, schema)
      records.each do |record|
        begin
          RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::SchemaBuilder.build(record))
        rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
          error_records[e] = record # Avoid failing the build
        end
      end
    end
  end
  if error_records.any?
    error_message = <<~EOS
      RSpec::OpenAPI got errors building #{error_records.size} requests

      #{error_records.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
    EOS
    colorizer = ::RSpec::Core::Formatters::ConsoleCodes
    RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-openapi-0.4.8 lib/rspec/openapi/hooks.rb
rspec-openapi-0.4.7 lib/rspec/openapi/hooks.rb