Skip to content

Commit

Permalink
Merge pull request #16192 from opf/chore/make_vcr_work_outside_of_sto…
Browse files Browse the repository at this point in the history
…rages_module

Make VCR `cassette_library_dir` configurable through test metadata
  • Loading branch information
cbliard authored Jul 19, 2024
2 parents e834e88 + 9f361e7 commit a1c27bf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,11 @@
shared_let(:storage) { create(:sharepoint_dev_drive_storage) }

shared_let(:original_folders) do
WebMock.enable! && VCR.turn_on!
VCR.use_cassette("one_drive/copy_template_folder_existing_folders") { existing_folder_tuples }
ensure
VCR.turn_off! && WebMock.disable!
use_storages_vcr_cassette("one_drive/copy_template_folder_existing_folders") { existing_folder_tuples }
end

shared_let(:base_template_folder) do
WebMock.enable! && VCR.turn_on!
VCR.use_cassette("one_drive/copy_template_folder_base_folder") { create_base_folder }
ensure
VCR.turn_off! && WebMock.disable!
use_storages_vcr_cassette("one_drive/copy_template_folder_base_folder") { create_base_folder }
end

shared_let(:source_path) { base_template_folder.id }
Expand Down Expand Up @@ -76,17 +70,11 @@
describe "#call" do
# rubocop:disable RSpec/BeforeAfterAll
before(:all) do
WebMock.enable! && VCR.turn_on!
VCR.use_cassette("one_drive/copy_template_folder_setup") { setup_template_folder }
ensure
VCR.turn_off! && WebMock.disable!
use_storages_vcr_cassette("one_drive/copy_template_folder_setup") { setup_template_folder }
end

after(:all) do
WebMock.enable! && VCR.turn_on!
VCR.use_cassette("one_drive/copy_template_folder_teardown") { delete_template_folder }
ensure
VCR.turn_off! && WebMock.disable!
use_storages_vcr_cassette("one_drive/copy_template_folder_teardown") { delete_template_folder }
end
# rubocop:enable RSpec/BeforeAfterAll

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@
# otherwise it will run the request every test suite run.
# Then we disable both VCR and WebMock to return to the usual state
shared_let(:original_folder_ids) do
WebMock.enable! && VCR.turn_on!
VCR.use_cassette("one_drive/sync_service_original_folders") do
use_storages_vcr_cassette("one_drive/sync_service_original_folders") do
original_folders(storage)
end
ensure
VCR.turn_off! && WebMock.disable!
end

subject(:service) { described_class.new(storage) }
Expand Down
17 changes: 16 additions & 1 deletion modules/storages/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
require "spec_helper"
require "dry/container/stub"

STORAGES_CASSETTE_LIBRARY_DIR = "modules/storages/spec/support/fixtures/vcr_cassettes"

# Record Storages Cassettes in module
VCR.configure do |config|
config.cassette_library_dir = "modules/storages/spec/support/fixtures/vcr_cassettes"
config.filter_sensitive_data("<ACCESS_TOKEN>") do
ENV.fetch("ONE_DRIVE_TEST_OAUTH_CLIENT_ACCESS_TOKEN", "MISSING_ONE_DRIVE_TEST_OAUTH_CLIENT_ACCESS_TOKEN")
end
Expand All @@ -47,10 +48,24 @@
end
end

def use_storages_vcr_cassette(name, options = {}, &)
WebMock.enable! && VCR.turn_on!
VCR.configure do |vcr_config|
vcr_config.cassette_library_dir = STORAGES_CASSETTE_LIBRARY_DIR
end
VCR.use_cassette(name, options, &)
ensure
VCR.turn_off! && WebMock.disable!
end

# Loads files from relative support/ directory
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
config.prepend_before { Storages::Peripherals::Registry.enable_stubs! }
config.append_after { Storages::Peripherals::Registry.unstub }

config.define_derived_metadata(file_path: %r{/modules/storages/spec}) do |metadata|
metadata[:vcr_cassette_library_dir] = STORAGES_CASSETTE_LIBRARY_DIR
end
end
5 changes: 4 additions & 1 deletion spec/support/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def stub_request_with_timeout(method, path_matcher)
end

VCR.configure do |config|
config.cassette_library_dir = "spec/support/fixtures/vcr_cassettes"
config.hook_into :webmock
config.configure_rspec_metadata!
config.before_record do |i|
Expand Down Expand Up @@ -80,6 +79,10 @@ def stub_request_with_timeout(method, path_matcher)
# Only enable VCR's webmock integration for tests tagged with :vcr otherwise interferes with WebMock
# See: https://github.com/vcr/vcr/issues/146
#
VCR.configure do |vcr_config|
cassette_library_dir = example.metadata[:vcr_cassette_library_dir] || "spec/support/fixtures/vcr_cassettes"
vcr_config.cassette_library_dir = cassette_library_dir
end
VCR.turn_on!
example.run
ensure
Expand Down

0 comments on commit a1c27bf

Please sign in to comment.