Skip to content

Commit

Permalink
Add optional generator for installing and configuring VCR (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrictson authored Oct 13, 2023
1 parent 4caaa26 commit 4dbe5ad
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ tomo:
prompt: "Tomo"
description: "Install tomo gem for SSH-based deployment"

vcr:
prompt: "VCR"
description: "Install and configure the vcr gem"
requires: test_framework

rubocop:
prompt: "RuboCop"
description: "Install rubocop gems; apply formatting rules"
Expand Down
2 changes: 2 additions & 0 deletions lib/nextgen/generators/vcr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
install_gems "vcr", "webmock", group: :test
template "test/support/vcr.rb.tt", rspec? ? "spec/support/vcr.rb" : "test/support/vcr.rb"
26 changes: 26 additions & 0 deletions template/test/support/vcr.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "vcr"

VCR.configure do |config|
config.hook_into :webmock
config.allow_http_connections_when_no_cassette = false
config.ignore_localhost = true
config.ignore_host "chromedriver.storage.googleapis.com"
config.cassette_library_dir = File.expand_path("../cassettes", __dir__)
<% if rspec? -%>
config.configure_rspec_metadata!
<% end -%>
config.default_cassette_options = {
# Enable automatic expiration and re-recording of cassettes
# re_record_interval: 1.week,
record: ENV["CI"] ? :none : :once,
record_on_error: false,
match_requests_on: %i[method uri body]
}

# Make sure headers containing secrets aren't recorded in cassettes and stored in git
%w[Authorization X-Api-Key].each do |sensitive_header|
config.filter_sensitive_data("[#{sensitive_header.upcase}]") do |interaction|
interaction.request.headers[sensitive_header]&.first
end
end
end
42 changes: 42 additions & 0 deletions test/nextgen/generators/vcr_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative "test_case"

class Nextgen::Generators::VcrTest < Nextgen::Generators::TestCase
destination File.join(Dir.tmpdir, "test_#{SecureRandom.hex(8)}")
setup :prepare_destination

setup do
Pathname.new(destination_root).join("Gemfile").write(<<~GEMFILE)
source "https://rubygems.org"
GEMFILE
end

test "installs vcr and webmock gems" do
apply_generator

assert_file "Gemfile" do |gemfile|
assert_match(/gem "vcr"/, gemfile)
assert_match(/gem "webmock"/, gemfile)
end
end

test "adds a test/support/vcr.rb file" do
apply_generator

assert_file "test/support/vcr.rb" do |support|
refute_match(/rspec/, support)
end
end

test "when rspec is present, adds a spec/support/vcr.rb file" do
Dir.chdir(destination_root) do
FileUtils.mkdir_p("spec/support")
FileUtils.touch("spec/spec_helper.rb")
end

apply_generator

assert_file "spec/support/vcr.rb" do |support|
assert_match(/rspec/, support)
end
end
end

0 comments on commit 4dbe5ad

Please sign in to comment.