simonc / memfs

MemFs provides a fake file system that can be used for tests. Strongly inspired by FakeFS.
MIT License
320 stars 27 forks source link

Switch between fake & real fs. #24

Closed thsur closed 7 years ago

thsur commented 7 years ago

Somewhat related to #23 & #7 - I needed to read from the original file system, and this is what I came up with to switch between both file systems.

Background story: I'm testing a command line app lazy-loading subcommands via some loader class that requires the appropriate files. With the switch implemented, mocking the loader was easy as saying:

class Loader

  alias_method :_load, :load

  def load(file)
    MemFs.halt { _load(file) }
  end

end

So this is might be a valid use case, or it might be not, or it might be a stupid implementation, but I thought to give it a try.

simonc commented 7 years ago

Hi @thsur and thanks a lot for this cool idea!

I think the halt mechanism could be implemented calling deactivate and activate instead of copying the code, that would prevent any error if the activation code changes in the future ☺️

It would also require tests before being merged :wink:

thsur commented 7 years ago

Hi @simonc,

I think the halt mechanism could be implemented calling deactivate and activate instead of copying the code

yep, you're right - I didn't focus enough on my problem, which was not so much switching between file systems, but to keep the state of the faked one.

So I've rewritten the code and also wrote a spec for .halt - since I've no experience with RSpec, you might want to review the latter...

simonc commented 7 years ago

Hi @thsur. I think we have several options here:

  1. add a clear: true argument to activate! that way we can disable MemFs and keep the state
  2. Add a real API to FileSystem to dump/restore the current state

I would vote for the :clear option, it's the simplest one.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.001%) to 99.831% when pulling 9cf83ef4221a069cfe1e1b0a06cf81e8e8fa5c0c on pontycode:switch-fs into 90607ace3a8dcc6a8709c058a0eb1efef1b9984e on simonc:master.

thsur commented 7 years ago

Hi @simonc,

sorry, took me a while to come back to the issue. I wanted to introduce some sort of parameter for activate! myself, but didn't want to clutter your method signatures. But you're right, it's the easiest one, so it's in.

simonc commented 7 years ago

Hey @thsur. That's pretty cool. Two small things though:

  1. the comment regarding the before block
  2. I think we should have a spec regarding the ensure that tests that halt re-actives MemFs in case an exception happens

After this, it's a GO! :shipit:

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.001%) to 99.831% when pulling 4ebbed43d2290b9de25ca7bcb6e2ae9f7f9adb64 on pontycode:switch-fs into 90607ace3a8dcc6a8709c058a0eb1efef1b9984e on simonc:master.

thsur commented 7 years ago

Hey @simonc, thanks for your comments!

What is the purpose of this before block?

Hm. Good question. Maybe it's purpose was to illustrate my lack of talent when coding by copy & paste...

I think we should have a spec regarding the ensure that tests that halt re-actives MemFs in case an exception happens

Absolutely. It's in now.

simonc commented 7 years ago

Merged! 🎉 Thanks a lot! ☺️

thsur commented 7 years ago

Thank you! :beers: