Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FakeFS.deactivate! not effective in a VCR persister #167

Closed
taqtiqa-mark opened this issue Dec 14, 2012 · 1 comment
Closed

FakeFS.deactivate! not effective in a VCR persister #167

taqtiqa-mark opened this issue Dec 14, 2012 · 1 comment

Comments

@taqtiqa-mark
Copy link

Hi,
I've encountered an issue where FakeFS.deactivate! does not seem to deactivate FakeFS.

This gist should exhibit the problem (included ./bundle/config should isolate the gems):

https://gist.github.com/4274138

git clone https://gist.github.com/4274138.git
cd 4274138
bundle install
bundle exec rspec spec
cd ..

Essentially the bdd/cassettes folder is empty and it should be populated:

$ ls -la bdd/cassettes/
total 0
drwxr-sr-x 2 hedge 1000  6 2012-12-13 17:17 .
drwxr-sr-x 3 hedge 1000 22 2012-12-13 17:17 ..

@ariofrio appreciate any insights you might have.

@eventualbuddha
Copy link
Contributor

It looks like the problem with your VCR persister is that you're trying to reinvent FakeFS.without. Here are the changes to your gist that make it work:

diff --git a/lib/fakefs_persister.rb b/lib/fakefs_persister.rb
index 8c94cef..2ce5533 100644
--- a/lib/fakefs_persister.rb
+++ b/lib/fakefs_persister.rb
@@ -16,11 +16,10 @@ module Cio
         # @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
+          ::FakeFS.without do
+            ::FileUtils.mkdir_p(dir) if dir
+            @storage_location = dir ? absolute_path_for(dir) : nil
+          end
         end

         # Gets the cassette for the given storage key (file name).
@@ -28,12 +27,11 @@ module Cio
         # @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
+          ::FakeFS.without do
+            path = absolute_path_to_file(file_name)
+            return nil unless ::File.exist?(path)
+            ::File.read(path)
+          end
         end

         # Sets the cassette for the given storage key (file name).
@@ -41,41 +39,37 @@ module Cio
         # @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
+          ::FakeFS.without do
+            path = absolute_path_to_file(file_name)
+            directory = File.dirname(path)
+            FileUtils.mkdir_p(directory) unless File.exist?(directory)
+            File.open(path, 'w') { |f| f.write(content) }
+          end
         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
+          ::FakeFS.without do
+            return nil unless storage_location
+            ::File.join(storage_location, sanitized_file_name_from(file_name))
+          end
         end

         private

         def absolute_path_for(path)
-          ::FakeFS.deactivate! if ::FakeFS.activated?
-          result = ::Dir.chdir(path) { ::Dir.pwd }
-          ::FakeFS.activate!
-          result
+          ::FakeFS.without do
+            ::Dir.chdir(path) { ::Dir.pwd }
+          end
         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
+          FakeFS.without do
+            parts = file_name.to_s.split('.')
+            file_extension = '.' + parts.pop if parts.size > 1
+            parts.join('.').gsub(/[^\w\-\/]+/, '_') + file_extension.to_s
+          end
         end
     end
   end
-end
\ No newline at end of file
+end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants