Sha256: 2b785e3b5ad863aa7c6b9193b421134cba7ecb00be650067c65bd8273afcf602

Contents?: true

Size: 1.57 KB

Versions: 20

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require_relative '../../puppet/thread_local'

module Puppet
  module Pops
    # Utility class for keeping track of the "Puppet stack", ie the file
    # and line numbers of Puppet Code that created the current context.
    #
    # To use this make a call with:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stack(file, line, receiver, message, args)
    # ```
    #
    # To get the stack call:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stacktrace
    # ```
    #
    # or
    #
    # ```rb
    # Puppet::Pops::PuppetStack.top_of_stack
    # ```
    #
    # 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
      @stack = Puppet::ThreadLocal.new { Array.new }

      def self.stack(file, line, obj, message, args, &block)
        file = 'unknown' if file.nil? || file == ''
        line = 0 if line.nil?

        result = nil
        @stack.value.unshift([file, line])
        begin
          if block_given?
            result = obj.send(message, *args, &block)
          else
            result = obj.send(message, *args)
          end
        ensure
          @stack.value.shift()
        end
        result
      end

      def self.stacktrace
        @stack.value.dup
      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
        @stack.value.first || []
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/pops/puppet_stack.rb
puppet-8.10.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.10.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.10.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-8.9.0 lib/puppet/pops/puppet_stack.rb
puppet-8.9.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.9.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.9.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-8.8.1 lib/puppet/pops/puppet_stack.rb
puppet-8.8.1-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.8.1-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.8.1-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-8.7.0 lib/puppet/pops/puppet_stack.rb
puppet-8.7.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.7.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.7.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-8.6.0 lib/puppet/pops/puppet_stack.rb
puppet-8.6.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.6.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-8.6.0-universal-darwin lib/puppet/pops/puppet_stack.rb