def find_cookbook_name(run_context, filename)
      possibles = {}
      Poise.debug("[Poise] Checking cookbook for #{filename.inspect}")
      run_context.cookbook_collection.each do |name, ver|
        # This special method is added by Halite::Gem#as_cookbook_version.
        if ver.respond_to?(:halite_root)
          # The join is there because ../poise-ruby/lib starts with ../poise so
          # we want a trailing /.
          if filename.start_with?(File.join(ver.halite_root, ''))
            Poise.debug("[Poise] Found matching halite_root in #{name}: #{ver.halite_root.inspect}")
            possibles[ver.halite_root] = name
          end
        else
          Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |seg|
            ver.segment_filenames(seg).each do |file|
              if ::File::ALT_SEPARATOR
                file = file.gsub(::File::ALT_SEPARATOR, ::File::SEPARATOR)
              end
              # Put this behind an environment variable because it is verbose
              # even for normal debugging-level output.
              Poise.debug("[Poise] Checking #{seg} in #{name}: #{file.inspect}")
              if file == filename
                Poise.debug("[Poise] Found matching #{seg} in #{name}: #{file.inspect}")
                possibles[file] = name
              end
            end
          end
        end
      end
      raise Poise::Error.new("Unable to find cookbook for file #{filename.inspect}") if possibles.empty?
      # Sort the items by matching path length, pick the name attached to the longest.
      possibles.sort_by{|key, value| key.length }.last[1]
    end
def find_cookbook_name(run_context, filename)
      possibles = {}
      Poise.debug("[Poise] Checking cookbook for #{filename.inspect}")
      run_context.cookbook_collection.each do |name, ver|
        # This special method is added by Halite::Gem#as_cookbook_version.
        if ver.respond_to?(:halite_root)
          # The join is there because ../poise-ruby/lib starts with ../poise so
          # we want a trailing /.
          if filename.start_with?(File.join(ver.halite_root, ''))
            Poise.debug("[Poise] Found matching halite_root in #{name}: #{ver.halite_root.inspect}")
            possibles[ver.halite_root] = name
          end
        else
          Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |seg|
            ver.segment_filenames(seg).each do |file|
              if ::File::ALT_SEPARATOR
                file = file.gsub(::File::ALT_SEPARATOR, ::File::SEPARATOR)
              end
              # Put this behind an environment variable because it is verbose
              # even for normal debugging-level output.
              Poise.debug("[Poise] Checking #{seg} in #{name}: #{file.inspect}")
              if file == filename
                Poise.debug("[Poise] Found matching #{seg} in #{name}: #{file.inspect}")
                possibles[file] = name
              end
            end
          end
        end
      end
      raise Poise::Error.new("Unable to find cookbook for file #{filename.inspect}") if possibles.empty?
      # Sort the items by matching path length, pick the name attached to the longest.
      possibles.sort_by{|key, value| key.length }.last[1]
    end
Find the cookbook name for a given filename. The can used to find the
 cookbook that corresponds to a caller of a file.

 @param run_context [Chef::RunContext] Context to check.
 @param filename [String] Absolute filename to check for.
 @return [String]
 @example
   def my_thing
     caller_filename = caller.first.split(':').first
     cookbook = Poise::Utils.find_cookbook_name(run_context, caller_filename)
     # ...
   end