crystal-community / crystal-libraries-needed

A list of libraries that are needed or wanted for the Crystal-Language
141 stars 4 forks source link

File System Mocking library #102

Open HCLarsen opened 5 years ago

HCLarsen commented 5 years ago

Something like webmock.cr, but for the local file system. It would be very useful for testing. Ruby has something called FakeFS, so it should be entirely possible.

bew commented 5 years ago

There is another gem called memfs: https://github.com/simonc/memfs

Both are different from how webmock work though, because with webmock you say what you expect, and then you do a web request, and if it doesn't match the expectations it fails. But the fs gems allows you to do some fs manipulations (in memory) and then you have to check the result is what you expected.

Are you looking for an in memory fs (like fakefs/memfs) or a expect-then-do style of spec system for filesystems?


I'd say both would be great, but just an in memory fs is enough for a start!

HCLarsen commented 5 years ago

For my current use case, an expect-then-do style of system would work. This program that I'm writing will use a config file, so being able to "read" that file and getting a known response regardless of what's on my hard drive, that's basically what I need for now.

I'm sure in the long run, someone will need an entire in memory fs, but for now, the other would work for me.

bew commented 5 years ago

https://forum.crystal-lang.org/t/poc-pluggable-crystal-system-backends/735 I've made a successful POC at changing the implementation of the used system functions, in this case I can do an in memory environment on demand.. And still use ENV. The same would be possible for File and Dir to create an in-memory filesystem, or intercept all FS calls (and install expectations /..).. I think it's the base building block for this kind of stuff, raise your voice if you have suggestions ;)

HCLarsen commented 5 years ago

I don't understand your POC as I'm not familiar with OS back end stuff.

I thought that doing something like this would work the same way as webmock and timecop, where you overwrite the methods like File#read and File#write so that your calls to those methods give a controlled response, instead of actually touching the filesystem.