GithubHelp home page GithubHelp logo

Comments (5)

grosser avatar grosser commented on July 24, 2024

A workaround would be to load everything before enabling fakefs (Dir.glob + require maybe)
If the behavior changed between ruby versions then I think that means that the old ruby used some internal api and the new version uses File.read or some other fakefs method.
It could help narrowing this down by making a simple example file (using bundler/inline) to reproduce this in 2 ruby versions ... I'd expect one of them to call some internal FakeFS method and the other to not do that.

from fakefs.

tovodeverett avatar tovodeverett commented on July 24, 2024

I'm surprised you haven't run into this issue before. I've been having to clone since the dawn of time (Rails 4.x?).

I have no memory of how this code works - I wrote this 8+ years ago, but here it is in case it helps. This is from spec_helper.rb:

# Support FakeFS helpers
require 'fakefs/spec_helpers'
require 'pp' # Resolves https://github.com/defunkt/fakefs/issues/215, https://github.com/defunkt/fakefs/issues/99

# Support for FakeFS::SpecHelpers::Rails
module FakeFS
  module SpecHelpers
    module Rails
      def self.extended(example_group)
        example_group.use_fakefs_rails(example_group)
      end

      def self.included(example_group)
        example_group.extend FakeFS::SpecHelpers
        example_group.extend self
      end

      def use_fakefs_rails(describe_block)
        describe_block.before :each do
          unless FakeFS::FileSystem.unarchive(:rails)
            FakeFS::FileSystem.clone(::Rails.root.join('app'))
            FakeFS::FileSystem.clone(::Rails.root.join('config'))
            FakeFS::FileSystem.clone(::Rails.root.join('public'))
            FakeFS::FileSystem.clone(Gem.loaded_specs['jquery-rails'].full_gem_path)
            FakeFS::FileSystem.clone(Gem.loaded_specs['coffee-script-source'].full_gem_path)
            FileUtils.mkdir_p('/tmp')
            FakeFS::FileSystem.archive(:rails)
          end
        end
      end
    end
  end
end

Then I just use include FakeFS::SpecHelpers::Rails in my rspec contexts.

The cool thing there is by using archive, I amortize the cost of cloning huge chunks of my Rails app across all the tests that need FakeFS.

from fakefs.

grosser avatar grosser commented on July 24, 2024

nice trick!

from fakefs.

alishersadikov avatar alishersadikov commented on July 24, 2024

A workaround would be to load everything before enabling fakefs (Dir.glob + require maybe) If the behavior changed between ruby versions then I think that means that the old ruby used some internal api and the new version uses File.read or some other fakefs method. It could help narrowing this down by making a simple example file (using bundler/inline) to reproduce this in 2 ruby versions ... I'd expect one of them to call some internal FakeFS method and the other to not do that.

Thanks for a quick reply and sorry it took me a while to get back. Thanks for the suggestions. Using bundler/inline, there is probably a way to see what methods are called internally, but I have been scratching my head about that.

from fakefs.

alishersadikov avatar alishersadikov commented on July 24, 2024

I'm surprised you haven't run into this issue before. I've been having to clone since the dawn of time (Rails 4.x?).

I have no memory of how this code works - I wrote this 8+ years ago, but here it is in case it helps. This is from spec_helper.rb:

# Support FakeFS helpers
require 'fakefs/spec_helpers'
require 'pp' # Resolves https://github.com/defunkt/fakefs/issues/215, https://github.com/defunkt/fakefs/issues/99

# Support for FakeFS::SpecHelpers::Rails
module FakeFS
  module SpecHelpers
    module Rails
      def self.extended(example_group)
        example_group.use_fakefs_rails(example_group)
      end

      def self.included(example_group)
        example_group.extend FakeFS::SpecHelpers
        example_group.extend self
      end

      def use_fakefs_rails(describe_block)
        describe_block.before :each do
          unless FakeFS::FileSystem.unarchive(:rails)
            FakeFS::FileSystem.clone(::Rails.root.join('app'))
            FakeFS::FileSystem.clone(::Rails.root.join('config'))
            FakeFS::FileSystem.clone(::Rails.root.join('public'))
            FakeFS::FileSystem.clone(Gem.loaded_specs['jquery-rails'].full_gem_path)
            FakeFS::FileSystem.clone(Gem.loaded_specs['coffee-script-source'].full_gem_path)
            FileUtils.mkdir_p('/tmp')
            FakeFS::FileSystem.archive(:rails)
          end
        end
      end
    end
  end
end

Then I just use include FakeFS::SpecHelpers::Rails in my rspec contexts.

The cool thing there is by using archive, I amortize the cost of cloning huge chunks of my Rails app across all the tests that need FakeFS.

This looks nice! We have a rudimentary, now inefficient way of doing this, by using a helper method:

def within_stubbed_fs
    # some cruft here
    FakeFS.with_fresh do
       # now I have to clone the file system here 
       yield 
    end 
end 

I search for archive/unarchive methods and they don't seem to be in the source code. But I found the issue you had created @tovodeverett: #170, so maybe I can try to mimic that.

from fakefs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.