File: test_logging.rb

package info (click to toggle)
ruby-logging 2.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 6,139; sh: 11; makefile: 2
file content (258 lines) | stat: -rw-r--r-- 7,625 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

require File.expand_path('../setup', __FILE__)

module TestLogging

  class TestLogging < Test::Unit::TestCase
    include LoggingTestCase

    def setup
      super
      @levels = ::Logging::LEVELS
      @lnames = ::Logging::LNAMES

      @fn = File.join(TMP, 'test.log')
      @glob = File.join(TMP, '*.log')
    end

    def test_backtrace
      assert_equal true, ::Logging.backtrace

      assert_equal false, ::Logging.backtrace('off')
      assert_equal false, ::Logging.backtrace

      assert_equal true, ::Logging.backtrace('on')
      assert_equal true, ::Logging.backtrace

      assert_equal false, ::Logging.backtrace(:off)
      assert_equal false, ::Logging.backtrace

      assert_equal true, ::Logging.backtrace(:on)
      assert_equal true, ::Logging.backtrace

      assert_equal false, ::Logging.backtrace(false)
      assert_equal false, ::Logging.backtrace

      assert_equal true, ::Logging.backtrace(true)
      assert_equal true, ::Logging.backtrace

      assert_raise(ArgumentError) {::Logging.backtrace 'foo'}
    end

    def test_utc_offset
      assert_nil ::Logging.utc_offset

      ::Logging.utc_offset = 0
      assert_equal 0, ::Logging.utc_offset

      ::Logging.utc_offset = "UTC"
      assert_equal 0, ::Logging.utc_offset

      ::Logging.utc_offset = "+01:00"
      assert_equal "+01:00", ::Logging.utc_offset

      assert_raise(ArgumentError) {::Logging.utc_offset = "06:00"}
    end

    def test_cause_depth
      assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth

      ::Logging.cause_depth = 0
      assert_equal 0, ::Logging.cause_depth

      ::Logging.cause_depth = nil
      assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth

      ::Logging.cause_depth = "1024"
      assert_equal 1024, ::Logging.cause_depth

      ::Logging.cause_depth = -1
      assert_equal ::Logging::DEFAULT_CAUSE_DEPTH, ::Logging.cause_depth

      assert_raise(ArgumentError) {::Logging.cause_depth = "foo"}
    end

    def test_basepath
      assert_nil ::Logging.basepath

      ::Logging.basepath = ""
      assert_nil ::Logging.basepath

      ::Logging.basepath = "./"
      assert_equal File.expand_path("../../", __FILE__), ::Logging.basepath

      ::Logging.reset
      assert_nil ::Logging.basepath
    end

    def test_logger
      assert_raise(TypeError) {::Logging.logger []}

      logger = ::Logging.logger STDOUT
      assert_match %r/\A-?\d+\z/, logger.name
      assert_same logger, ::Logging.logger(STDOUT)

      logger.close
      assert !STDOUT.closed?

      assert !File.exist?(@fn)
      fd = File.new @fn, 'w'
      logger = ::Logging.logger fd, 2, 100
      assert_equal @fn, logger.name
      logger.debug 'this is a debug message'
      logger.warn 'this is a warning message'
      logger.error 'and now we should have over 100 bytes of data ' +
                   'in the log file'
      logger.info 'but the log file should not roll since we provided ' +
                  'a file descriptor -- not a file name'
      logger.close
      assert fd.closed?
      assert File.exist?(@fn)
      assert_equal 1, Dir.glob(@glob).length

      FileUtils.rm_f @fn
      assert !File.exist?(@fn)
      logger = ::Logging.logger @fn, 2, 100
      assert File.exist?(@fn)
      assert_equal @fn, logger.name
      logger.debug 'this is a debug message'
      logger.warn 'this is a warning message'
      logger.error 'and now we should have over 100 bytes of data ' +
                   'in the log file'
      logger.info 'but the log file should not roll since we provided ' +
                  'a file descriptor -- not a file name'
      logger.close
      assert_equal 3, Dir.glob(@glob).length
    end

    def test_init_default
      assert_equal({}, @levels)
      assert_equal([], @lnames)
      assert_same false, ::Logging.initialized?

      ::Logging::Repository.instance

      assert_equal 5, @levels.length
      assert_equal 5, @lnames.length
      assert_equal 5, ::Logging::MAX_LEVEL_LENGTH

      assert_equal 0, @levels['debug']
      assert_equal 1, @levels['info']
      assert_equal 2, @levels['warn']
      assert_equal 3, @levels['error']
      assert_equal 4, @levels['fatal']

      assert_equal 'DEBUG', @lnames[0]
      assert_equal 'INFO',  @lnames[1]
      assert_equal 'WARN',  @lnames[2]
      assert_equal 'ERROR', @lnames[3]
      assert_equal 'FATAL', @lnames[4]
    end

    def test_init_special
      assert_equal({}, @levels)
      assert_equal([], @lnames)
      assert_same false, ::Logging.initialized?

      assert_raise(ArgumentError) {::Logging.init(1, 2, 3, 4)}

      ::Logging.init :one, 'two', :THREE, 'FoUr', :sIx

      assert_equal 5, @levels.length
      assert_equal 5, @lnames.length
      assert_equal 5, ::Logging::MAX_LEVEL_LENGTH

      assert_equal 0, @levels['one']
      assert_equal 1, @levels['two']
      assert_equal 2, @levels['three']
      assert_equal 3, @levels['four']
      assert_equal 4, @levels['six']

      assert_equal 'ONE',   @lnames[0]
      assert_equal 'TWO',   @lnames[1]
      assert_equal 'THREE', @lnames[2]
      assert_equal 'FOUR',  @lnames[3]
      assert_equal 'SIX',   @lnames[4]
    end

    def test_init_all_off
      assert_equal({}, @levels)
      assert_equal([], @lnames)
      assert_same false, ::Logging.initialized?

      ::Logging.init %w(a b all c off d)

      assert_equal 4, @levels.length
      assert_equal 4, @lnames.length
      assert_equal 3, ::Logging::MAX_LEVEL_LENGTH

      assert_equal 0, @levels['a']
      assert_equal 1, @levels['b']
      assert_equal 2, @levels['c']
      assert_equal 3, @levels['d']

      assert_equal 'A', @lnames[0]
      assert_equal 'B', @lnames[1]
      assert_equal 'C', @lnames[2]
      assert_equal 'D', @lnames[3]
    end

    def test_format_as
      assert_equal false, ::Logging.const_defined?('OBJ_FORMAT')

      assert_raises(ArgumentError) {::Logging.format_as 'bob'}
      assert_raises(ArgumentError) {::Logging.format_as String}
      assert_raises(ArgumentError) {::Logging.format_as :what?}

      remove_const = lambda do |const|
        ::Logging.class_eval {remove_const const if const_defined? const}
      end

      ::Logging.format_as :string
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :string, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as :inspect
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :inspect, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as :json
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :json, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as :yaml
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :yaml, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as 'string'
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :string, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as 'inspect'
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :inspect, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]

      ::Logging.format_as 'yaml'
      assert ::Logging.const_defined?('OBJ_FORMAT')
      assert_equal :yaml, ::Logging::OBJ_FORMAT
      remove_const[:OBJ_FORMAT]
    end

    def test_path
      path = ::Logging.path(*%w[one two three])
      assert_match %r/one\/two\/three$/, path
    end

    def test_version
      assert_match %r/\d+\.\d+\.\d+/, ::Logging.version
    end

  end  # class TestLogging
end  # module TestLogging