Skip to content

Commit

Permalink
Merge pull request #128 from andyundso/prepend-openapi-in-any-case
Browse files Browse the repository at this point in the history
Prepend `openapi!` in any case
  • Loading branch information
exoego committed Aug 15, 2023
2 parents 0c92c20 + a37d3ec commit 7a6fda3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
6 changes: 2 additions & 4 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
require 'rspec/openapi/schema_merger'
require 'rspec/openapi/schema_cleaner'

if ENV['OPENAPI']
require 'rspec/openapi/minitest_hooks'
require 'rspec/openapi/rspec_hooks'
end
require 'rspec/openapi/minitest_hooks' if Object.const_defined?('Minitest')
require 'rspec/openapi/rspec_hooks' if ENV['OPENAPI'] && Object.const_defined?('RSpec')

module RSpec::OpenAPI
@path = 'doc/openapi.yaml'
Expand Down
20 changes: 12 additions & 8 deletions lib/rspec/openapi/minitest_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
module RSpec::OpenAPI::Minitest
Example = Struct.new(:context, :description, :metadata, :file_path)

module TestPatch
def self.prepended(base)
base.extend(ClassMethods)
end

module RunPatch
def run(*args)
result = super
if ENV['OPENAPI'] && self.class.openapi?
Expand All @@ -22,6 +18,12 @@ def run(*args)
end
result
end
end

module ActivateOpenApiClassMethods
def self.prepended(base)
base.extend(ClassMethods)
end

module ClassMethods
def openapi?
Expand All @@ -35,10 +37,12 @@ def openapi!
end
end

Minitest::Test.prepend RSpec::OpenAPI::Minitest::TestPatch
Minitest::Test.prepend RSpec::OpenAPI::Minitest::ActivateOpenApiClassMethods

if ENV['OPENAPI']
Minitest::Test.prepend RSpec::OpenAPI::Minitest::RunPatch

Minitest.after_run do
if ENV['OPENAPI']
Minitest.after_run do
result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records)
result_recorder.record_results!
puts result_record.error_message if result_recorder.errors?
Expand Down
3 changes: 1 addition & 2 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
ENV['RAILS_ENV'] ||= 'test'
ENV['OPENAPI_OUTPUT'] ||= 'yaml'

require File.expand_path('../rails/config/environment', __dir__)

require 'minitest/autorun'
require File.expand_path('../rails/config/environment', __dir__)

RSpec::OpenAPI.request_headers = %w[X-Authorization-Token]
RSpec::OpenAPI.response_headers = %w[X-Cursor]
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_tests/roda_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require_relative '../roda/roda_app'
require 'json'
require 'rack/test'
require 'rspec/openapi'
require 'minitest/autorun'
require 'rspec/openapi'

ENV['OPENAPI_OUTPUT'] ||= 'yaml'

Expand Down
6 changes: 6 additions & 0 deletions spec/minitest/rack_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@
expect(new_yaml).to eq org_yaml
end
end

describe 'with disabled OpenAPI generation' do
it 'can run tests' do
minitest 'spec/integration_tests/roda_test.rb'
end
end
end
6 changes: 6 additions & 0 deletions spec/minitest/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
expect(new_yaml).to eq org_yaml
end
end

describe 'with disabled OpenAPI generation' do
it 'can run tests' do
minitest 'spec/integration_tests/rails_test.rb'
end
end
end

0 comments on commit 7a6fda3

Please sign in to comment.