lib/rspec/openapi/hooks.rb in rspec-openapi-0.4.1 vs lib/rspec/openapi/hooks.rb in rspec-openapi-0.4.2
- old
+ new
@@ -3,37 +3,39 @@
require 'rspec/openapi/record_builder'
require 'rspec/openapi/schema_builder'
require 'rspec/openapi/schema_file'
require 'rspec/openapi/schema_merger'
-records = []
-records_errors = []
+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)
- records << record if record
+ path_records[path] << record if record
end
end
RSpec.configuration.after(:suite) do
title = File.basename(Dir.pwd)
- RSpec::OpenAPI::SchemaFile.new(RSpec::OpenAPI.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
- # NOTE: Don't fail the build
- records_errors << [e, record]
+ 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 records_errors.any?
+ if error_records.any?
error_message = <<~EOS
- RSpec::OpenAPI got errors building #{records_errors.size} requests
+ RSpec::OpenAPI got errors building #{error_records.size} requests
- #{records_errors.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
+ #{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