Sha256: 5ee1c33ce4ca4c52384e216ffc4125b8ec97e65f42a0a51d4227427002ae2a71

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 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|
      RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::DefaultSchema.build(title))
      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

5 entries across 5 versions & 1 rubygems

Version Path
rspec-openapi-0.4.6 lib/rspec/openapi/hooks.rb
rspec-openapi-0.4.5 lib/rspec/openapi/hooks.rb
rspec-openapi-0.4.4 lib/rspec/openapi/hooks.rb
rspec-openapi-0.4.3 lib/rspec/openapi/hooks.rb
rspec-openapi-0.4.2 lib/rspec/openapi/hooks.rb