vcr-unused_cassettes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2af05df07a5be2c9da47dfc5bfb67ed5d86e016ff456fbe6862454630f294a60
4
+ data.tar.gz: db5c1c3ba677b989ea8b5ce708209262cfe99bcfb7ba8ff1f561fdb54c5d13a4
5
+ SHA512:
6
+ metadata.gz: cf304b9c1f3730acbdb05690187cace477ad54f37ad424444422e9073585050ab9662eb29b4f0441595a18a45fb4f655e7f5cfa5a135cdae341dd1fa069462ed
7
+ data.tar.gz: 13b75a92af78bc193225d1664eb8633ecc6bfda0740b22f2d6523d1f50443bed6be63c15e4e8585afb68598984456820f8c382c3e24abd0be6a9692894baf551
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.2
data/.standard.yml ADDED
@@ -0,0 +1 @@
1
+ parallel: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2025-01-16
4
+
5
+ Initial release:
6
+ - support for minitest
7
+ - basic interpretation of variables and constants in the `VCR.use_cassette` call
8
+ - support for multiple persisters and serializers
9
+ - rake task to check for unused cassettes
10
+ - rake tasks to remove unused cassettes
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 9265647+jbockler@users.noreply.github.com . All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,17 @@
1
+ # Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jbockler/vcr-unused_cassettes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jbockler/vcr-unused_cassettes/blob/master/CODE_OF_CONDUCT.md).
4
+
5
+
6
+ ## Development
7
+
8
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
9
+
10
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
11
+
12
+
13
+ ## Pull Requests
14
+ * Fork the repository
15
+ * Create a feature branch
16
+ * Write [good commit messages](https://cbea.ms/git-commit/)
17
+ * comply with the standardrb style
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ ruby File.read(".ruby-version").strip
6
+
7
+ # Specify your gem's dependencies in vcr-unused_cassettes.gemspec
8
+ gemspec
9
+
10
+ gem "rake", "~> 13.0"
11
+
12
+ gem "rspec", "~> 3.0"
13
+
14
+ gem "standard", "~> 1.43"
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vcr-unused_cassettes (1.0.0)
5
+ vcr
6
+ zeitwerk
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.1)
13
+ json (2.9.1)
14
+ language_server-protocol (3.17.0.3)
15
+ lint_roller (1.1.0)
16
+ parallel (1.26.3)
17
+ parser (3.3.6.0)
18
+ ast (~> 2.4.1)
19
+ racc
20
+ racc (1.8.1)
21
+ rainbow (3.1.1)
22
+ rake (13.1.0)
23
+ regexp_parser (2.10.0)
24
+ rspec (3.13.0)
25
+ rspec-core (~> 3.13.0)
26
+ rspec-expectations (~> 3.13.0)
27
+ rspec-mocks (~> 3.13.0)
28
+ rspec-core (3.13.0)
29
+ rspec-support (~> 3.13.0)
30
+ rspec-expectations (3.13.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.13.0)
33
+ rspec-mocks (3.13.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.13.0)
36
+ rspec-support (3.13.1)
37
+ rubocop (1.69.2)
38
+ json (~> 2.3)
39
+ language_server-protocol (>= 3.17.0)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.3.0.2)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 2.9.3, < 3.0)
44
+ rubocop-ast (>= 1.36.2, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 2.4.0, < 4.0)
47
+ rubocop-ast (1.37.0)
48
+ parser (>= 3.3.1.0)
49
+ rubocop-performance (1.23.0)
50
+ rubocop (>= 1.48.1, < 2.0)
51
+ rubocop-ast (>= 1.31.1, < 2.0)
52
+ ruby-progressbar (1.13.0)
53
+ standard (1.43.0)
54
+ language_server-protocol (~> 3.17.0.2)
55
+ lint_roller (~> 1.0)
56
+ rubocop (~> 1.69.1)
57
+ standard-custom (~> 1.0.0)
58
+ standard-performance (~> 1.6)
59
+ standard-custom (1.0.2)
60
+ lint_roller (~> 1.0)
61
+ rubocop (~> 1.50)
62
+ standard-performance (1.6.0)
63
+ lint_roller (~> 1.1)
64
+ rubocop-performance (~> 1.23.0)
65
+ unicode-display_width (3.1.3)
66
+ unicode-emoji (~> 4.0, >= 4.0.4)
67
+ unicode-emoji (4.0.4)
68
+ vcr (6.2.0)
69
+ zeitwerk (2.7.1)
70
+
71
+ PLATFORMS
72
+ arm64-darwin-21
73
+ arm64-darwin-23
74
+
75
+ DEPENDENCIES
76
+ rake (~> 13.0)
77
+ rspec (~> 3.0)
78
+ standard (~> 1.43)
79
+ vcr-unused_cassettes!
80
+
81
+ RUBY VERSION
82
+ ruby 3.3.2p78
83
+
84
+ BUNDLED WITH
85
+ 2.3.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Josch Bockler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Vcr::UnusedCassettes
2
+
3
+ This gem provides a way to detect unused [VCR](https://github.com/vcr/vcr) cassettes. It is intended to be used in a test suite to ensure that all VCR cassettes are being used. You could run this in your CI pipeline to ensure that no cassettes are being left behind.
4
+
5
+
6
+ ## Pre-requisites
7
+
8
+ This gem currently only is supposed to work with minitest, but rspec and cucumber support is planned.
9
+
10
+ The interpretation of which cassettes are being used is pretty simple at the moment. Only string literals and some basic use of variables and constants is interpreted at the moment. If you do some more magic with the `VCR.use_cassette` calls, it is possible that you get false positives. If you encounter such a case, please open a pr or an issue with a code example.
11
+
12
+ ## Installation
13
+
14
+ Install the gem and add to the application's Gemfile by executing:
15
+
16
+ $ bundle add vcr-unused_cassettes --group "development, test"
17
+
18
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
+
20
+ $ gem install vcr-unused_cassettes
21
+
22
+ This gem require the VCR gem to be installed and configured in the same environment too.
23
+
24
+ Additionally you have to load the rake tasks in your application. Add the following line to your `Rakefile`:
25
+
26
+ ```ruby
27
+ require "vcr-unused_cassettes"
28
+
29
+ spec = Gem::Specification.find_by_name
30
+ rakefile = "#{spec.gem_dir}/lib/vcr/unused_cassettes/Rakefile"
31
+ load rakefile
32
+ ```
33
+
34
+ If you are using Rails you don't have to do that. This gem ships with a Railtie that automatically loads the rake tasks.
35
+
36
+ ## Usage
37
+
38
+ This gem provides 2 tasks:
39
+
40
+ ### Check for unused cassettes
41
+ $ rails vcr:unused_cassettes:check
42
+
43
+ ### Remove unused cassettes
44
+ $ rails vcr:unused_cassettes:remove
45
+
46
+ ## Roadmap
47
+ Open for contributions. Some ideas what could be done in the future:
48
+ - automate manual tests into rspec tests
49
+ - find usages of cassettes when test name is used
50
+ - Add support for rspec and cucumber
51
+ - create multiple run configurations to use e.g. minitest and cucumber in the same repo
52
+ - fancy spinner with progress indicator (configurable for ci environments)
53
+ - parallelize the analysis of the cassettes
54
+
55
+ ## Contributing
56
+
57
+ Contributions and ideas are welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
58
+
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the Vcr::UnusedCassettes project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jbockler/vcr-unused_cassettes/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,4 @@
1
+ require "vcr/unused_cassettes"
2
+
3
+ path = File.expand_path(__dir__)
4
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VCR::UnusedCassettes
4
+ class CallContext
5
+ ValueUnresolveable = Class.new(StandardError)
6
+
7
+ def initialize
8
+ @context = {
9
+ variables: {},
10
+ constants: {}
11
+ }
12
+ end
13
+
14
+ def track(node)
15
+ reset_local_variables! if new_test_node?(node)
16
+ case node.type
17
+ when :local_variable_write_node
18
+ store_variable(node.name, node.value)
19
+ when :constant_write_node
20
+ store_constant(node.name, node.value)
21
+ when :instance_variable_write_node
22
+ store_variable(node.name, node.value)
23
+ end
24
+ end
25
+
26
+ def resolve_variable(variable_name)
27
+ @context.dig(:variables, variable_name)
28
+ end
29
+
30
+ def resolve_constant(constant_name)
31
+ @context.dig(:constants, constant_name)
32
+ end
33
+
34
+ def extract_value(node, string_interpolation_error: :raise)
35
+ case node.type
36
+ when :nil_node
37
+ nil
38
+ when :string_node
39
+ node.unescaped
40
+ when :symbol_node
41
+ node.unescaped.to_sym
42
+ when :hash_node, :keyword_hash_node
43
+ node.elements.each_with_object({}) do |element, hash|
44
+ if element.type == :assoc_splat_node
45
+ hash.merge(extract_value(element.value, string_interpolation_error: string_interpolation_error))
46
+ next
47
+ end
48
+ key = extract_value(element.key, string_interpolation_error: string_interpolation_error)
49
+ value = extract_value(element.value, string_interpolation_error: string_interpolation_error)
50
+ hash[key] = value
51
+ rescue ValueUnresolveable
52
+ next
53
+ end
54
+ when :array_node
55
+ node.elements.map do |element|
56
+ extract_value(element, string_interpolation_error: string_interpolation_error)
57
+ end
58
+ when :interpolated_string_node
59
+ node.parts.map do |part_node|
60
+ if part_node.type == :embedded_statements_node
61
+ if part_node.statements.body.size != 1
62
+ if string_interpolation_error == :raise
63
+ raise ValueUnresolveable, "Could not resolve value for node: #{part_node.inspect}"
64
+ elsif string_interpolation_error == :wildcard
65
+ "*"
66
+ end
67
+ else
68
+ extract_value(part_node.statements.body.first, string_interpolation_error: string_interpolation_error)
69
+ end
70
+ else
71
+ extract_value(part_node, string_interpolation_error: string_interpolation_error)
72
+ end
73
+ rescue ValueUnresolveable
74
+ if string_interpolation_error == :raise
75
+ raise ValueUnresolveable, "Could not resolve value for node: #{part_node.inspect}"
76
+ elsif string_interpolation_error == :wildcard
77
+ "*"
78
+ end
79
+ end.join
80
+ when :local_variable_read_node, :instance_variable_read_node
81
+ resolve_variable(node.name)
82
+ when :constant_read_node
83
+ @context.dig(:constants, node.name)
84
+ when :assoc_splat_node
85
+ extract_value(node.value, string_interpolation_error: string_interpolation_error)
86
+ else
87
+ if node.respond_to?(:value) && !node.value.is_a?(Prism::Node)
88
+ node.value
89
+ else
90
+ raise ValueUnresolveable, "Could not resolve value for node: #{node.inspect}"
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def reset_local_variables!
98
+ @context[:variables].select! { |key, _value| key.start_with?("@") }
99
+ end
100
+
101
+ def store_variable(name, value_node)
102
+ @context[:variables][name] = extract_value(value_node)
103
+ rescue ValueUnresolveable
104
+ # todo add debug logging
105
+ end
106
+
107
+ def store_constant(name, value_node)
108
+ @context[:constants][name] = extract_value(value_node)
109
+ rescue ValueUnresolveable
110
+ # todo add debug logging
111
+ end
112
+
113
+ def new_test_node?(node)
114
+ case node.type
115
+ when :def_node
116
+ if node.name.start_with?("test")
117
+ return true
118
+ end
119
+ when :call_node
120
+ if node.name == :test
121
+ return true
122
+ end
123
+ end
124
+ false
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module VCR::UnusedCassettes
6
+ class CassetteUsageFinder
7
+ attr_accessor :filename, :used_cassettes, :warnings, :call_context
8
+
9
+ def initialize(filename)
10
+ self.filename = filename
11
+ self.used_cassettes = []
12
+ self.warnings = []
13
+ self.call_context = CallContext.new
14
+ end
15
+
16
+ def find_cassette_usages
17
+ parse_result = Prism.parse_file(filename)
18
+ return [[], []] unless parse_result
19
+
20
+ find_cassette_usages_in(parse_result.value)
21
+
22
+ [used_cassettes, warnings]
23
+ end
24
+
25
+ def find_cassette_usages_in(node)
26
+ return if node.nil?
27
+
28
+ call_context.track(node)
29
+
30
+ if node_contains_call?(node)
31
+ found_name = find_cassette_name(node)
32
+ if !found_name.nil? && /[a-zA-Z0-9]/.match?(found_name)
33
+ cassette_use = {pattern: found_name}
34
+ cassette_options = extract_options(node)
35
+ cassette_use[:persister] = cassette_options[:persist_with] if cassette_options&.has_key?(:persist_with)
36
+ cassette_use[:serializer] = cassette_options[:serialize_with] if cassette_options&.has_key?(:serialize_with)
37
+ used_cassettes << cassette_use
38
+ end
39
+ end
40
+
41
+ return if node.child_nodes.nil?
42
+
43
+ node.child_nodes.each do |child_node|
44
+ find_cassette_usages_in(child_node)
45
+ end
46
+ end
47
+
48
+ def extract_options(node)
49
+ return nil if node.arguments.arguments.size <= 1
50
+ node.arguments.arguments.each do |argument_node|
51
+ next unless argument_node.is_a?(Prism::KeywordHashNode)
52
+ return call_context.extract_value(argument_node, string_interpolation_error: :raise)
53
+ end
54
+ nil
55
+ rescue CallContext::ValueUnresolveable => error
56
+ warnings << build_warning(node, error)
57
+ end
58
+
59
+ def node_contains_call?(node)
60
+ return false unless node.is_a?(Prism::CallNode)
61
+
62
+ receiver = node.receiver
63
+ return false unless receiver.is_a?(Prism::ConstantReadNode) && receiver.name == :VCR
64
+
65
+ node.name == :use_cassette
66
+ end
67
+
68
+ def find_cassette_name(node)
69
+ # cassette is the first argument of the use_cassette call
70
+ cassette_argument = node.arguments.arguments.first
71
+ call_context.extract_value(cassette_argument, string_interpolation_error: :wildcard)
72
+ rescue CallContext::ValueUnresolveable => error
73
+ warnings << build_warning(node, error)
74
+ nil
75
+ end
76
+
77
+ def build_warning(node, error)
78
+ Warning.new.tap do |warning|
79
+ warning.message = "Could not determine cassette name for #{filename}:#{node.line}"
80
+ warning.details = error.message
81
+ warning.backtrace = error.backtrace
82
+ end
83
+ end
84
+ end
85
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ require "vcr/unused_cassettes"
2
+ require "rails"
3
+
4
+ module VCR
5
+ module UnusedCassettes
6
+ class Railtie < Rails::Railtie
7
+ railtie_name :vcr_unused_cassettes
8
+
9
+ rake_tasks do
10
+ path = File.expand_path(__dir__)
11
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VCR::UnusedCassettes
4
+ class Runner
5
+ def find_unused_cassettes
6
+ existing_cassettes = find_existing_cassettes
7
+ cassette_uses, warnings = used_cassettes_names_patterns
8
+ cassette_uses.map! { |cassette_use| path_for_cassette_use(cassette_use) }
9
+
10
+ unused_cassettes = existing_cassettes.select do |existing_cassette_name|
11
+ !cassette_uses.any? { |used_cassette_name| File.fnmatch?(used_cassette_name, existing_cassette_name) }
12
+ end
13
+
14
+ [unused_cassettes, warnings]
15
+ end
16
+
17
+ def used_cassettes_names_patterns
18
+ used_cassettes = []
19
+ warnings = []
20
+
21
+ file_list = `grep -r -l "VCR.use_cassette" test/`.split("\n")
22
+ return [[], []] if file_list.empty? || $? != 0
23
+
24
+ file_list.each do |file|
25
+ found_usages, found_warnings = CassetteUsageFinder.new(file).find_cassette_usages
26
+ used_cassettes += found_usages
27
+ warnings += found_warnings
28
+ end
29
+
30
+ [used_cassettes, warnings]
31
+ end
32
+
33
+ def find_existing_cassettes
34
+ cassettes = []
35
+ all_vcr_persisters.each do |_name, persister|
36
+ cassettes += Dir.glob(cassette_path_with_wildcard("*", persister))
37
+ end
38
+ cassettes.uniq
39
+ end
40
+
41
+ def path_for_cassette(cassette_name, persister)
42
+ result = persister.absolute_path_to_file(cassette_name)
43
+ return result.to_s if result
44
+ raise Error, "Could not determine path for cassette #{cassette_name}"
45
+ end
46
+
47
+ def cassette_path_with_wildcard(name, persister)
48
+ placeholder = "a_placeholder_that_should_not_exist"
49
+ full_path = path_for_cassette(name.gsub("*", placeholder), persister)
50
+ full_path.sub(placeholder, "*")
51
+ end
52
+
53
+ def default_persister
54
+ @default_persister ||= begin
55
+ default_persister = VCR.configuration.default_cassette_options[:persist_with]
56
+ VCR.configuration.cassette_persisters[default_persister]
57
+ end
58
+ end
59
+
60
+ def default_or_option(type, option_name, cassette_use)
61
+ cassette_use[type] || VCR.configuration.default_cassette_options[option_name]
62
+ end
63
+
64
+ def path_for_cassette_use(cassette_use)
65
+ persister_name = default_or_option(:persister, :persist_with, cassette_use)
66
+ persister = VCR.configuration.cassette_persisters[persister_name]
67
+
68
+ serializer_name = default_or_option(:serializer, :serialize_with, cassette_use)
69
+ serializer = VCR.cassette_serializers[serializer_name]
70
+ cassette_storage_key = "#{cassette_use[:pattern]}.#{serializer.file_extension}"
71
+ cassette_path_with_wildcard(cassette_storage_key, persister)
72
+ end
73
+
74
+ def all_vcr_persisters
75
+ # dirty, but VCR::Cassette::Persisters does not expose the persisters
76
+ VCR.cassette_persisters.instance_variable_get(:@persisters)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,42 @@
1
+ namespace :vcr do
2
+ namespace :unused_cassettes do
3
+ desc "List unused cassettes"
4
+ task check: :environment do |_task|
5
+ unused_cassettes, warnings = VCR::UnusedCassettes::Runner.new.find_unused_cassettes
6
+
7
+ VCR::UnusedCassettes::Warning.print(warnings)
8
+
9
+ if unused_cassettes.empty?
10
+ puts "Everything is fine! No unused cassettes found."
11
+ exit(true)
12
+ end
13
+
14
+ puts "Unused cassettes:"
15
+ unused_cassettes.each { |cassette| puts cassette.gsub(Dir.pwd, ".") }
16
+ puts "\n"
17
+
18
+ abort("There are #{unused_cassettes.size} unused cassettes")
19
+ end
20
+
21
+ desc "Remove unused cassettes"
22
+ task remove: :environment do |_task|
23
+ unused_cassettes, warnings = VCR::UnusedCassettes::Runner.new.find_unused_cassettes
24
+
25
+ VCR::UnusedCassettes::Warning.print(warnings)
26
+
27
+ if unused_cassettes.empty?
28
+ puts "Everything is fine! No unused cassettes found."
29
+ exit(true)
30
+ end
31
+
32
+ puts "Removing unused cassettes:"
33
+ unused_cassettes.each do |cassette|
34
+ puts cassette.gsub(Dir.pwd, ".")
35
+ File.delete(cassette)
36
+ end
37
+ puts "\n"
38
+
39
+ puts "Removed #{unused_cassettes.size} unused cassettes"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VCR
4
+ module UnusedCassettes
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VCR::UnusedCassettes
4
+ class Warning
5
+ attr_accessor :message, :details, :backtrace
6
+
7
+ def self.print(warnings)
8
+ return if warnings.nil? || warnings.empty?
9
+
10
+ puts "Warnings:"
11
+ warnings.each do |warning|
12
+ puts warning.message
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem_extension(VCR)
6
+ loader.setup
7
+
8
+ module VCR
9
+ module UnusedCassettes
10
+ require_relative "unused_cassettes/railtie" if defined?(Rails)
11
+
12
+ class Error < StandardError; end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module Vcr
2
+ module UnusedCassettes
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/vcr/unused_cassettes/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "vcr-unused_cassettes"
7
+ spec.version = VCR::UnusedCassettes::VERSION
8
+ spec.authors = ["Josch Bockler"]
9
+ spec.email = ["9265647+jbockler@users.noreply.github.com "]
10
+
11
+ spec.summary = "Find your unused VCR cassettes"
12
+ spec.description = "Adds a rake task to find unused VCR cassettes in your project. To check for them in your CI or if you just want to cleanup."
13
+ spec.homepage = "https://github.com/jbockler/vcr-unused_cassettes"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.3.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "vcr"
34
+ spec.add_dependency "zeitwerk"
35
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcr-unused_cassettes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Josch Bockler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vcr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Adds a rake task to find unused VCR cassettes in your project. To check
42
+ for them in your CI or if you just want to cleanup.
43
+ email:
44
+ - '9265647+jbockler@users.noreply.github.com '
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".ruby-version"
51
+ - ".standard.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - CONTRIBUTING.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - lib/vcr/unused_cassettes.rb
61
+ - lib/vcr/unused_cassettes/Rakefile
62
+ - lib/vcr/unused_cassettes/call_context.rb
63
+ - lib/vcr/unused_cassettes/cassette_usage_finder.rb
64
+ - lib/vcr/unused_cassettes/configuration.rb
65
+ - lib/vcr/unused_cassettes/railtie.rb
66
+ - lib/vcr/unused_cassettes/runner.rb
67
+ - lib/vcr/unused_cassettes/tasks/vcr/unused_cassettes.rake
68
+ - lib/vcr/unused_cassettes/version.rb
69
+ - lib/vcr/unused_cassettes/warning.rb
70
+ - sig/vcr/unused_cassettes.rbs
71
+ - vcr-unused_cassettes.gemspec
72
+ homepage: https://github.com/jbockler/vcr-unused_cassettes
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ homepage_uri: https://github.com/jbockler/vcr-unused_cassettes
77
+ source_code_uri: https://github.com/jbockler/vcr-unused_cassettes
78
+ changelog_uri: https://github.com/jbockler/vcr-unused_cassettes/blob/main/CHANGELOG.md
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.3.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.5.9
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Find your unused VCR cassettes
98
+ test_files: []