Module: Wukong::SpecHelpers::IntegrationTests

Included in:
Wukong::SpecHelpers
Defined in:
lib/wukong/spec_helpers/integration_tests.rb

Overview

This module defines methods that are helpful to use in integration tests which require reading files from the local repository.

Integration tests will spawn new system processes with their own environments. This module provides methods and hooks for customizing that environment.

Instance Method Summary collapse

Instance Method Details

#bin_dir(*args) ⇒ String

The directory to add to the PATH environment variable for the spawned processes.

If args are given, return a path within this directory.

Parameters:

Returns:



34
35
36
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 34

def bin_dir *args
  root.join('bin', *args).to_s
end

#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:

  • args (Array<String>)
  • options (Hash) (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

#examples_dir(*args) ⇒ String

The directory to use for examples for the spawned process.

If args are given, return a path within this directory.

Parameters:

Returns:



44
45
46
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 44

def examples_dir *args
  root.join('examples', *args).to_s
end

#exit_with(code) ⇒ Object

Checks that the command exits with the given code.

Parameters:

  • code (Integer)


92
93
94
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 92

def exit_with code
  ExitCodeMatcher.new(code)
end

#have_stderr(*expectations) ⇒ Object

Checks that each expectation appears in the STDERR of the command. Order is irrelevant and each expectation can be either a String to check for inclusion or a Regexp to match with.

Parameters:

  • expectations (Array<String,Regexp>)


85
86
87
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 85

def have_stderr *expectations
  StderrMatcher.new(*expectations)
end

#have_stdout(*expectations) ⇒ Object

Checks that each expectation appears in the STDOUT of the command. Order is irrelevant and each expectation can be either a String to check for inclusion or a Regexp to match with.

Parameters:

  • expectations (Array<String,Regexp>)


75
76
77
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 75

def have_stdout *expectations
  StdoutMatcher.new(*expectations)
end

#integration_cwdString

The directory to spawn new processes in.

Returns:



65
66
67
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 65

def integration_cwd
  root.to_s
end

#integration_envHash

A Hash of environment variables to use for the spawned process.

By default, will put the IntegrationHelper#lib_dir on the RUBYLIB and the IntegrationHelper#bin_dir on the PATH.

Returns:

  • (Hash)


55
56
57
58
59
60
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 55

def integration_env
  {
    "PATH"    => [bin_dir.to_s, ENV["PATH"]].compact.join(':'),
    "RUBYLIB" => [lib_dir.to_s, ENV["RUBYLIB"]].compact.join(':')
  }
end

#lib_dir(*args) ⇒ String

The directory to add to the RUBYLIB environment variable for the spawned processes.

If args are given, return a path within this directory.

Parameters:

Returns:



23
24
25
# File 'lib/wukong/spec_helpers/integration_tests.rb', line 23

def lib_dir *args
  root.join('lib', *args).to_s
end