Skip to content

Commit

Permalink
added configuration for path-based overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerine committed Sep 27, 2023
1 parent a05157d commit aab9d26
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module RSpec::OpenAPI
@application_version = '1.0.0'
@request_headers = []
@servers = []
@config_filename = 'rspec_openapi.rb'
@path_servers = []
@path_server_records = Hash.new

Check notice

Code scanning / Rubocop

Prefer literals to Array.new/Hash.new/String.new. Note

Style/EmptyLiteral: Use hash literal {} instead of Hash.new.
@security_schemes = []
@example_types = %i[request]
@response_headers = []
Expand All @@ -36,6 +39,10 @@ class << self
:application_version,
:request_headers,
:servers,
:config_filename,
:schema_filename,
:path_servers,
:path_server_records,
:security_schemes,
:example_types,
:response_headers,
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/openapi/result_recorder.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# frozen_string_literal: true

class RSpec::OpenAPI::ResultRecorder
def initialize(path_records)
def initialize(path_records, path_server_records)
@path_records = path_records
@path_server_records = path_server_records
@error_records = {}
end

def record_results!
title = File.basename(Dir.pwd)
@path_records.each do |path, records|
RSpec::OpenAPI::SchemaFile.new(path).edit do |spec|
RSpec::OpenAPI.servers = @path_server_records[path].to_a if @path_server_records[path].present?
dir = path.match(/^(.*[\\\/])/)[0]

Check notice

Code scanning / Rubocop

Use / or %r around regular expressions. Note

Style/RegexpLiteral: Use %r around regular expression.
eval(File.read("#{dir}#{RSpec::OpenAPI.config_filename}")) if File.exist?("#{dir}#{RSpec::OpenAPI.config_filename}")

Check notice

Code scanning / Rubocop

The use of eval represents a serious security risk. Note

Security/Eval: The use of eval is a serious security risk.

Check notice

Code scanning / Rubocop

Favor modifier if/unless usage when you have a single-line body. Note

Style/IfUnlessModifier: Modifier form of if makes the line too long.

Check notice

Code scanning / Rubocop

Checks that line length does not exceed the configured limit. Note

Layout/LineLength: Line is too long. [124/120]
schema = RSpec::OpenAPI::DefaultSchema.build(title)
schema[:info].merge!(RSpec::OpenAPI.info)
RSpec::OpenAPI::SchemaMerger.merge!(spec, schema)
Expand Down
7 changes: 5 additions & 2 deletions lib/rspec/openapi/rspec_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
path = RSpec::OpenAPI.path.yield_self { |p| p.is_a?(Proc) ? p.call(example) : p }
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example)
RSpec::OpenAPI.path_records[path] << record if record
path_servers = RSpec::OpenAPI.path_servers.yield_self { |p| p.is_a?(Proc) ? p.call(example) : p }
RSpec::OpenAPI.path_server_records[path] = Set.new unless RSpec::OpenAPI.path_server_records[path]
RSpec::OpenAPI.path_server_records[path] += path_servers if path_servers
end
end

RSpec.configuration.after(:suite) do
result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records)
result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records, RSpec::OpenAPI.path_server_records)
result_recorder.record_results!
if result_recorder.errors?
error_message = result_recorder.error_message
colorizer = RSpec::Core::Formatters::ConsoleCodes
RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
end
end
end

Check notice

Code scanning / Rubocop

Checks trailing blank lines and final newline. Note

Layout/TrailingEmptyLines: Final newline missing.

0 comments on commit aab9d26

Please sign in to comment.