Module: RSpec::OpenHAB::Shell

Defined in:
lib/rspec/openhab/shell.rb

Overview

Class Method Summary collapse

Class Method Details

.shell_env(cmd = nil) ⇒ Object

Read in the bash environment, after an optional command.

Returns Array of key/value pairs.


11
12
13
14
15
# File 'lib/rspec/openhab/shell.rb', line 11

def shell_env(cmd = nil)
  cmd = "#{cmd} > /dev/null; " if cmd
  env = `#{cmd}printenv -0`
  env.split("\0").map { |l| l.split("=", 2) }
end

.shell_source(file) ⇒ Object

Source a given file, and compare environment before and after.

Returns Hash of any keys that have changed.


19
20
21
# File 'lib/rspec/openhab/shell.rb', line 19

def shell_source(file)
  (shell_env(". #{File.realpath(file)}") - shell_env).to_h
end

.source_env_from(file) ⇒ Object

Find variables changed as a result of sourcing the given file,

and update in ENV.


25
26
27
# File 'lib/rspec/openhab/shell.rb', line 25

def source_env_from(file)
  shell_source(file).each { |k, v| ENV[k] = v }
end