Module: CommandExec::SpecHelper

Defined in:
lib/command_exec/spec_helper_module.rb

Overview

Helpers for specs

Instance Method Summary collapse

Instance Method Details

#capture_stderr(&block) ⇒ Object

Capture stderr

Parameters:

  • block (Block)


10
11
12
13
14
15
16
# File 'lib/command_exec/spec_helper_module.rb', line 10

def capture_stderr(&block)
  previous_stderr, $stderr = $stderr, StringIO.new
  block.call
  return $stderr.string
ensure
  $stderr = previous_stderr
end

#capture_stdout(&block) ⇒ Object

Capture stdout

Parameters:

  • block (Block)


21
22
23
24
25
26
27
# File 'lib/command_exec/spec_helper_module.rb', line 21

def capture_stdout(&block)
  previous_stdout, $stdout = $stdout, StringIO.new
  block.call
  return $stdout.string
ensure
  $stdout = previous_stdout
end

#create_temp_file_with(base_name, content) ⇒ String

Create temporary files for testing (which will be deleted when the ruby process terminates)

Parameters:

  • base_name (String)

    the path to the temporary file

  • content (String)

    the content which should be written to the file

Returns:

  • (String)

    the path to the temporary file



64
65
66
67
68
69
# File 'lib/command_exec/spec_helper_module.rb', line 64

def create_temp_file_with(base_name, content)
  file = Tempfile.new(base_name)
  file.write(content)
  file.close
  file.path
end

#environment(env = {}, options = {}) { ... } ⇒ Object

Manipulate environment for the given block

Parameters:

  • env (Hash) (defaults to: {})

    The environment for the block which should be merged with ENV

  • options (Hash) (defaults to: {})

    Options for environment manipulation

Options Hash (options):

  • :clear (True, False)

    Should the environment clear before merge?

Yields:

  • Block which should be executed



42
43
44
45
46
47
48
49
50
# File 'lib/command_exec/spec_helper_module.rb', line 42

def environment(env={},options={},&block)
  previous_environment, environment = ENV.to_hash, env
  ENV.clear if options[:clear] == true
  ENV.update(environment)
  block.call
ensure
  ENV.clear
  ENV.update previous_environment
end