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
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
108 109 110 111 112 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 108 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 78 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" 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
54 55 56 57 58 59 60 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 54 def minitest_script(pattern) <<~RUBY require "openapi_ruby/minitest" #{glob_loads(pattern)} OpenapiRuby::Generator::SchemaWriter.generate_all! RUBY end |
.rspec_script(pattern) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/openapi_ruby/generator/rake_task_support.rb', line 45 def rspec_script(pattern) <<~RUBY require "rspec/core" $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 |