Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Forked from anonymous/error.log
Created December 12, 2012 06:35
Show Gist options
  • Save myronmarston/4265543 to your computer and use it in GitHub Desktop.
Save myronmarston/4265543 to your computer and use it in GitHub Desktop.
require 'fileutils'
module Cio
class Bdd
# The only built-in cassette persister. Persists cassettes to the file system.
class FakeFSFileSystem
#extend self
# @private
attr_reader :storage_location
def initialize(dir)
@storage_location=dir
end
# @private
def storage_location=(dir)
puts 'is this called?'
::FakeFS.deactivate! if ::FakeFS.activated?
::FileUtils.mkdir_p(dir) if dir
@storage_location = dir ? absolute_path_for(dir) : nil
::FakeFS.activate!
@storage_location
end
# Gets the cassette for the given storage key (file name).
#
# @param [String] file_name the file name
# @return [String] the cassette content
def [](file_name)
::FakeFS.deactivate! if ::FakeFS.activated?
path = absolute_path_to_file(file_name)
return nil unless ::File.exist?(path)
result = ::File.read(path)
::FakeFS.activate!
result
end
# Sets the cassette for the given storage key (file name).
#
# @param [String] file_name the file name
# @param [String] content the content to store
def []=(file_name, content)
::FakeFS.deactivate! if ::FakeFS.activated?
path = absolute_path_to_file(file_name)
directory = File.dirname(path)
FileUtils.mkdir_p(directory) unless File.exist?(directory)
result = File.open(path, 'w') { |f| f.write(content) }
::FakeFS.activate!
result
end
# @private
def absolute_path_to_file(file_name)
::FakeFS.deactivate! if ::FakeFS.activated?
return nil unless storage_location
result = ::File.join(storage_location, sanitized_file_name_from(file_name))
::FakeFS.activate!
result
end
private
def absolute_path_for(path)
::FakeFS.deactivate! if ::FakeFS.activated?
result = ::Dir.chdir(path) { ::Dir.pwd }
::FakeFS.activate!
result
end
def sanitized_file_name_from(file_name)
::FakeFS.deactivate! if ::FakeFS.activated?
parts = file_name.to_s.split('.')
file_extension = '.' + parts.pop if parts.size > 1
result = parts.join('.').gsub(/[^\w\-\/]+/, '_') + file_extension.to_s
::FakeFS.activate!
result
end
end
end
end
require 'spec_helper'
def make_http_request(url)
::Typhoeus.get(url).body
end
def here(fname)
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
end
describe "VCR example group metadata",:vcr, :type => :fakefs do
let(:url){'http://google.com.au/index.html'}
it 'records an http request' do
make_http_request(url).should match /The document has moved/
end
it 'records another http request' do
make_http_request(url).should match /The document has moved/
end
context 'in a nested example group' do
it 'records another one' do
make_http_request(url).should match /The document has moved/
end
end
end
describe "VCR example metadata" do
let(:url){'http://google.com/index.html'}
it 'records an http request', :vcr do
make_http_request(url).should match /The document has moved/
end
end
# A sample Gemfile
source "http://rubygems.org"
gem 'vcr'
gem 'fakefs'
gem 'typhoeus'
gem 'fakeweb'
gem 'rspec'
gem 'rr'
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
ethon (0.5.7)
ffi (~> 1.2.0)
mime-types (~> 1.18)
fakefs (0.4.2)
fakeweb (1.3.0)
ffi (1.2.0)
mime-types (1.19)
rr (1.0.4)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.1)
rspec-expectations (2.12.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.0)
typhoeus (0.5.3)
ethon (~> 0.5.3)
vcr (2.3.0)
PLATFORMS
ruby
DEPENDENCIES
fakefs
fakeweb
rr
rspec
typhoeus
vcr
Configuring VCR in Rspec for FakeFS
DONE Configuring VCR in Rspec for FakeFS
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Loaded fakefs/spec_helpers
....
Finished in 0.6508 seconds
4 examples, 0 failures
require 'fakeweb'
require 'typhoeus'
require 'vcr'
require 'fakefs_persister'
module ::FakeFSTrigger
def self.included(base)
RSpec.configure do |config|
config.before(:suite) do
require 'fakefs/spec_helpers'
config.include ::FakeFS::SpecHelpers
::FakeFS.activate!
puts ' Loaded fakefs/spec_helpers'
end
config.after(:suite) do
::FakeFS::FileSystem.clear
::FakeFS.deactivate!
end
end
VCR.configure do |c|
begin
require 'fakefs/spec_helpers'
puts 'Configuring VCR in Rspec for FakeFS'
c.allow_http_connections_when_no_cassette = true
c.cassette_library_dir = 'bdd/cassettes'
c.cassette_persisters[:file_system] = ::Cio::Bdd::FakeFSFileSystem.new('bdd/cassettes')
c.default_cassette_options = { :persist_with => :file_system }
c.hook_into :fakeweb, :typhoeus
c.configure_rspec_metadata!
c.ignore_localhost = true
c.default_cassette_options = { :record => :new_episodes }
puts 'DONE Configuring VCR in Rspec for FakeFS'
rescue => e
puts e.message
puts e.backtrace.join '\n'
end
end
end
end
RSpec.configure do |c|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
c.mock_with :rr #::RR::Adapters::Rspec
c.backtrace_clean_patterns.push(::RR::Errors::BACKTRACE_IDENTIFIER)
c.treat_symbols_as_metadata_keys_with_true_values = true
c.run_all_when_everything_filtered = true
c.filter_run :focus
c.alias_it_should_behave_like_to :it_validates, "it validates"
c.include ::FakeFSTrigger, :type => :fakefs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment