Skip to content

Instantly share code, notes, and snippets.

@mmirus
Created August 31, 2021 20:44
Show Gist options
  • Save mmirus/acfb378356ede49da38bc53c75883323 to your computer and use it in GitHub Desktop.
Save mmirus/acfb378356ede49da38bc53c75883323 to your computer and use it in GitHub Desktop.
Find and delete unused VCR cassettes
#
# Find and delete unused cassettes
# Save in spec/support/
#
# Modified from examples found at https://github.com/vcr/vcr/issues/283
#
module UsedCassetteReporter
USED_CASSETTES = Set.new
def insert_cassette(name, options = {})
USED_CASSETTES << VCR::Cassette.new(name, options).file
super
end
end
RSpec.configure do |config|
config.after(:suite) do
all_cassettes = Dir[File.join(VCR::Cassette::Persisters::FileSystem.storage_location, '**', '*.yml')].map do |d|
File.expand_path(d)
end
unused_cassettes = all_cassettes - UsedCassetteReporter::USED_CASSETTES.to_a
unless unused_cassettes.empty?
puts "\nUnused cassettes to delete:"
puts unused_cassettes
unused_cassettes.each { |v| File.delete(v) }
end
end
end
@mmirus
Copy link
Author

mmirus commented Aug 31, 2021

This seems like it delete cassettes that are actually being used...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment