Class: Rspec::Shell::Expectations::StubbedEnv
- Inherits:
-
Object
- Object
- Rspec::Shell::Expectations::StubbedEnv
- Defined in:
- lib/rspec/shell/expectations/stubbed_env.rb
Overview
A shell environment that can manipulate behaviour of executables
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #execute(command, env_vars = {}) ⇒ Object
-
#initialize ⇒ StubbedEnv
constructor
A new instance of StubbedEnv.
- #stub_command(command) ⇒ Object
Constructor Details
#initialize ⇒ StubbedEnv
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
#dir ⇒ Object (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
#cleanup ⇒ Object
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 |