From 4dbe5ad4bb43b75b9b1fd2e8be9763ee367b19e7 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Fri, 13 Oct 2023 11:19:04 -0700 Subject: [PATCH] Add optional generator for installing and configuring VCR (#6) --- config/generators.yml | 5 ++++ lib/nextgen/generators/vcr.rb | 2 ++ template/test/support/vcr.rb.tt | 26 ++++++++++++++++++ test/nextgen/generators/vcr_test.rb | 42 +++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 lib/nextgen/generators/vcr.rb create mode 100644 template/test/support/vcr.rb.tt create mode 100644 test/nextgen/generators/vcr_test.rb diff --git a/config/generators.yml b/config/generators.yml index a82e948..42e305f 100644 --- a/config/generators.yml +++ b/config/generators.yml @@ -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" diff --git a/lib/nextgen/generators/vcr.rb b/lib/nextgen/generators/vcr.rb new file mode 100644 index 0000000..c421186 --- /dev/null +++ b/lib/nextgen/generators/vcr.rb @@ -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" diff --git a/template/test/support/vcr.rb.tt b/template/test/support/vcr.rb.tt new file mode 100644 index 0000000..bd50097 --- /dev/null +++ b/template/test/support/vcr.rb.tt @@ -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 diff --git a/test/nextgen/generators/vcr_test.rb b/test/nextgen/generators/vcr_test.rb new file mode 100644 index 0000000..9c1c897 --- /dev/null +++ b/test/nextgen/generators/vcr_test.rb @@ -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