class Rspec::Shell::Expectations::StubbedEnv

A shell environment that can manipulate behaviour of executables

Attributes

dir[R]

Public Class Methods

new() click to toggle source
# File lib/rspec/shell/expectations/stubbed_env.rb, line 18
def initialize
  @dir = Dir.mktmpdir
  ENV['PATH'] = "#{@dir}:#{ENV['PATH']}"
  at_exit { cleanup }
end

Public Instance Methods

cleanup() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/rspec/shell/expectations/stubbed_env.rb, line 30
def stub_command(command)
  StubbedCommand.new command, @dir
end

Private Instance Methods

env() click to toggle source
# File lib/rspec/shell/expectations/stubbed_env.rb, line 40
def env
  "PATH=#{@dir}:$PATH"
end