generated from mattbrictson/gem
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional generator for installing and configuring VCR (#6)
- Loading branch information
1 parent
4caaa26
commit 4dbe5ad
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |