diff options
Diffstat (limited to 'spec/bundler/support/command_execution.rb')
| -rw-r--r-- | spec/bundler/support/command_execution.rb | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/spec/bundler/support/command_execution.rb b/spec/bundler/support/command_execution.rb index 556285ac52..e2915b996d 100644 --- a/spec/bundler/support/command_execution.rb +++ b/spec/bundler/support/command_execution.rb @@ -1,25 +1,38 @@ # frozen_string_literal: true -require "support/helpers" -require "support/path" - module Spec - CommandExecution = Struct.new(:command, :working_directory, :exitstatus, :stdout, :stderr) do - include RSpec::Matchers::Composable + class CommandExecution + def initialize(command, timeout:) + @command = command + @timeout = timeout + @original_stdout = String.new + @original_stderr = String.new + end - def to_s - c = Shellwords.shellsplit(command.strip).map {|s| s.include?("\n") ? " \\\n <<EOS\n#{s.gsub(/^/, " ").chomp}\nEOS" : Shellwords.shellescape(s) } - c = c.reduce("") do |acc, elem| - concat = acc + " " + elem - - last_line = concat.match(/.*\z/)[0] - if last_line.size >= 100 - acc + " \\\n " + elem - else - concat - end + attr_accessor :exitstatus, :command, :original_stdout, :original_stderr + attr_reader :timeout + attr_writer :failure_reason + + def raise_error! + return unless failure? + + error_header = if failure_reason == :timeout + "Invoking `#{command}` was aborted after #{timeout} seconds with output:" + else + "Invoking `#{command}` failed with output:" end - "$ #{c.strip}" + + raise <<~ERROR + #{error_header} + + ---------------------------------------------------------------------- + #{stdboth} + ---------------------------------------------------------------------- + ERROR + end + + def to_s + "$ #{command}" end alias_method :inspect, :to_s @@ -27,12 +40,12 @@ module Spec @stdboth ||= [stderr, stdout].join("\n").strip end - def bundler_err - if Bundler::VERSION.start_with?("1.") - stdout - else - stderr - end + def stdout + normalize(original_stdout) + end + + def stderr + normalize(original_stderr) end def to_s_verbose @@ -53,5 +66,13 @@ module Spec return true unless exitstatus exitstatus > 0 end + + private + + attr_reader :failure_reason + + def normalize(string) + string.dup.force_encoding(Encoding::UTF_8).scrub.strip.gsub("\r\n", "\n") + end end end |
