Class: EcomDev::ChefSpec::Stub::FileSystem

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/ecomdev/chefspec/stub/file_system.rb

Instance Method Summary collapse

Constructor Details

#initializeFileSystem

Returns a new instance of FileSystem.



10
11
12
13
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 10

def initialize
  @current_example = nil
  @stubs = {}
end

Instance Method Details

#after_exampleObject



23
24
25
26
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 23

def after_example
  @current_example = nil
  @stubs.clear
end

#before_example(example) ⇒ Object



15
16
17
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 15

def before_example(example)
  @current_example = example
end

#current_exampleObject



19
20
21
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 19

def current_example
  @current_example
end

#dir_glob(path, result = []) ⇒ Object



59
60
61
62
63
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 59

def dir_glob(path, result = [])
  stub(Dir, :glob, true) do |stub|
    stub.with(path).and_return(result)
  end
end

#file_exists(file, exists = true) ⇒ Object



47
48
49
50
51
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 47

def file_exists(file, exists = true)
  stub(File, :exists?, true) do |stub|
    stub.with(file).and_return(exists)
  end
end

#file_read(file, content, *additional_args) ⇒ Object



53
54
55
56
57
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 53

def file_read(file, content, *additional_args)
  stub(File, :read, true) do |stub|
    stub.with(file, *additional_args).and_return(content)
  end
end

#stub(klass, method, static = false) ⇒ RSpec::Mocks::Matchers::Receive

Returns:

  • (RSpec::Mocks::Matchers::Receive)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ecomdev/chefspec/stub/file_system.rb', line 29

def stub(klass, method, static=false)
  @stubs[static.to_s] ||= {}
  @stubs[static.to_s][klass.to_s] ||= []
  stub_method = static ? :allow : :allow_any_instance_of
  method = method.to_sym
  unless @stubs[static.to_s][klass.to_s].include?(method)
    @stubs[static.to_s][klass.to_s] << method
    current_example.send(stub_method, klass).to current_example.receive(method).and_call_original
  end

  allowance = current_example.receive(method)
  if block_given?
    yield allowance
  end

  current_example.send(stub_method, klass).to allowance
end