Module: FakeFS

Included in:
SpecHelpers, SpecHelpers::All
Defined in:
lib/fakefs/base.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/file_system.rb,
lib/fakefs/fake/symlink.rb,
lib/fakefs/spec_helpers.rb

Overview

FakeFS module

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate!Object

unconditionally activate


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

def activate!
  Object.class_eval do
    remove_const(:Dir)
    remove_const(:File)
    remove_const(:FileTest)
    remove_const(:FileUtils)
    remove_const(:Pathname) if RUBY_VERSION >= '1.9.3'

    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 RUBY_VERSION >= '1.9.3'
    ::FakeFS::Kernel.hijack!
  end

  @activated = true

  true
end

.activated?Boolean

Returns:

  • (Boolean)

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

def activated?
  @activated ? true : false
end

.clear!Object

unconditionally clear the fake filesystem


69
70
71
# File 'lib/fakefs/base.rb', line 69

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

.deactivate!Object

unconditionally deactivate


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fakefs/base.rb', line 47

def deactivate!
  Object.class_eval do
    remove_const(:Dir)
    remove_const(:File)
    remove_const(:FileTest)
    remove_const(:FileUtils)
    remove_const(:Pathname) if RUBY_VERSION >= '1.9.3'

    const_set(:Dir,       RealDir)
    const_set(:File,      RealFile)
    const_set(:FileTest,  RealFileTest)
    const_set(:FileUtils, RealFileUtils)
    const_set(:Pathname,  RealPathname) if RUBY_VERSION >= '1.9.3'
    ::FakeFS::Kernel.unhijack!
  end

  @activated = false

  true
end

.withObject

present the fake filesystem to the block


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fakefs/base.rb', line 80

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


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

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

.withoutObject

present a non-fake filesystem to the block


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fakefs/base.rb', line 94

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