Module: Kitchen::Util

Defined in:
lib/kitchen/util.rb

Overview

Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.

Author:

Class Method Summary collapse

Class Method Details

.duration(total) ⇒ String

Returns a formatted string representing a duration in seconds.

Parameters:

  • total (Integer)

    the total number of seconds

Returns:

  • (String)

    a formatted string of the form (XmYY.00s)



95
96
97
98
99
100
# File 'lib/kitchen/util.rb', line 95

def self.duration(total)
  total = 0 if total.nil?
  minutes = (total / 60).to_i
  seconds = (total - (minutes * 60))
  format("(%dm%.2fs)", minutes, seconds)
end

.from_logger_level(const) ⇒ Symbol

Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.

Parameters:

  • const (Integer)

    Logger::Severity constant value for a logging level (Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL)

Returns:

  • (Symbol)

    symbol representation of the logging level



47
48
49
50
51
52
53
54
55
# File 'lib/kitchen/util.rb', line 47

def self.from_logger_level(const)
  case const
  when Logger::DEBUG then :debug
  when Logger::INFO then :info
  when Logger::WARN then :warn
  when Logger::ERROR then :error
  else :fatal
  end
end

.outdent!(string) ⇒ String

Modifes the given string to strip leading whitespace on each line, the amount which is calculated by using the first line of text.

Examples:


string = <<-STRING
  a
    b
c
STRING
Util.outdent!(string) # => "a\n  b\nc\n"

Parameters:

  • string (String)

    the string that will be modified

Returns:

  • (String)

    the modified string



132
133
134
# File 'lib/kitchen/util.rb', line 132

def self.outdent!(string)
  string.gsub!(/^ {#{string.index(/[^ ]/)}}/, "")
end

.shell_helpersString

Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions. This function is usually called inline in a string that will be executed remotely on a test instance.

Returns:

  • (String)

    a string representation of useful helper functions



141
142
143
144
145
# File 'lib/kitchen/util.rb', line 141

def self.shell_helpers
  IO.read(File.join(
    File.dirname(__FILE__), %w[.. .. support download_helpers.sh]
  ))
end

.stringified_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to strings. All keys within a Hash are coerced by calling #to_s and hashes with arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as strings



81
82
83
84
85
86
87
88
89
# File 'lib/kitchen/util.rb', line 81

def self.stringified_hash(obj)
  if obj.is_a?(Hash)
    obj.inject({}) { |h, (k, v)| h[k.to_s] = stringified_hash(v); h }
  elsif obj.is_a?(Array)
    obj.inject([]) { |a, e| a << stringified_hash(e); a }
  else
    obj
  end
end

.symbolized_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to symbols. All keys within a Hash are coerced by calling #to_sym and hashes within arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as symbols



64
65
66
67
68
69
70
71
72
# File 'lib/kitchen/util.rb', line 64

def self.symbolized_hash(obj)
  if obj.is_a?(Hash)
    obj.inject({}) { |h, (k, v)| h[k.to_sym] = symbolized_hash(v); h }
  elsif obj.is_a?(Array)
    obj.inject([]) { |a, e| a << symbolized_hash(e); a }
  else
    obj
  end
end

.to_logger_level(symbol) ⇒ Integer

Returns the standard library Logger level constants for a given symbol representation.

Parameters:

  • symbol (Symbol)

    symbol representation of a logger level (:debug, :info, :warn, :error, :fatal)

Returns:

  • (Integer)

    Logger::Severity constant value or nil if input is not valid



34
35
36
37
38
# File 'lib/kitchen/util.rb', line 34

def self.to_logger_level(symbol)
  return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol)

  Logger.const_get(symbol.to_s.upcase)
end

.wrap_command(cmd) ⇒ String

Generates a command (or series of commands) wrapped so that it can be invoked on a remote instance or locally.

This method uses the Bourne shell (/bin/sh) to maximize the chance of cross platform portability on Unixlike systems.

Parameters:

  • the (String)

    command

Returns:

  • (String)

    a wrapped command string



110
111
112
113
114
115
116
# File 'lib/kitchen/util.rb', line 110

def self.wrap_command(cmd)
  cmd = "false" if cmd.nil?
  cmd = "true" if cmd.to_s.empty?
  cmd = cmd.sub(/\n\Z/, "") if cmd =~ /\n\Z/

  "sh -c '\n#{cmd}\n'"
end