fake your files - memfs
DESCRIPTION
My talk for Paris.rb on 2014-02-04.TRANSCRIPT
![Page 1: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/1.jpg)
Fake your filesSimon Courtois - @happynoff
![Page 2: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/2.jpg)
How to test files manipulation ?
![Page 3: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/3.jpg)
it "creates the given file" do
endfile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
![Page 4: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/4.jpg)
it "creates the given file" do
endexpect(File.exists?(‘thing.txt’)).to be_truefile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
![Page 5: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/5.jpg)
class FileMaker def create(path) FileUtils.touch(path) end end
it "creates the given file" do
end
file_maker.create('thing.txt')FileUtils.rm('thing.txt')
expect(File.exists?(‘thing.txt’)).to be_true
![Page 6: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/6.jpg)
Read-only file system ?
![Page 7: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/7.jpg)
it "creates the given file" do
endfile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
![Page 8: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/8.jpg)
class FileMaker def create(path) FileUtils.touch(path) end end
it "creates the given file" do
end
expect(FileUtils).to receive(:touch) .with(‘thing.txt') .and_return(true)file_maker.create('thing.txt')
![Page 9: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/9.jpg)
Boooh! Test behavior not implementation
![Page 10: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/10.jpg)
Enters a solution FakeFS
![Page 11: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/11.jpg)
it "creates the given file" dofile_maker.create('thing.txt')
end expect(File.exists?('thing.txt')).to be_true
class FileMaker def create(path) FileUtils.touch(path end end
)
![Page 12: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/12.jpg)
class FileMaker def create(path) FileUtils.touch(path end end
it "creates the given file" doFakeFS do
file_maker.create('thing.txt')expect(File.exists?('thing.txt')).to
endend
be_true
)
![Page 13: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/13.jpg)
class FileMaker def create(path) FileUtils.touch(path end end
it "creates the given file" doFakeFS do
file_maker.create('thing.txt')expect(File.exists?('thing.txt')).to
endend
be_false
, noop: true)
be_true
![Page 14: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/14.jpg)
BOOM! That’s a red dot
![Page 15: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/15.jpg)
FakeFS overwrites FileUtils and
ignores options
![Page 16: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/16.jpg)
Try MemFS
![Page 17: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/17.jpg)
class FileMaker def create(path) FileUtils.touch(path, noop: true) end end
it "creates the given file" dofile_maker.create('thing.txt')expect(File.exists?('thing.txt')).to be_false
end
![Page 18: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/18.jpg)
class FileMaker def create(path) FileUtils.touch(path, noop: true) end end
it "creates the given file" do
file_maker.create('thing.txt')expect(File.exists?('thing.txt')).to be_false
endend
MemFs.activate do
![Page 19: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/19.jpg)
MemFs doesn’t overwrite FileUtils
only low-level classes
![Page 20: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/20.jpg)
File.open('thing.txt') do |f| f.puts 'hello' end !File.read('thing.txt') # => "hello\n"
File.symlink('thing.txt', 'thing-link.txt') File.symlink?('thing-link.txt') # => true
File.chmod(0777, 'thing.txt')
File.stat('thing.txt').ctime # => 2014-02-04 19:00:00 +0100
![Page 21: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/21.jpg)
Some resources
![Page 22: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/22.jpg)
http://github.com/defunkt/fakefs
http://github.com/simonc/memfs
![Page 23: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/23.jpg)
Questions?
![Page 24: Fake your files - MemFs](https://reader034.vdocument.in/reader034/viewer/2022052410/549a4ddbac795959288b5c34/html5/thumbnails/24.jpg)
Thank youSimon Courtois - @happynoff