Module: OpenapiRuby::Generator::RakeTaskSupport
- Defined in:
- lib/openapi_ruby/generator/rake_task_support.rb
Overview
Helpers backing the openapi_ruby:generate rake task. Extracted
so the framework detection / script generation logic is testable
without booting Rake.
Class Method Summary collapse
- .default_pattern_for(framework) ⇒ Object
- .detect_test_framework ⇒ Object
- .generate_script(framework, pattern) ⇒ Object
- .glob_loads(pattern) ⇒ Object
-
.hybrid_script(pattern) ⇒ Object
Loads both adapters and both file globs in one process.
- .minitest_script(pattern) ⇒ Object
- .rspec_script(pattern) ⇒ Object
-
.suppress_autorun ⇒ Object
Prepended to every generated script, before any consumer file is required.
Class Method Details
.default_pattern_for(framework) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 27 def default_pattern_for(framework) case framework when "rspec" then "spec/**/*_spec.rb" when "minitest" then "test/**/*_test.rb" when "hybrid" then "spec/**/*_spec.rb,test/**/*_test.rb" end end |
.detect_test_framework ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 11 def detect_test_framework rspec = File.exist?("spec/spec_helper.rb") || File.exist?("spec/rails_helper.rb") minitest = File.exist?("test/test_helper.rb") if rspec && minitest "hybrid" elsif rspec "rspec" elsif minitest "minitest" else raise ArgumentError, "Could not detect test framework. Set FRAMEWORK=rspec, FRAMEWORK=minitest, or FRAMEWORK=hybrid." end end |
.generate_script(framework, pattern) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 35 def generate_script(framework, pattern) case framework when "rspec" then rspec_script(pattern) when "minitest" then minitest_script(pattern) when "hybrid" then hybrid_script(pattern) else raise ArgumentError, "Unknown test framework '#{framework}'." end end |
.glob_loads(pattern) ⇒ Object
124 125 126 127 128 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 124 def glob_loads(pattern) pattern.split(",").map do |p| %[Dir.glob(#{p.strip.inspect}).sort.each { |f| require File.expand_path(f) }] end.join("\n") end |
.hybrid_script(pattern) ⇒ Object
Loads both adapters and both file globs in one process. Useful
during a phased RSpec → Minitest migration where the suite
holds both DSL styles. Consumers should guard
require "rails/test_help" and require "rspec/rails" in
their test helpers with unless OpenapiRuby.schema_generating?
so the two test frameworks don't both register Rails lazy
hooks in the same process — only the DSL needs to be live for
schema generation.
Each glob runs with its own framework's directory at the head
of $LOAD_PATH so the typical require "openapi_helper" /
require "rails_helper" / require "test_helper" resolves
to the right file. Without this, both spec/ and test/ getting
unshifted in one block leads to whichever was unshifted last
winning every lookup — and the wrong helper getting loaded
for the other side's files.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 93 def hybrid_script(pattern) globs = pattern.split(",").map(&:strip) spec_globs = globs.grep(%r{\bspec/}) test_globs = globs.grep(%r{\btest/}) other_globs = globs - spec_globs - test_globs <<~RUBY require "rspec/core" require "openapi_ruby/rspec" require "openapi_ruby/minitest" #{suppress_autorun} load_with_path = lambda do |dir, glob| path = File.expand_path(dir) added = !$LOAD_PATH.include?(path) $LOAD_PATH.unshift(path) if added begin Dir.glob(glob).sort.each { |f| require File.expand_path(f) } ensure $LOAD_PATH.delete(path) if added end end #{spec_globs.map { |g| "load_with_path.call(\"spec\", #{g.inspect})" }.join("\n ")} #{test_globs.map { |g| "load_with_path.call(\"test\", #{g.inspect})" }.join("\n ")} #{other_globs.map { |g| %[Dir.glob(#{g.inspect}).sort.each { |f| require File.expand_path(f) }] }.join("\n ")} OpenapiRuby::Generator::SchemaWriter.generate_all! RUBY end |
.minitest_script(pattern) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 67 def minitest_script(pattern) <<~RUBY require "openapi_ruby/minitest" #{suppress_autorun} $LOAD_PATH.unshift(File.expand_path("test")) unless $LOAD_PATH.include?(File.expand_path("test")) #{glob_loads(pattern)} OpenapiRuby::Generator::SchemaWriter.generate_all! RUBY end |
.rspec_script(pattern) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 57 def rspec_script(pattern) <<~RUBY require "rspec/core" #{suppress_autorun} $LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec")) #{glob_loads(pattern)} OpenapiRuby::Generator::SchemaWriter.generate_all! RUBY end |
.suppress_autorun ⇒ Object
Prepended to every generated script, before any consumer file is required. Loading the consumer's test files must neither run them (AutorunSuppressor) nor drag in a database (TestSchemaSuppressor).
48 49 50 51 52 53 54 55 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 48 def suppress_autorun <<~RUBY.chomp require "openapi_ruby/generator/autorun_suppressor" require "openapi_ruby/generator/test_schema_suppressor" OpenapiRuby::Generator::AutorunSuppressor.install! OpenapiRuby::Generator::TestSchemaSuppressor.install! RUBY end |