class CLIntegracon::Subject

Constants

ReplacementPattern

Attributes

default_args[RW]

@return [Array<String>]

The arguments which will always passed to the executable on launch and
should not been passed explicitly every time to the launch method.
Those are added behind the arguments given on +launch+.
environment_vars[RW]

@return [Hash<String,String>]

The environment variables which will always been defined for the executable
on launch should not been passed explicitly every time to the launch method.
executable[RW]

@return [String]

The executable statement to use for the tests
name[RW]

@return [String]

The name of the binary to use for the tests
output_path[RW]

@return [String]

The path where the output of the executable will be written to.
replace_patterns[RW]

@return [Array<ReplacementPattern>]

The replace patterns that are redacted when the subject is executed. These are
e.g. paths were side-effects occur, like manipulation of user configurations
in dot files or caching-specific directories or just dates and times.

Public Class Methods

new(name='subject', executable=nil) click to toggle source

“Designated” initializer

@param [String] name

The name of the binary

@param [String] executable

The executable subject statement (optional)
# File lib/CLIntegracon/subject.rb, line 58
def initialize(name='subject', executable=nil)
  self.name = name
  self.executable = executable || name
  self.environment_vars = {}
  self.default_args = []
  self.replace_patterns = []
  self.output_path = 'execution_output.txt'
end

Public Instance Methods

launch(head_arguments='', tail_arguments='') click to toggle source

Runs the executable with the given arguments.

@note: You can check by `$?.success?` if the execution succeeded.

@param [String] head_arguments

The arguments to pass to the executable before the default arguments.

@param [String] tail_arguments

The arguments to pass to the executable after the default arguments.

@return [String]

The output, which is emitted while execution.
# File lib/CLIntegracon/subject.rb, line 130
def launch(head_arguments='', tail_arguments='')
  command = command_line(head_arguments, tail_arguments)
  output, status = run(command)
  output = apply_replacements(output)
  write_output(command, output)
  [output, status]
end
replace_path(path, name=nil) click to toggle source

Define a path, whose occurrences in the output should be replaced by either its basename or a given placeholder.

@param [String] path

The path

@param [String] name

The name of the path, or the basename of the given path
# File lib/CLIntegracon/subject.rb, line 94
def replace_path(path, name=nil)
  name ||= File.basename path
  self.replace_pattern path, name
end
replace_pattern(pattern, replacement) click to toggle source

Define a pattern, whose occurrences in the output should be replaced by a given placeholder.

@param [Regexp|String] pattern

The pattern

@param [String] replacement

The replacement
# File lib/CLIntegracon/subject.rb, line 81
def replace_pattern(pattern, replacement)
  self.replace_patterns << ReplacementPattern.new(pattern, replacement)
end
replace_user_path(path, name=nil) click to toggle source

Define a path in the user directory, whose occurrences in the output should be replaced by either its basename or a given placeholder.

@param [String] path

The path

@param [String] name

The name of the path, or the given path
# File lib/CLIntegracon/subject.rb, line 108
def replace_user_path(path, name=nil)
  name ||= "$HOME/#{path}"
  self.replace_path %r[/Users/.*/#{path.to_s}], name
end

Protected Instance Methods

apply_replacements(output) click to toggle source

Apply the configured replacements to the output.

@param [String] output

The output, which was emitted while execution.

@return [String]

The redacted output.
# File lib/CLIntegracon/subject.rb, line 192
def apply_replacements(output)
  replace_patterns.reduce(output) do |output, replacement_pattern|
    replacement_pattern.replace(output)
  end
end
command_line(head_arguments='', tail_arguments='') click to toggle source

Merges the given with the configured arguments and returns the command line to execute.

@param [String] head_arguments

The arguments to pass to the executable before the default arguments.

@param [String] tail_arguments

The arguments to pass to the executable after the default arguments.

@return [String]

The command line to execute.
# File lib/CLIntegracon/subject.rb, line 179
def command_line(head_arguments='', tail_arguments='')
  args = [head_arguments, default_args, tail_arguments].flatten.compact.select { |s| s.length > 0 }.join ' '
  "#{executable} #{args}"
end
environment_var_assignments() click to toggle source

Serializes configured environment variables

@return [String]

Serialized assignment of configured environment variables.
# File lib/CLIntegracon/subject.rb, line 149
def environment_var_assignments
  environment_vars.keys.sort.map { |key| "#{key}=#{environment_vars[key]}" }.join ' '
end
run(command_line) click to toggle source

Run the command.

@param [String] command_line

THe command line to execute

@return [[String, Process::Status]]

The output, which is emitted during execution, and the exit status.
# File lib/CLIntegracon/subject.rb, line 161
def run(command_line)
  require 'open3'
  env = Hash[environment_vars.map { |k, v| [k.to_s, v.to_s] }]
  Open3.capture2e(env, command_line.to_s)
end
write_output(command, output) click to toggle source

Saves the output in a file called output_path, relative to current dir.

@param [String] command

The executed command line.

@param [String] output

The output, which was emitted while execution.
# File lib/CLIntegracon/subject.rb, line 206
def write_output(command, output)
  File.open(output_path, 'w') do |file|
    printable_command = apply_replacements("#{environment_var_assignments} #{command} 2>&1")
    file.write printable_command.sub(executable, name)
    file.write "\n"
    file.write output
  end
end