Sha256: c0f1d50d2d9878f490b0713807c1b43655e41b88f94c17015f23766e672a82cc

Contents?: true

Size: 1.7 KB

Versions: 118

Compression:

Stored size: 1.7 KB

Contents

module Puppet::Pops
# Module for making a call such that there is an identifiable entry on
# the ruby call stack enabling getting a puppet call stack
# To use this make a call with:
# ```
# Puppet::Pops::PuppetStack.stack(file, line, receiver, message, args)
# ```
# To get the stack call:
# ```
# Puppet::Pops::PuppetStack.stacktrace
#
# When getting a backtrace in Ruby, the puppet stack frames are
# identified as coming from "in 'stack'" and having a ".pp" file
# name.
# To support testing, a given file that is an empty string, or nil
# as well as a nil line number are supported. Such stack frames
# will be represented with the text `unknown` and `0ยด respectively.
#
module PuppetStack
  # Pattern matching an entry in the ruby stack that is a puppet entry
  PP_ENTRY_PATTERN = /^(.*\.pp)?:([0-9]+):in (`stack'|`block in call_function'|`<eval>')/

  # Sends a message to an obj such that it appears to come from
  # file, line when calling stacktrace.
  #
  def self.stack(file, line, obj, message, args, &block)
    file = '' if file.nil?
    line = 0 if line.nil?

    if block_given?
      Kernel.eval("obj.send(message, *args, &block)", Kernel.binding(), file, line)
    else
      Kernel.eval("obj.send(message, *args)", Kernel.binding(), file, line)
    end
  end

  def self.stacktrace
    caller().reduce([]) do |memo, loc|
      if loc =~ PP_ENTRY_PATTERN
        memo << [$1.nil? ? 'unknown' : $1, $2.to_i]
      end
      memo
    end
  end

  # Returns an Array with the top of the puppet stack, or an empty Array if there was no such entry.
  #
  def self.top_of_stack
    caller.each do |loc|
      if loc =~ PP_ENTRY_PATTERN
        return [$1.nil? ? 'unknown' : $1, $2.to_i]
      end
    end
    []
  end
end
end

Version data entries

118 entries across 118 versions & 2 rubygems

Version Path
puppet-6.10.1 lib/puppet/pops/puppet_stack.rb
puppet-6.10.1-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.10.1-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.10.1-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-6.4.4 lib/puppet/pops/puppet_stack.rb
puppet-6.4.4-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.4.4-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.4.4-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-6.10.0 lib/puppet/pops/puppet_stack.rb
puppet-6.10.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.10.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.10.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-6.9.0 lib/puppet/pops/puppet_stack.rb
puppet-6.9.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.9.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.9.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-6.8.1 lib/puppet/pops/puppet_stack.rb
puppet-6.8.1-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.8.1-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-6.8.1-universal-darwin lib/puppet/pops/puppet_stack.rb