Fake file system for Ruby

I need some kind of code that pushes the actual file system to fake. Therefore, when I run it, it converts /home/user/Documents/fake_fsto /, so every call Diror Filegoes to this directory. Example:

I want to make the file on /some_file, so I use:

File.open('/some_file', 'w') do |f|
  f.puts 'something on this file'
end

And he will write on /home/user/Documents/fake_fs/some_fileinstead /some_file. Is there any way to do this? Thank you

+3
source share
1 answer

You have two options:

Option 1 - Use the gem to fake

FakeFS , , , . FakeFS , , Ruby standard lib, - - .

2 - ,

/ , . , .

:

$root = ENV['ROOT_DIR'] || '/'
File.open(File.join($root,'some_file'),'w') do |file|
  # whatever
end

ROOT_DIR , , .

chroot , .

Dir.chroot(ENV['ROOT_DIR'] || '/')

File.open('/some_file','w') do |file|
  # whatever
end

. man chroot.

2.

+2

All Articles