Module: MemFs

Defined in:
lib/memfs.rb,
lib/memfs/io.rb,
lib/memfs/dir.rb,
lib/memfs/file.rb,
lib/memfs/version.rb,
lib/memfs/fake/file.rb,
lib/memfs/file/stat.rb,
lib/memfs/fake/entry.rb,
lib/memfs/file_system.rb,
lib/memfs/fake/symlink.rb,
lib/memfs/fake/directory.rb,
lib/memfs/fake/file/content.rb,
lib/memfs/filesystem_access.rb

Overview

Provides a clean way to interact with a fake file system.

Examples:

Calling activate with a block.

MemFs.activate do
  Dir.mkdir '/hello_world'
  # /hello_world exists here, in memory
end
# /hello_world doesn't exist and has never been on the real FS

Calling activate! and deactivate!.

MemFs.activate!
  # The fake file system is running here
MemFs.deactivate!
# Everything back to normal

Defined Under Namespace

Modules: Fake, FilesystemAccess Classes: Dir, File, FileSystem, IO

Constant Summary collapse

OriginalDir =

Keeps track of the original Ruby Dir class.

::Dir
OriginalFile =

Keeps track of the original Ruby File class.

::File
OriginalIO =

Keeps track of the original Ruby IO class.

::IO
VERSION =
'1.0.0'.freeze

Class Method Summary collapse

Class Method Details

.activate { ... } ⇒ Object

Calls the given block with MemFs activated.

The advantage of using #activate against #activate! is that, in case an exception occurs, MemFs is deactivated.

Examples:

MemFs.activate do
  Dir.mkdir '/hello_world'
  # /hello_world exists here, in memory
end
# /hello_world doesn't exist and has never been on the real FS

Exception in activate block.

MemFs.activate do
  raise "Some Error"
end
# Still back to the original Ruby classes

Yields:

  • with no argument.

Returns:

  • nothing.



54
55
56
57
58
59
# File 'lib/memfs.rb', line 54

def activate
  activate!
  yield
ensure
  deactivate!
end

.activate!(clear: true) ⇒ Object

Note:

Don’t forget to call #deactivate! to disable the fake file system, you may have some issues in your scripts or tests otherwise.

Activates the fake file system.

Examples:

MemFs.activate!
Dir.mkdir '/hello_world'
# /hello_world exists here, in memory
MemFs.deactivate!
# /hello_world doesn't exist and has never been on the real FS

Returns:

  • nothing.

See Also:

  • #deactivate!


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/memfs.rb', line 76

def activate!(clear: true)
  Object.class_eval do
    remove_const :Dir
    remove_const :File
    remove_const :IO

    const_set :Dir, MemFs::Dir
    const_set :IO, MemFs::IO
    const_set :File, MemFs::File
  end

  MemFs::FileSystem.instance.clear! if clear
end

.deactivate!Object

Note:

This method should always be called when using activate!

Deactivates the fake file system.

Returns:

  • nothing.

See Also:

  • #activate!


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/memfs.rb', line 97

def deactivate!
  Object.class_eval do
    remove_const :Dir
    remove_const :File
    remove_const :IO

    const_set :Dir, MemFs::OriginalDir
    const_set :IO, MemFs::OriginalIO
    const_set :File, MemFs::OriginalFile
  end
end

.haltObject

Switches back to the original file system, calls the given block (if any), and switches back afterwards.

If a block is given, all file & dir operations (like reading dir contents or requiring files) will operate on the original fs.

Examples:

MemFs.halt do
  puts Dir.getwd
end

Returns:

  • nothing



121
122
123
124
125
126
127
# File 'lib/memfs.rb', line 121

def halt
  deactivate!

  yield if block_given?
ensure
  activate!(clear: false)
end

.touch(*paths) ⇒ Object

Creates a file and all its parent directories.

Parameters:

  • path:

    The path of the file to create.

Returns:

  • nothing.



135
136
137
138
139
140
141
142
143
144
# File 'lib/memfs.rb', line 135

def touch(*paths)
  if ::File != MemFs::File
    fail 'Always call MemFs.touch inside a MemFs active context.'
  end

  paths.each do |path|
    FileUtils.mkdir_p File.dirname(path)
    FileUtils.touch path
  end
end