Skip to content

Commit

Permalink
Add option to downcase cassette names before saving (vcr#802)
Browse files Browse the repository at this point in the history
* Add option to downcase cassette names before saving
* Update debugging feature spec to include new option
* Document the downcase_cassette_names persister option

Co-Authored-By: Alex Taylor <[email protected]>
Co-authored-by: Olle Jonsson <[email protected]>
  • Loading branch information
mctaylorpants and olleolleolle authored Mar 20, 2020
1 parent c39bcd2 commit 1f70077
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions features/configuration/debug_logging.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Feature: Debug Logging
Given that port numbers in "record.log" are normalized to "7777"
Then the file "record.log" should contain exactly:
"""
[Cassette: 'example'] Initialized with options: {:record=>:once, :record_on_error=>true, :match_requests_on=>[:method, :host, :path], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system}
[Cassette: 'example'] Initialized with options: {:record=>:once, :record_on_error=>true, :match_requests_on=>[:method, :host, :path], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system, :persister_options=>{}}
[webmock] Handling request: [get http://localhost:7777/] (disabled: false)
[Cassette: 'example'] Initialized HTTPInteractionList with request matchers [:method, :host, :path] and 0 interaction(s): { }
[webmock] Identified request type (recordable) for [get http://localhost:7777/]
Expand All @@ -44,7 +44,7 @@ Feature: Debug Logging
Given that port numbers in "playback.log" are normalized to "7777"
Then the file "playback.log" should contain exactly:
"""
[Cassette: 'example'] Initialized with options: {:record=>:once, :record_on_error=>true, :match_requests_on=>[:method, :host, :path], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system}
[Cassette: 'example'] Initialized with options: {:record=>:once, :record_on_error=>true, :match_requests_on=>[:method, :host, :path], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system, :persister_options=>{}}
[webmock] Handling request: [get http://localhost:7777/] (disabled: false)
[Cassette: 'example'] Initialized HTTPInteractionList with request matchers [:method, :host, :path] and 1 interaction(s): { [get http://localhost:7777/] => [200 "Hello World"] }
[Cassette: 'example'] Checking if [get http://localhost:7777/] matches [get http://localhost:7777/] using [:method, :host, :path]
Expand Down
6 changes: 6 additions & 0 deletions lib/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def current_cassette
# @option options :persist_with [Symbol] Which cassette persister to
# use. Defaults to :file_system. You can also register and use a
# custom persister.
# @option options :persister_options [Hash] Pass options to the
# persister specified in `persist_with`. Currently available options for the file_system persister:
# - `:downcase_cassette_names`: when `true`, names of cassettes will be
# normalized in lowercase before reading and writing, which can avoid
# confusion when using both case-sensitive and case-insensitive file
# systems.
# @option options :preserve_exact_body_bytes [Boolean] Whether or not
# to base64 encode the bytes of the requests and responses for this cassette
# when serializing it. See also `VCR::Configuration#preserve_exact_body_bytes`.
Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/cassette.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def assert_valid_options!
:record, :record_on_error, :erb, :match_requests_on, :re_record_interval, :tag, :tags,
:update_content_length_header, :allow_playback_repeats, :allow_unused_http_interactions,
:exclusive, :serialize_with, :preserve_exact_body_bytes, :decode_compressed_response,
:recompress_response, :persist_with, :clean_outdated_http_interactions
:recompress_response, :persist_with, :persister_options, :clean_outdated_http_interactions
]

if invalid_options.size > 0
Expand Down
10 changes: 9 additions & 1 deletion lib/vcr/cassette/persisters/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ def sanitized_file_name_from(file_name)
file_extension = '.' + parts.pop
end

parts.join('.').gsub(/[^[:word:]\-\/]+/, '_') + file_extension.to_s
file_name = parts.join('.').gsub(/[^[:word:]\-\/]+/, '_') + file_extension.to_s
file_name.downcase! if downcase_cassette_names?
file_name
end

def downcase_cassette_names?
!!VCR.configuration
.default_cassette_options
.dig(:persister_options, :downcase_cassette_names)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/vcr/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ def initialize
:match_requests_on => RequestMatcherRegistry::DEFAULT_MATCHERS,
:allow_unused_http_interactions => true,
:serialize_with => :yaml,
:persist_with => :file_system
:persist_with => :file_system,
:persister_options => {}
}

self.uri_parser = URI
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/vcr/cassette/persisters/file_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,24 @@ class Persisters

expected = File.join(FileSystem.storage_location, "\u842c\u570b\u78bc")
expect(FileSystem.absolute_path_to_file("\u842c\u570b\u78bc")).to eq(expected)

expected = File.join(FileSystem.storage_location, "Uppercase_Cassette.yml")
expect(FileSystem.absolute_path_to_file("Uppercase_Cassette.yml")).to eq(expected)
end

it 'handles files with no extensions (even when there is a dot in the path)' do
expected = File.join(FileSystem.storage_location, "/foo_bar/baz_qux")
expect(FileSystem.absolute_path_to_file("/foo.bar/baz qux")).to eq(expected)
end

it 'downcases cassette names if the option is passed' do
VCR.configuration.default_cassette_options.merge!(
{ :persister_options => { :downcase_cassette_names => true } }
)

expected = File.join(FileSystem.storage_location, "/path/to/cassette")
expect(FileSystem.absolute_path_to_file("/pAtH/tO/CaSsEtTe")).to eq(expected)
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/vcr/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
:record => :once,
:record_on_error => true,
:serialize_with => :yaml,
:persist_with => :file_system
:persist_with => :file_system,
:persister_options => {}
})
end

Expand Down

0 comments on commit 1f70077

Please sign in to comment.