vcr 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ end
12
12
  require 'spec/expectations'
13
13
 
14
14
  VCR.config do |c|
15
- c.cache_dir = File.join(File.dirname(__FILE__), '..', 'fixtures', 'vcr_cassettes', RUBY_VERSION)
15
+ c.cassette_library_dir = File.join(File.dirname(__FILE__), '..', 'fixtures', 'vcr_cassettes', RUBY_VERSION)
16
16
  end
17
17
 
18
18
  VCR.module_eval do
@@ -31,25 +31,25 @@ end
31
31
 
32
32
  Before do |scenario|
33
33
  VCR.current_cucumber_scenario = scenario
34
- temp_dir = File.join(VCR::Config.cache_dir, 'temp')
34
+ temp_dir = File.join(VCR::Config.cassette_library_dir, 'temp')
35
35
  FileUtils.rm_rf(temp_dir) if File.exist?(temp_dir)
36
36
  end
37
37
 
38
38
  Before('@copy_not_the_real_response_to_temp') do
39
- orig_file = File.join(VCR::Config.cache_dir, 'not_the_real_response.yml')
40
- temp_file = File.join(VCR::Config.cache_dir, 'temp', 'not_the_real_response.yml')
41
- FileUtils.mkdir_p(File.join(VCR::Config.cache_dir, 'temp'))
39
+ orig_file = File.join(VCR::Config.cassette_library_dir, 'not_the_real_response.yml')
40
+ temp_file = File.join(VCR::Config.cassette_library_dir, 'temp', 'not_the_real_response.yml')
41
+ FileUtils.mkdir_p(File.join(VCR::Config.cassette_library_dir, 'temp'))
42
42
  FileUtils.cp orig_file, temp_file
43
43
  end
44
44
 
45
45
  at_exit do
46
46
  %w(record_cassette1 record_cassette2).each do |tag|
47
- cache_file = File.join(VCR::Config.cache_dir, 'cucumber_tags', "#{tag}.yml")
48
- FileUtils.rm_rf(cache_file) if File.exist?(cache_file)
47
+ file = File.join(VCR::Config.cassette_library_dir, 'cucumber_tags', "#{tag}.yml")
48
+ FileUtils.rm_rf(file) if File.exist?(file)
49
49
  end
50
50
  end
51
51
 
52
52
  VCR.cucumber_tags do |t|
53
- t.tags '@record_cassette1', '@record_cassette2', :record => :unregistered
53
+ t.tags '@record_cassette1', '@record_cassette2', :record => :new_episodes
54
54
  t.tags '@replay_cassette1', '@replay_cassette2', '@replay_cassette3', :record => :none
55
55
  end
data/lib/vcr.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'vcr/cassette'
2
2
  require 'vcr/config'
3
3
  require 'vcr/cucumber_tags'
4
+ require 'vcr/deprecations'
4
5
  require 'vcr/recorded_response'
5
6
 
6
7
  require 'vcr/extensions/fake_web'
@@ -14,23 +15,23 @@ module VCR
14
15
  cassettes.last
15
16
  end
16
17
 
17
- def create_cassette!(*args)
18
+ def insert_cassette(*args)
18
19
  cassette = Cassette.new(*args)
19
20
  cassettes.push(cassette)
20
21
  cassette
21
22
  end
22
23
 
23
- def destroy_cassette!
24
+ def eject_cassette
24
25
  cassette = cassettes.pop
25
- cassette.destroy! if cassette
26
+ cassette.eject if cassette
26
27
  cassette
27
28
  end
28
29
 
29
- def with_cassette(*args)
30
- create_cassette!(*args)
30
+ def use_cassette(*args)
31
+ insert_cassette(*args)
31
32
  yield
32
33
  ensure
33
- destroy_cassette!
34
+ eject_cassette
34
35
  end
35
36
 
36
37
  def config
data/lib/vcr/cassette.rb CHANGED
@@ -3,20 +3,21 @@ require 'yaml'
3
3
 
