Skip to content

Instantly share code, notes, and snippets.

@mmirus
Created August 31, 2021 20:44
Show Gist options
  • Select an option

  • Save mmirus/acfb378356ede49da38bc53c75883323 to your computer and use it in GitHub Desktop.

Select an option

Save mmirus/acfb378356ede49da38bc53c75883323 to your computer and use it in GitHub Desktop.

Revisions

  1. mmirus created this gist Aug 31, 2021.
    30 changes: 30 additions & 0 deletions vcr_unused_cassette_reporter.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #
    # 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