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, IO Classes: Dir, File, FileSystem

Constant Summary collapse

OriginalDir =

Keeps track of the original Ruby Dir class.

::Dir
OriginalFile =

Keeps track of the original Ruby File class.

::File
VERSION =
'0.5.0'

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.



51
52
53
54
55
56
# File 'lib/memfs.rb', line 51

def activate
  activate!
  yield
ensure
  deactivate!
end

.activate!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!


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/memfs.rb', line 73

def activate!
  Object.class_eval do
    remove_const :Dir
    remove_const :File

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

  MemFs::FileSystem.instance.clear!
end

.deactivate!Object

Note:

This method should always be called when using activate!

Deactivates the fake file system.

Returns:

  • nothing.

See Also:

  • #activate!


92
93
94
95
96
97
98
99
100
# File 'lib/memfs.rb', line 92

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

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

.touch(*paths) ⇒ Object

Creates a file and all its parent directories.

Parameters:

  • path:

    The path of the file to create.

Returns:

  • nothing.



108
109
110
111
112
113
114
115
116
117
# File 'lib/memfs.rb', line 108

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