4
4
  module VCR
5
5
  class Cassette
6
- VALID_RECORD_MODES = [:all, :none, :unregistered].freeze
6
+ VALID_RECORD_MODES = [:all, :none, :new_episodes].freeze
7
7
 
8
8
  attr_reader :name, :record_mode
9
9
 
10
10
  def initialize(name, options = {})
11
11
  @name = name
12
12
  @record_mode = options[:record] || VCR::Config.default_cassette_options[:record]
13
+ deprecate_unregistered_record_mode
13
14
  @allow_real_http_lambda = allow_real_http_lambda_for(options[:allow_real_http] || VCR::Config.default_cassette_options[:allow_real_http])
14
15
  self.class.raise_error_unless_valid_record_mode(record_mode)
15
16
  set_fakeweb_allow_net_connect
16
17
  load_recorded_responses
17
18
  end
18
19
 
19
- def destroy!
20
+ def eject
20
21
  write_recorded_responses_to_disk
21
22
  deregister_original_recorded_responses
22
23
  restore_fakeweb_allow_net_conect
@@ -30,8 +31,8 @@ module VCR
30
31
  recorded_responses << recorded_response
31
32
  end
32
33
 
33
- def cache_file
34
- File.join(VCR::Config.cache_dir, "#{name.to_s.gsub(/[^\w\-\/]+/, '_')}.yml") if VCR::Config.cache_dir
34
+ def file
35
+ File.join(VCR::Config.cassette_library_dir, "#{name.to_s.gsub(/[^\w\-\/]+/, '_')}.yml") if VCR::Config.cassette_library_dir
35
36
  end
36
37
 
37
38
  def self.raise_error_unless_valid_record_mode(record_mode)
@@ -44,14 +45,12 @@ module VCR
44
45
  @allow_real_http_lambda ? @allow_real_http_lambda.call(uri) : false
45
46
  end
46
47
 
47
- private
48
-
49
48
  def new_recorded_responses
50
49
  recorded_responses - @original_recorded_responses
51
50
  end
52
51
 
53
52
  def should_allow_net_connect?
54
- [:unregistered, :all].include?(record_mode)
53
+ [:new_episodes, :all].include?(record_mode)
55
54
  end
56
55
 
57
56
  def set_fakeweb_allow_net_connect
@@ -67,8 +66,8 @@ module VCR
67
66
  @original_recorded_responses = []
68
67
  return if record_mode == :all
69
68
 
70
- if cache_file
71
- @original_recorded_responses = File.open(cache_file, 'r') { |f| YAML.load(f.read) } if File.exist?(cache_file)
69
+ if file
70
+ @original_recorded_responses = File.open(file, 'r') { |f| YAML.load(f.read) } if File.exist?(file)
72
71
  recorded_responses.replace(@original_recorded_responses)
73
72
  end
74
73
 
@@ -86,10 +85,10 @@ module VCR
86
85
  end
87
86
 
88
87
  def write_recorded_responses_to_disk
89
- if VCR::Config.cache_dir && new_recorded_responses.size > 0
90
- directory = File.dirname(cache_file)
88
+ if VCR::Config.cassette_library_dir && new_recorded_responses.size > 0
89
+ directory = File.dirname(file)
91
90
  FileUtils.mkdir_p directory unless File.exist?(directory)
92
- File.open(cache_file, 'w') { |f| f.write recorded_responses.to_yaml }
91
+ File.open(file, 'w') { |f| f.write recorded_responses.to_yaml }
93
92
  end
94
93
  end
95
94
 
data/lib/vcr/config.rb CHANGED
@@ -3,21 +3,16 @@ require 'fileutils'
3
3
  module VCR
4
4
  class Config
5
5
  class << self
6
- attr_reader :cache_dir
7
- def cache_dir=(cache_dir)
8
- @cache_dir = cache_dir
9
- FileUtils.mkdir_p(cache_dir) if cache_dir
6
+ attr_reader :cassette_library_dir
7
+ def cassette_library_dir=(cassette_library_dir)
8
+ @cassette_library_dir = cassette_library_dir
9
+ FileUtils.mkdir_p(cassette_library_dir) if cassette_library_dir
10
10
  end
