Fake your files - MemFs

24
Fake your files Simon Courtois - @happynoff

description

My talk for Paris.rb on 2014-02-04.

Transcript of Fake your files - MemFs

Page 1: Fake your files - MemFs

Fake your filesSimon Courtois - @happynoff

Page 2: Fake your files - MemFs

How to test files manipulation ?

Page 3: Fake your files - MemFs

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

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

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

Read-only file system ?

Page 7: Fake your files - MemFs

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

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

Boooh! Test behavior not implementation

Page 10: Fake your files - MemFs

Enters a solution FakeFS

Page 11: Fake your files - MemFs

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

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

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

BOOM! That’s a red dot

Page 15: Fake your files - MemFs

FakeFS overwrites FileUtils and

ignores options

Page 16: Fake your files - MemFs

Try MemFS

Page 17: Fake your files - MemFs

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

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

MemFs doesn’t overwrite FileUtils

only low-level classes

Page 20: Fake your files - MemFs

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

Some resources

Page 22: Fake your files - MemFs

http://github.com/defunkt/fakefs

http://github.com/simonc/memfs

Page 23: Fake your files - MemFs

Questions?

Page 24: Fake your files - MemFs

Thank youSimon Courtois - @happynoff