PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /opt/cpanel/ea-ruby27/src/passenger-release-6.0.23/test/ruby/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //opt/cpanel/ea-ruby27/src/passenger-release-6.0.23/test/ruby/debug_logging_spec.rb

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
PhusionPassenger.require_passenger_lib 'debug_logging'
require 'stringio'
require 'tmpdir'
require 'fileutils'

module PhusionPassenger

describe DebugLogging do
  after :each do
    DebugLogging.log_level = DEFAULT_LOG_LEVEL
    DebugLogging.log_file = nil
    DebugLogging.stderr_evaluator = nil
    FileUtils.rm_rf(@tmpdir) if @tmpdir
  end

  class MyClass
    include DebugLogging
  end

  def use_log_file!
    @tmpdir = Dir.mktmpdir
    @log_file = "#{@tmpdir}/debug.log"
    DebugLogging.log_file = @log_file
  end

  describe "#debug" do
    it "doesn't print the message if log level is LVL_INFO" do
      use_log_file!
      DebugLogging.log_level = LVL_INFO
      DebugLogging.debug("hello")
      File.exist?(@log_file).should be_falsey
    end

    it "prints the message if log level is LVL_DEBUG" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.debug("hello")
      File.exist?(@log_file).should be_truthy
    end

    it "prints the message if log level is greater than LVL_DEBUG" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG2
      DebugLogging.debug("hello")
      File.exist?(@log_file).should be_truthy
    end

    it "prints the location of the calling function" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.debug("hello")
      File.read(@log_file).should include("debug_logging_spec.rb")
    end

    it "prints to STDERR by default" do
      io = StringIO.new
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.stderr_evaluator = lambda { io }
      DebugLogging.debug("hello")
      io.string.should include("hello")
    end

    it "reopens the log file handle if it has been closed" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.debug("hello")
      DebugLogging._log_device.close
      DebugLogging.debug("world")
      File.read(@log_file).should include("world")
    end

    it "also works as included method" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      MyClass.new.send(:debug, "hello")
      File.read(@log_file).should include("hello")
    end

    it "is private when included" do
      MyClass.private_method_defined?(:debug)
    end
  end

  describe "#trace" do
    specify "#trace(x, ...) doesn't print the message if the log level is lower than LVL_INFO + x" do
      use_log_file!
      DebugLogging.log_level = LVL_INFO + 1
      DebugLogging.trace(2, "hello")
      File.exist?(@log_file).should be_falsey
    end

    specify "#trace(x, ...) prints the message if the log level equals LVL_INFO + 2" do
      use_log_file!
      DebugLogging.log_level = LVL_INFO + 2
      DebugLogging.trace(2, "hello")
      File.exist?(@log_file).should be_truthy
    end

    specify "#trace(x, ...) prints the message if the log level is greater than LVL_INFO + 3" do
      use_log_file!
      DebugLogging.log_level = LVL_INFO + 3
      DebugLogging.trace(2, "hello")
      File.exist?(@log_file).should be_truthy
    end

    specify "#trace prints the location of the calling function" do
      io = StringIO.new
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.stderr_evaluator = lambda { io }
      DebugLogging.trace(1, "hello")
      io.string.should include("hello")
    end

    it "prints to STDERR by default" do
      io = StringIO.new
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.stderr_evaluator = lambda { io }
      DebugLogging.trace(1, "hello")
      io.string.should include("hello")
    end

    it "reopens the log file handle if it has been closed" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      DebugLogging.trace(1, "hello")
      DebugLogging._log_device.close
      DebugLogging.trace(1, "world")
      File.read(@log_file).should include("world")
    end

    it "also works as included method" do
      use_log_file!
      DebugLogging.log_level = LVL_DEBUG
      MyClass.new.send(:trace, 1, "hello")
      File.read(@log_file).should include("hello")
    end

    it "is private when included" do
      MyClass.private_method_defined?(:trace)
    end
  end
end

end # module PhusionPassenger