11
11
 
12
12
  attr_writer :default_cassette_options
13
13
  def default_cassette_options
14
14
  @default_cassette_options ||= {}
15
15
  end
16
-
17
- def default_cassette_record_mode=(value)
18
- warn %Q{WARNING: #default_cassette_record_mode is deprecated. Instead, use: "default_cassette_options = { :record => :#{value.to_s} }"}
19
- default_cassette_options.merge!(:record => value)
20
- end
21
16
  end
22
17
  end
23
18
  end
@@ -23,11 +23,11 @@ module VCR
23
23
 
24
24
  @main_object.instance_eval do
25
25
  Before(tag_name) do
26
- VCR.create_cassette!(cassette_name, options)
26
+ VCR.insert_cassette(cassette_name, options)
27
27
  end
28
28
 
29
29
  After(tag_name) do
30
- VCR.destroy_cassette!
30
+ VCR.eject_cassette
31
31
  end
32
32
  end
33
33
  self.class.add_tag(tag_name)
@@ -0,0 +1,54 @@
1
+ module VCR
2
+ def create_cassette!(*args)
3
+ warn "WARNING: VCR.create_cassette! is deprecated. Instead, use: VCR.insert_cassette."
4
+ insert_cassette(*args)
5
+ end
6
+
7
+ def destroy_cassette!(*args)
8
+ warn "WARNING: VCR.destroy_cassette! is deprecated. Instead, use: VCR.eject_cassette."
9
+ eject_cassette(*args)
10
+ end
11
+
12
+ def with_cassette(*args, &block)
13
+ warn "WARNING: VCR.with_cassette is deprecated. Instead, use: VCR.use_cassette."
14
+ use_cassette(*args, &block)
15
+ end
16
+
17
+ class Cassette
18
+ def destroy!(*args)
19
+ warn "WARNING: VCR::Cassette#destroy! is deprecated. Instead, use: VCR::Cassette#eject."
20
+ eject(*args)
21
+ end
22
+
23
+ def cache_file(*args)
24
+ warn "WARNING: VCR::Cassette#cache_file is deprecated. Instead, use: VCR::Cassette#file."
25
+ file(*args)
26
+ end
27
+
28
+ private
29
+
30
+ def deprecate_unregistered_record_mode
31
+ if @record_mode == :unregistered
32
+ @record_mode = :new_episodes
33
+ Kernel.warn "WARNING: VCR's :unregistered record mode is deprecated. Instead, use: :new_episodes."
34
+ end
35
+ end
36
+ end
37
+
38
+ class Config
39
+ def self.cache_dir(*args)
40
+ warn "WARNING: VCR::Config.cache_dir is deprecated. Instead, use: VCR::Config.cassette_library_dir."
41
+ cassette_library_dir(*args)
42
+ end
43
+
44
+ def self.cache_dir=(value)
45
+ warn "WARNING: VCR::Config.cache_dir= is deprecated. Instead, use: VCR::Config.cassette_library_dir=."
46
+ self.cassette_library_dir = value
47
+ end
48
+
49
+ def self.default_cassette_record_mode=(value)
50
+ warn %Q{WARNING: #default_cassette_record_mode is deprecated. Instead, use: "default_cassette_options = { :record => :#{value.to_s} }"}
51
+ default_cassette_options.merge!(:record => value)
52
+ end
53
+ end
54
+ end
@@ -1,28 +1,28 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe VCR::Cassette do
4
- describe '#cache_file' do
5
- temp_dir File.expand_path(File.dirname(__FILE__) + '/fixtures/cache_file'), :assign_to_cache_dir => true
4
+ describe '#file' do
5
+ temp_dir File.expand_path(File.dirname(__FILE__) + '/fixtures/file'), :assign_to_cassette_library_dir => true
6
6
 
7
- it 'combines the cache_dir with the cassette name' do
8
- cassette = VCR::Cassette.new('the_cache_file')
9
- cassette.cache_file.should == File.join(VCR::Config.cache_dir, 'the_cache_file.yml')
7
+ it 'combines the cassette_library_dir with the cassette name' do
8
+ cassette = VCR::Cassette.new('the_file')
9
+ cassette.file.should == File.join(VCR::Config.cassette_library_dir, 'the_file.yml')
10
10
  end
11
11
 
12
12
  it 'strips out disallowed characters so that it is a valid file name with no spaces' do
13
13
  cassette = VCR::Cassette.new("\nthis \t! is-the_13212_file name")
14
- cassette.cache_file.should =~ /#{Regexp.escape('_this_is-the_13212_file_name.yml')}$/
14
+ cassette.file.should =~ /#{Regexp.escape('_this_is-the_13212_file_name.yml')}$/
15
15
  end
16
16
 
17
17
  it 'keeps any path separators' do
18
18
  cassette = VCR::Cassette.new("dir/file_name")
19
- cassette.cache_file.should =~ /#{Regexp.escape('dir/file_name.yml')}$/
19
+ cassette.file.should =~ /#{Regexp.escape('dir/file_name.yml')}$/
20
20
  end
21
21
 
22
- it 'returns nil if the cache_dir is not set' do
23
- VCR::Config.cache_dir = nil
24
- cassette = VCR::Cassette.new('the_cache_file')
25
- cassette.cache_file.should be_nil
22
+ it 'returns nil if the cassette_library_dir is not set' do
23
+ VCR::Config.cassette_library_dir = nil
24
+ cassette = VCR::Cassette.new('the_file')
25
+ cassette.file.should be_nil
26
26
  end
27
27
  end
28
28
 
@@ -49,7 +49,7 @@ describe VCR::Cassette do
49
49
  end
50
50
  end
51
51
 
52
- { :unregistered => true, :all => true, :none => false }.each do |record_mode, allow_fakeweb_connect|
52
+ { :new_episodes => true, :all => true, :none => false }.each do |record_mode, allow_fakeweb_connect|
53
53
  it "sets FakeWeb.allow_net_connect to #{allow_fakeweb_connect} when the record mode is #{record_mode}" do
54
54
  FakeWeb.allow_net_connect = !allow_fakeweb_connect
55
55
  VCR::Cassette.new(:name, :record => record_mode)
@@ -57,9 +57,9 @@ describe VCR::Cassette do
57
57
  end
58
58
  end
59
59
 
60
- { :unregistered => true, :all => false, :none => true }.each do |record_mode, load_responses|
61
- it "#{load_responses ? 'loads' : 'does not load'} the recorded responses from the cached yml file when the record mode is #{record_mode}" do
62
- VCR::Config.cache_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
60
+ { :new_episodes => true, :all => false, :none => true }.each do |record_mode, load_responses|
61
+ it "#{load_responses ? 'loads' : 'does not load'} the recorded responses from the library yml file when the record mode is #{record_mode}" do
62
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
63
63
  cassette = VCR::Cassette.new('example', :record => record_mode)
64
64
 
65
65
  if load_responses
@@ -84,7 +84,7 @@ describe VCR::Cassette do
84
84
  end
85
85
 
86
86
  it "#{load_responses ? 'registers' : 'does not register'} the recorded responses with fakeweb when the record mode is #{record_mode}" do
87
- VCR::Config.cache_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
87
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
88
88
  cassette = VCR::Cassette.new('example', :record => record_mode)
89
89
 
90
90
  rr1 = FakeWeb.response_for(:get, "http://example.com")
@@ -141,14 +141,14 @@ describe VCR::Cassette do
141
141
  end
142
142
  end
143
143
 
144
- describe '#destroy!' do
145
- temp_dir File.expand_path(File.dirname(__FILE__) + '/fixtures/cassette_spec_destroy'), :assign_to_cache_dir => true
144
+ describe '#eject' do
145
+ temp_dir File.expand_path(File.dirname(__FILE__) + '/fixtures/cassette_spec_eject'), :assign_to_cassette_library_dir => true
146
146
 
147
147
  [true, false].each do |orig_allow_net_connect|
148
- it "resets FakeWeb.allow_net_connect #{orig_allow_net_connect} if it was originally #{orig_allow_net_connect}" do
148
+ it "resets FakeWeb.allow_net_connect to #{orig_allow_net_connect} if it was originally #{orig_allow_net_connect}" do
149
149
  FakeWeb.allow_net_connect = orig_allow_net_connect
150
150
  cassette = VCR::Cassette.new(:name)
151
- cassette.destroy!
151
+ cassette.eject
152
152
  FakeWeb.allow_net_connect?.should == orig_allow_net_connect
153
153
  end
154
154
  end
@@ -160,55 +160,55 @@ describe VCR::Cassette do
160
160
  VCR::RecordedResponse.new(:get, 'http://google.com', :get_google_dot_come_response)
161
161
  ]
162
162
 
163
- cassette = VCR::Cassette.new(:destroy_test)
163
+ cassette = VCR::Cassette.new(:eject_test)
164
164
  cassette.stub!(:recorded_responses).and_return(recorded_responses)
165
165
 
166
- lambda { cassette.destroy! }.should change { File.exist?(cassette.cache_file) }.from(false).to(true)
167
- saved_recorded_responses = File.open(cassette.cache_file, "r") { |f| YAML.load(f.read) }
166
+ lambda { cassette.eject }.should change { File.exist?(cassette.file) }.from(false).to(true)
167
+ saved_recorded_responses = File.open(cassette.file, "r") { |f| YAML.load(f.read) }
168
168
  saved_recorded_responses.should == recorded_responses
169
169
  end
170
170
 
171
- it "writes the recorded responses a subdirectory if the cassette name includes a directory" do
171
+ it "writes the recorded responses to a subdirectory if the cassette name includes a directory" do
172
172
  recorded_responses = [VCR::RecordedResponse.new(:get, 'http://example.com', :get_example_dot_come_response)]
173
173
  cassette = VCR::Cassette.new('subdirectory/test_cassette')
174
174
  cassette.stub!(:recorded_responses).and_return(recorded_responses)
175
175
 
176
- lambda { cassette.destroy! }.should change { File.exist?(cassette.cache_file) }.from(false).to(true)
177
- saved_recorded_responses = File.open(cassette.cache_file, "r") { |f| YAML.load(f.read) }
176
+ lambda { cassette.eject }.should change { File.exist?(cassette.file) }.from(false).to(true)
177
+ saved_recorded_responses = File.open(cassette.file, "r") { |f| YAML.load(f.read) }
178
178
  saved_recorded_responses.should == recorded_responses
179
179
  end
180
180
 
181
181
  it "writes both old and new recorded responses to disk" do
182
- cache_file = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec/example.yml")
183
- FileUtils.cp cache_file, File.join(@temp_dir, 'previously_recorded_responses.yml')
182
+ file = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec/example.yml")
183
+ FileUtils.cp file, File.join(@temp_dir, 'previously_recorded_responses.yml')
184
184
  cassette = VCR::Cassette.new('previously_recorded_responses')
185
185
  cassette.should have(3).recorded_responses
186
186
  new_recorded_response = VCR::RecordedResponse.new(:get, 'http://example.com/bar', :example_dot_com_bar_response)
187
187
  cassette.store_recorded_response!(new_recorded_response)
188
- cassette.destroy!
189
- saved_recorded_responses = File.open(cassette.cache_file, "r") { |f| YAML.load(f.read) }
188
+ cassette.eject
189
+ saved_recorded_responses = File.open(cassette.file, "r") { |f| YAML.load(f.read) }
190
190
  saved_recorded_responses.should have(4).recorded_responses
191
191
  saved_recorded_responses.last.should == new_recorded_response
192
192
  end
193
193
  end
194
194
 
195
- describe '#destroy for a cassette with previously recorded responses' do
195
+ describe '#eject for a cassette with previously recorded responses' do
196
196
  it "de-registers the recorded responses from fakeweb" do
197
- VCR::Config.cache_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
197
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
198
198
  cassette = VCR::Cassette.new('example', :record => :none)
199
199
  FakeWeb.registered_uri?(:get, 'http://example.com').should be_true
200
200
  FakeWeb.registered_uri?(:get, 'http://example.com/foo').should be_true
201
- cassette.destroy!
201
+ cassette.eject
202
202
  FakeWeb.registered_uri?(:get, 'http://example.com').should be_false
203
203
  FakeWeb.registered_uri?(:get, 'http://example.com/foo').should be_false
204
204
  end
205
205
 
206
206
  it "does not re-write to disk the previously recorded resposes if there are no new ones" do
207
- VCR::Config.cache_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
208
- yaml_file = File.join(VCR::Config.cache_dir, 'example.yml')
207
+ VCR::Config.cassette_library_dir = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{RUBY_VERSION}/cassette_spec")
208
+ yaml_file = File.join(VCR::Config.cassette_library_dir, 'example.yml')
209
209
  cassette = VCR::Cassette.new('example', :record => :none)
210
- File.should_not_receive(:open).with(cassette.cache_file, 'w')
211
- lambda { cassette.destroy! }.should_not change { File.mtime(yaml_file) }
210
+ File.should_not_receive(:open).with(cassette.file, 'w')
211
+ lambda { cassette.eject }.should_not change { File.mtime(yaml_file) }
212
212
  end
213
213
  end
214
214
  end
data/spec/config_spec.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe VCR::Config do
4
- describe '#cache_dir=' do
4
+ describe '#cassette_library_dir=' do
5
5
  temp_dir(File.expand_path(File.dirname(__FILE__) + '/fixtures/config_spec'))
6
6
 
7
7
  it 'creates the directory if it does not exist' do
8
- lambda { VCR::Config.cache_dir = @temp_dir }.should change { File.exist?(@temp_dir) }.from(false).to(true)
8
+ lambda { VCR::Config.cassette_library_dir = @temp_dir }.should change { File.exist?(@temp_dir) }.from(false).to(true)
9
9
  end
10
10
 
11
11
  it 'does not raise an error if given nil' do
12
- lambda { VCR::Config.cache_dir = nil }.should_not raise_error
12
+ lambda { VCR::Config.cassette_library_dir = nil }.should_not raise_error
13
13
  end
14
14
  end
15
15
 
@@ -19,29 +19,4 @@ describe VCR::Config do
19
19
  VCR::Config.default_cassette_options.should == {}
20
20
  end
21
21
  end
22
-
23
- describe '#default_cassette_record_mode=' do
24
- disable_warnings
25
-
26
- it 'sets the default_cassette_options[:record] option' do
27
- VCR::Cassette::VALID_RECORD_MODES.each do |mode|
28
- VCR::Config.default_cassette_options = nil
29
- VCR::Config.default_cassette_record_mode = mode
30
- VCR::Config.default_cassette_options[:record].should == mode
31
- end
32
- end
33
-
34
- it 'merges the :record option with the existing default_cassette_record options' do
35
- VCR::Config.default_cassette_options = { :an => :option }
36
- VCR::Config.default_cassette_record_mode = :all
37
- VCR::Config.default_cassette_options.should == { :an => :option, :record => :all }
38
- end
39
-
40
- it 'warns the user that it is deprecated' do
41
- VCR::Cassette::VALID_RECORD_MODES.each do |mode|
42
- VCR::Config.should_receive(:warn).with(%Q{WARNING: #default_cassette_record_mode is deprecated. Instead, use: "default_cassette_options = { :record => :#{mode.to_s} }"})
43
- VCR::Config.default_cassette_record_mode = mode
44
- end
45
- end
46
- end
47
22
  end