Skip to content

Commit

Permalink
Merge pull request #120 from Zero-Config-Rails/fix-vcr-generator
Browse files Browse the repository at this point in the history
Fix VCR generator
  • Loading branch information
abhaynikam authored Jun 26, 2024
2 parents faaf902 + 053eeef commit 27b1f42
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
51 changes: 28 additions & 23 deletions lib/generators/boring/vcr/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,46 @@ module Boring
module Vcr
class InstallGenerator < Rails::Generators::Base
desc "Adds VCR to the application"
source_root File.expand_path('templates', __dir__)
source_root File.expand_path("templates", __dir__)

class_option :testing_framework,
type: :string,
alias: "-tf",
default: "rspec",
default: "minitest",
enum: %w[rspec minitest],
desc: "Tell us which test framework you are using. Default to rspec"
desc:
"Tell us which test framework you are using. Defaults to minitest"

class_option :stubbing_libraries,
type: :array,
alias: "-sl",
default: ['webmock'],
default: ["webmock"],
enum: %w[webmock typhoeus faraday excon],
desc: "Tell us stubbing library you want to use seprated by space. Default to webmock"
desc:
"Tell us stubbing library you want to use separated by space. Defaults to webmock"

def verify_presence_of_rails_helper
return if !rspec? || File.exist?("spec/rails_helper.rb")
def verify_presence_of_test_helper
return if rspec? || (minitest? && File.exist?("test/test_helper.rb"))

say "We couldn't find spec/rails_helper.rb. Please configure RSpec and rerun the generator. Consider running `rails generate boring:rspec:install` to set up RSpec.",
say "We couldn't find test/test_helper.rb. Please configure Minitest and rerun the generator.",
:red

abort
end

def verify_presence_of_test_helper
return if !minitest? || File.exist?("test/test_helper.rb")
def verify_presence_of_rails_helper
return if minitest? || (rspec? && File.exist?("spec/rails_helper.rb"))

say "We couldn't find test/test_helper.rb. Please configure Minitest and rerun the generator.",
say "We couldn't find spec/rails_helper.rb. Please configure RSpec and rerun the generator. Consider running `rails generate boring:rspec:install` to set up RSpec.",
:red

abort
end

def add_vcr_gem
say "Adding VCR gems to Gemfile", :green
# TODO: Use check_and_install_gem method when it is available
gem 'vcr', group: :test
gem "vcr", group: :test
end

def add_stubbing_library_gems
Expand All @@ -52,7 +56,7 @@ def add_stubbing_library_gems
end

def setup_vcr_for_rspec
return unless rspec?
return unless rspec?

say "Setting up VCR for RSpec", :green

Expand All @@ -68,7 +72,7 @@ def setup_vcr_for_rspec
end

def setup_vcr_for_minitest
return unless minitest?
return unless minitest?

say "Setting up VCR for Minitest", :green

Expand All @@ -82,34 +86,35 @@ def setup_vcr_for_minitest
end
RUBY


inject_into_file "test/test_helper.rb",
vcr_config,
end: /^end\s*\Z/m
inject_into_file "test/test_helper.rb", vcr_config, end: /^end\s*\Z/m
end

private

def format_stubbing_libraries
options[:stubbing_libraries]
.map { |stubbing_library| ":#{stubbing_library}" }
.join(', ')
.join(", ")
end

def rspec?
options[:testing_framework].to_s == 'rspec'
options[:testing_framework].to_s == "rspec"
end

def minitest?
options[:testing_framework].to_s == 'minitest'
options[:testing_framework].to_s == "minitest"
end

def all_support_files_are_required?
line_to_check = "Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }"
line_to_check =
"Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }"
rails_file_content_array = File.readlines("spec/rails_helper.rb")
rails_file_content_array.any? do |line|
line !~ /^\s*#/ &&
(line.include?(line_to_check) || line.include?(line_to_check.gsub("'", '"')))
(
line.include?(line_to_check) ||
line.include?(line_to_check.gsub("'", '"'))
)
end
end
end
Expand Down
40 changes: 26 additions & 14 deletions test/generators/vcr/vcr_install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,66 @@ def destination_root
app_path
end

def test_should_exit_if_test_helper_is_not_present
Dir.chdir(app_path) do
`mv test/test_helper.rb test/test_helper.bak`

assert_raises SystemExit do
quietly { run_generator }
end

`mv test/test_helper.bak test/test_helper.rb`
end
end

def test_should_exit_if_rails_helper_is_not_present
assert_raises SystemExit do
quietly { run_generator }
quietly { run_generator %w[--testing_framework=rspec] }
end
end

def test_should_configure_vcr
Dir.chdir(app_path) do
configure_rspec
quietly { run_generator }

assert_gem "vcr"
assert_gem "webmock"
assert_file "spec/support/vcr.rb" do |content|

assert_file "test/test_helper.rb" do |content|
assert_match(/require "vcr"/, content)
assert_match(/VCR.configure do |c|/, content)
assert_match(/c.hook_into :webmock/, content)
assert_match(/c.configure_rspec_metadata!/, content)
end

assert_file 'spec/rails_helper.rb' do |content|
assert_match(/require 'support\/vcr'/, content)
assert_no_match(/c.configure_rspec_metadata!/, content)
end
end
end

def test_should_configure_vcr_for_minitest
def test_should_configure_vcr_for_rspec
Dir.chdir(app_path) do
quietly { run_generator %w[--testing_framework=minitest] }
configure_rspec
quietly { run_generator %w[--testing_framework=rspec] }

assert_file "test/test_helper.rb" do |content|
assert_file "spec/support/vcr.rb" do |content|
assert_match(/require "vcr"/, content)
assert_match(/VCR.configure do |c|/, content)
assert_match(/c.hook_into :webmock/, content)
assert_no_match(/c.configure_rspec_metadata!/, content)
assert_match(/c.configure_rspec_metadata!/, content)
end

assert_file "spec/rails_helper.rb" do |content|
assert_match(%r{require 'support/vcr'}, content)
end
end
end

def test_should_configure_vcr_with_correct_stubbing_libraries
Dir.chdir(app_path) do
configure_rspec
quietly { run_generator %w[--stubbing_libraries=webmock typhoeus] }

assert_gem "webmock"
assert_gem "typhoeus"

assert_file "spec/support/vcr.rb" do |content|
assert_file "test/test_helper.rb" do |content|
assert_match(/c.hook_into :webmock, :typhoeus/, content)
end
end
Expand Down

0 comments on commit 27b1f42

Please sign in to comment.