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/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, Kernel, SpecHelpers, Version Classes: Dir, FakeDir, FakeFile, FakeSymlink, File, Pathname

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate!Object


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

def activate!
  @activated = true
  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
  true
end

.activated?Boolean

Returns:

  • (Boolean)

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

def activated?
  @activated
end

.deactivate!Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fakefs/base.rb', line 44

def deactivate!
  @activated = false

  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
  true
end

.withObject


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fakefs/base.rb', line 64

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

.withoutObject


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

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