-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathhooks.rb
More file actions
49 lines (45 loc) · 1.89 KB
/
Copy pathhooks.rb
File metadata and controls
49 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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'
require 'rspec/openapi/schema_cleaner'
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.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
error_records[e] = record # Avoid failing the build
end
end
RSpec::OpenAPI::SchemaCleaner.cleanup!(spec, new_from_zero)
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