Module: FakeFS

Included in:
SpecHelpers, SpecHelpers::All
Defined in:
lib/fakefs/base.rb,
lib/fakefs/io.rb,
lib/fakefs/dir.rb,
lib/fakefs/file.rb,
lib/fakefs/kernel.rb,
lib/fakefs/globber.rb,
lib/fakefs/version.rb,
lib/fakefs/fake/dir.rb,
lib/fakefs/pathname.rb,
lib/fakefs/fake/file.rb,
lib/fakefs/file_test.rb,
lib/fakefs/fileutils.rb,
lib/fakefs/fake/inode.rb,
lib/fakefs/file_system.rb,
lib/fakefs/fake/symlink.rb,
lib/fakefs/spec_helpers.rb,
lib/fakefs/flockable_file.rb

Overview

FakeFS module

Defined Under Namespace

Modules: FileSystem, FileTest, FileUtils, Globber, Kernel, SpecHelpers, Version Classes: Dir, FakeDir, FakeFile, FakeInode, FakeSymlink, File, IO, Pathname

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate!(io_mocks: false) ⇒ Object

unconditionally activate



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fakefs/base.rb', line 24

def activate!(io_mocks: false)
  Object.class_eval do
    remove_const(:Dir)
    remove_const(:File)
    remove_const(:FileTest)
    remove_const(:FileUtils)
    remove_const(:Pathname)

    const_set(:Dir,       FakeFS::Dir)
    const_set(:File,      FakeFS::File)
    const_set(:FileUtils, FakeFS::FileUtils)
    const_set(:FileTest,  FakeFS::FileTest)
    const_set(:Pathname,  FakeFS::Pathname)

    if io_mocks
      remove_const(:IO)
      const_set(:IO, ::FakeFS::IO)
    end

    ::FakeFS::Kernel.hijack!
  end

  @activated = true

  true
end

.activated?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fakefs/base.rb', line 19

def activated?
  @activated ||= false
end

.clear!Object

unconditionally clear the fake filesystem



76
77
78
# File 'lib/fakefs/base.rb', line 76

def clear!
  ::FakeFS::FileSystem.clear
end

.deactivate!Object

unconditionally deactivate



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fakefs/base.rb', line 52

def deactivate!
  Object.class_eval do
    remove_const(:Dir)
    remove_const(:File)
    remove_const(:FileTest)
    remove_const(:FileUtils)
    remove_const(:IO)
    remove_const(:Pathname)

    const_set(:Dir,       RealDir)
    const_set(:File,      RealFile)
    const_set(:FileTest,  RealFileTest)
    const_set(:FileUtils, RealFileUtils)
    const_set(:IO,        RealIO)
    const_set(:Pathname,  RealPathname)
    ::FakeFS::Kernel.unhijack!
  end

  @activated = false

  true
end

.withObject

present the fake filesystem to the block



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fakefs/base.rb', line 87

def with
  if activated?
    yield
  else
    begin
      activate!
      yield
    ensure
      deactivate!
    end
  end
end

.with_fresh(&block) ⇒ Object

present a fresh new fake filesystem to the block



81
82
83
84
# File 'lib/fakefs/base.rb', line 81

def with_fresh(&block)
  clear!
  with(&block)
end

.withoutObject

present a non-fake filesystem to the block



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fakefs/base.rb', line 101

def without
  if !activated?
    yield
  else
    begin
      deactivate!
      yield
    ensure
      activate!
    end
  end
end

Instance Method Details

#use_fakefs(describe_block, opts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fakefs/spec_helpers.rb', line 36

def use_fakefs(describe_block, opts)
  describe_block.before opts[:with] do
    FakeFS.activate!
  end

  describe_block.after opts[:with] do
    FakeFS.deactivate!
    FakeFS::FileSystem.clear if opts[:with] == :each
  end
end