File: debug_logging_spec.rb

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (141 lines) | stat: -rw-r--r-- 3,708 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'phusion_passenger/debug_logging'
require 'phusion_passenger/utils/tmpdir'
require 'stringio'

module PhusionPassenger

describe DebugLogging do
	after :each do
		DebugLogging.log_level = 0
		DebugLogging.log_file = nil
		DebugLogging.stderr_evaluator = nil
	end
	
	class MyClass
		include DebugLogging
	end
	
	def use_log_file!
		@log_file = PhusionPassenger::Utils.passenger_tmpdir + "/debug.log"
		DebugLogging.log_file = @log_file
	end
	
	describe "#debug" do
		it "doesn't print the message if log level is 0" do
			use_log_file!
			DebugLogging.debug("hello")
			File.exist?(@log_file).should be_false
		end
		
		it "prints the message if log level is 1" do
			use_log_file!
			DebugLogging.log_level = 1
			DebugLogging.debug("hello")
			File.exist?(@log_file).should be_true
		end
		
		it "prints the message if log level is greater than 1" do
			use_log_file!
			DebugLogging.log_level = 2
			DebugLogging.debug("hello")
			File.exist?(@log_file).should be_true
		end
		
		it "prints the location of the calling function" do
			use_log_file!
			DebugLogging.log_level = 1
			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 = 1
			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 = 1
			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 = 1
			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 x" do
			use_log_file!
			DebugLogging.log_level = 1
			DebugLogging.trace(2, "hello")
			File.exist?(@log_file).should be_false
		end
		
		specify "#trace(x, ...) prints the message if the log level equals x" do
			use_log_file!
			DebugLogging.log_level = 2
			DebugLogging.trace(2, "hello")
			File.exist?(@log_file).should be_true
		end
		
		specify "#trace(x, ...) prints the message if the log level is greater than x" do
			use_log_file!
			DebugLogging.log_level = 3
			DebugLogging.trace(2, "hello")
			File.exist?(@log_file).should be_true
		end
		
		specify "#trace prints the location of the calling function" do
			io = StringIO.new
			DebugLogging.log_level = 1
			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 = 1
			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 = 1
			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 = 1
			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