Sha256: dca21bae20bcbd9fd17ce079eb9c45874e835804f705658f4827820bf1abcc98

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Beaker
  class Result
    attr_accessor :host, :cmd, :exit_code, :stdout, :stderr, :output,
                  :raw_stdout, :raw_stderr, :raw_output
    def initialize(host, cmd)
      @host       = host
      @cmd        = cmd
      @stdout     = ''
      @stderr     = ''
      @output     = ''
      @exit_code  = nil
    end

    # Ruby assumes chunked data (like something it receives from Net::SSH)
    # to be binary (ASCII-8BIT). We need to gather all chunked data and then
    # re-encode it as the default encoding it assumes for external text
    # (ie our test files and the strings they're trying to match Net::SSH's
    # output from)
    # This is also the lowest overhead place to normalize line endings, IIRC
    def finalize!
      @raw_stdout = @stdout
      @stdout     = normalize_line_endings( convert( @stdout ) )
      @raw_stderr = @stderr
      @stderr     = normalize_line_endings( convert( @stderr ) )
      @raw_output = @output
      @output     = normalize_line_endings( convert( @output ) )
    end

    def normalize_line_endings string
      return string.gsub(/\r\n?/, "\n")
    end

    def convert string
      if string.respond_to?( :encode )
        # We're running in >= 1.9 and we'll need to convert
        # Remove invalid and undefined UTF-8 character encodings
        encoding = Encoding ? Encoding.default_external : "UTF-8"
        return string.encode(encoding, string.encoding, :invalid => :replace, :undef => :replace, :replace => '')
      else
        # We're running in < 1.9 and Ruby doesn't care
        return string
      end
    end

    def log(logger)
      logger.debug "Exited: #{exit_code}" unless exit_code == 0
    end

    def formatted_output(limit=10)
      @output.split("\n").last(limit).collect {|x| "\t" + x}.join("\n")
    end

    def exit_code_in?(range)
      range.include?(@exit_code)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
beaker-1.15.0 lib/beaker/result.rb