Class: Rspec::Shell::Expectations::StubbedEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/shell/expectations/stubbed_env.rb

Overview

A shell environment that can manipulate behaviour of executables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStubbedEnv

Returns a new instance of StubbedEnv.



18
19
20
21
22
# File 'lib/rspec/shell/expectations/stubbed_env.rb', line 18

def initialize
  @dir = Dir.mktmpdir
  ENV['PATH'] = "#{@dir}:#{ENV['PATH']}"
  at_exit { cleanup }
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



16
17
18
# File 'lib/rspec/shell/expectations/stubbed_env.rb', line 16

def dir
  @dir
end

Instance Method Details

#cleanupObject



24
25
26
27
28
# File 'lib/rspec/shell/expectations/stubbed_env.rb', line 24

def cleanup
  paths = (ENV['PATH'].split ':') - [@dir]
  ENV['PATH'] = paths.join ':'
  FileUtils.remove_entry_secure @dir if Pathname.new(@dir).exist?
end

#execute(command, env_vars = {}) ⇒ Object



34
35
36
# File 'lib/rspec/shell/expectations/stubbed_env.rb', line 34

def execute(command, env_vars = {})
  Open3.capture3(env_vars, "#{env} #{command}")
end

#stub_command(command) ⇒ Object



30
31
32
# File 'lib/rspec/shell/expectations/stubbed_env.rb', line 30

def stub_command(command)
  StubbedCommand.new command, @dir
end