Method: Wukong::SpecHelpers::IntegrationTests#command

Defined in:
lib/wukong/spec_helpers/integration_tests.rb

#command(*args, options = {}) ⇒ Object

Spawn a command and capture its STDOUT, STDERR, and exit code.

The args will be joined together into a command line.

It is expected that you will use the matchers defined in IntegrationMatchers in your integration tests:

Examples:

Check output of 'ls' includes a string 'foo.txt'

it "lists files" do
  command('ls').should have_output('foo.txt')
end

More complicated

context "long format" do
  it "lists files with timestamps" do
    command('ls', '-l').should have_output('foo.txt', /\w+ \d+ \d+:\d+/)
  end
end

If the last element of args is a Hash it will be used for options.

The :env option specifies the command line environment to use for the command. By default this will be the value of the Ruby process's own ENV variable. If running in a context in which the integration_env method is defined, its return value will be merged on top of ENV. An explicitly provided :env option will again be merged on top.

The :cwd option specifies the working directory to start in. It defaults to the value of Dir.pwd

Parameters:

  • (defaults to: {})

Options Hash (options):

  • env (Hash)

    the shell environment to spawn the command with

  • cwd (Hash)

    the directory to execute the command in

Parameters:



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 135

def command *args
  a = args.flatten.compact
  options = (a.last.is_a?(Hash) ? a.pop : {})

  env = ENV.to_hash.dup
  env.merge!(integration_env) if respond_to?(:integration_env)
  env.merge!(options[:env] || {})

  cwd   = options[:cwd]
  cwd ||= (respond_to?(:integration_cwd) ? integration_cwd : Dir.pwd)

  IntegrationTestRunner.new(a, cwd: cwd, env: env)
end