lib/CLIntegracon/subject.rb in clintegracon-0.8.1 vs lib/CLIntegracon/subject.rb in clintegracon-0.9.0
- old
+ new
@@ -1,8 +1,16 @@
module CLIntegracon
class Subject
+ ReplacementPattern = Struct.new(:pattern, :replacement) do
+ # Applies the replacement pattern to the given output, returning
+ # a new string with the replacement applied
+ def replace(output)
+ output.gsub(pattern, replacement)
+ end
+ end
+
#-----------------------------------------------------------------------------#
# @!group Attributes
# @return [String]
@@ -22,13 +30,12 @@
# 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+.
attr_accessor :default_args
- # @return [Hash<String|Regexp,String>]
- # The replace patterns, whose keys are expected to occur in the output,
- # which should be redacted, when the subject will be executed. These are
+ # @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.
attr_accessor :replace_patterns
# @return [String]
@@ -51,11 +58,11 @@
def initialize(name='subject', executable=nil)
self.name = name
self.executable = executable || name
self.environment_vars = {}
self.default_args = []
- self.replace_patterns = {}
+ self.replace_patterns = []
self.output_path = 'execution_output.txt'
end
#-----------------------------------------------------------------------------#
@@ -70,11 +77,11 @@
#
# @param [String] replacement
# The replacement
#
def replace_pattern(pattern, replacement)
- self.replace_patterns[replacement] = pattern
+ self.replace_patterns << ReplacementPattern.new(pattern, replacement)
end
# Define a path, whose occurrences in the output should be replaced by
# either its basename or a given placeholder.
#
@@ -120,13 +127,14 @@
# @return [String]
# The output, which is emitted while execution.
#
def launch(head_arguments='', tail_arguments='')
command = command_line(head_arguments, tail_arguments)
- output = apply_replacements(run(command))
+ output, status = run(command)
+ output = apply_replacements(output)
write_output(command, output)
- output
+ [output, status]
end
#-----------------------------------------------------------------------------#
# @!group Helpers
@@ -145,15 +153,17 @@
# Run the command.
#
# @param [String] command_line
# THe command line to execute
#
- # @return [String]
- # The output, which is emitted while execution.
+ # @return [[String, Process::Status]]
+ # The output, which is emitted during execution, and the exit status.
#
def run(command_line)
- `#{environment_var_assignments} #{command_line} 2>&1`
+ require 'open3'
+ env = Hash[environment_vars.map { |k, v| [k.to_s, v.to_s] }]
+ Open3.capture2e(env, command_line.to_s)
end
# Merges the given with the configured arguments and returns the command
# line to execute.
#
@@ -178,13 +188,12 @@
#
# @return [String]
# The redacted output.
#
def apply_replacements(output)
- replace_patterns.each do |key, path|
- output = output.gsub(path, key)
+ replace_patterns.reduce(output) do |output, replacement_pattern|
+ replacement_pattern.replace(output)
end
- output
end
# Saves the output in a file called #output_path, relative to current dir.
#
# @param [String] command