Skip to content

Commit

Permalink
Create the cache file subdirectory if it doesn't exist before trying …
Browse files Browse the repository at this point in the history
…to write to it.
  • Loading branch information
myronmarston committed Feb 25, 2010
1 parent ccf4838 commit adea472
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/vcr/sandbox.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'fileutils'
require 'yaml'

module VCR
Expand Down Expand Up @@ -73,6 +74,8 @@ def load_recorded_responses

def write_recorded_responses_to_disk
if VCR::Config.cache_dir && new_recorded_responses.size > 0
directory = File.dirname(cache_file)
FileUtils.mkdir_p directory unless File.exist?(directory)
File.open(cache_file, 'w') { |f| f.write recorded_responses.to_yaml }
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/sandbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@
saved_recorded_responses.should == recorded_responses
end

it "should write the recorded responses a subdirectory if the sandbox name includes a directory" do
recorded_responses = [VCR::RecordedResponse.new(:get, 'http://example.com', :get_example_dot_come_response)]
sandbox = VCR::Sandbox.new('subdirectory/test_sandbox')
sandbox.stub!(:recorded_responses).and_return(recorded_responses)

lambda { sandbox.destroy! }.should change { File.exist?(sandbox.cache_file) }.from(false).to(true)
saved_recorded_responses = File.open(sandbox.cache_file, "r") { |f| YAML.load(f.read) }
saved_recorded_responses.should == recorded_responses
end

it "should write both old and new recorded responses to disk" do
cache_file = File.expand_path(File.dirname(__FILE__) + '/fixtures/sandbox_spec/example.yml')
FileUtils.cp cache_file, File.join(@temp_dir, 'previously_recorded_responses.yml')
Expand Down

0 comments on commit adea472

Please sign in to comment.