File: logger_spec.rb

package info (click to toggle)
facter 4.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,276 kB
  • sloc: ruby: 64,130; sh: 48; makefile: 2
file content (336 lines) | stat: -rw-r--r-- 8,768 bytes parent folder | download
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# frozen_string_literal: true

describe Logger do
  subject(:log) { Facter::Log.new(Class) }

  let(:logger) { Facter::Log.class_variable_get(:@@logger) }

  before do
    Facter::Options[:color] = false
  end

  describe '#debug' do
    before do
      allow(Facter).to receive(:debugging?).and_return(true)
    end

    context 'when debugging is set to false' do
      it 'no debug messages are sent' do
        allow(Facter).to receive(:debugging?).and_return(false)

        expect(logger).not_to receive(:debug)

        log.debug('info_message')
      end
    end

    shared_examples 'writes debug message with no color' do
      it 'calls debug on logger' do
        expect(logger).to receive(:debug).with('Class - debug_message')

        log.debug('debug_message')
      end
    end

    shared_examples 'writes debug message with color' do
      it 'calls debug on logger' do
        expect(logger).to receive(:debug)
          .with("Class - #{Facter::CYAN}debug_message#{Facter::RESET}")

        log.debug('debug_message')
      end
    end

    it_behaves_like 'writes debug message with no color'

    context 'when message callback is provided' do
      after do
        Facter::Log.class_variable_set(:@@message_callback, nil)
      end

      it 'provides on_message hook' do
        logger_double = instance_spy(Logger)
        Facter.on_message do |level, message|
          logger_double.debug("on_message called with level: #{level}, message: #{message}")
        end

        log.debug('test')

        expect(logger_double).to have_received(:debug).with('on_message called with level: debug, message: test')
      end
    end

    context 'when call is made during os detection in os_detector.rb and facter.rb is not fully loaded' do
      before do
        allow(Facter).to receive(:respond_to?).with(:debugging?).and_return(false)
      end

      it_behaves_like 'writes debug message with no color'

      it 'does not call Facter.debugging?' do
        log.debug('debug_message')

        expect(Facter).not_to have_received(:debugging?)
      end
    end

    context 'when non Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:macosx)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it_behaves_like 'writes debug message with color'
      end
    end

    context 'when Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it_behaves_like 'writes debug message with color'
      end
    end
  end

  describe '#debugonce' do
    before do
      allow(Facter).to receive(:debugging?).and_return(true)
    end

    it 'writes the same debug message only once' do
      message = 'Some error message'

      expect(logger).to receive(:debug).once.with("Class - #{message}")

      log.debugonce(message)
      log.debugonce(message)
    end
  end

  describe '#info' do
    it 'writes info message' do
      expect(logger).to receive(:info).with('Class - info_message')

      log.info('info_message')
    end

    context 'when non Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:macosx)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it 'print Green (32) info message' do
          expect(logger)
            .to receive(:info)
            .with("Class - #{Facter::GREEN}info_message#{Facter::RESET}")

          log.info('info_message')
        end
      end
    end

    context 'when Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it 'print info message' do
          expect(logger).to receive(:info)
            .with("Class - #{Facter::GREEN}info_message#{Facter::RESET}")

          log.info('info_message')
        end
      end
    end
  end

  describe '#warn' do
    it 'writes warn message' do
      expect(logger).to receive(:warn).with('Class - warn_message')

      log.warn('warn_message')
    end

    context 'when non Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:macosx)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it 'print Yellow (33) info message' do
          expect(logger)
            .to receive(:warn)
            .with("Class - #{Facter::YELLOW}warn_message#{Facter::RESET}")

          log.warn('warn_message')
        end
      end
    end

    context 'when Windows OS' do
      before do
        allow(OsDetector.instance).to receive(:identifier).and_return(:windows)
      end

      context 'when --colorize option is enabled' do
        before do
          Facter::Options[:color] = true
        end

        it 'print warn message' do
          expect(logger).to receive(:warn)
            .with("Class - #{Facter::YELLOW}warn_message#{Facter::RESET}")

          log.warn('warn_message')
        end
      end
    end
  end

  describe '#warnonce' do
    before do
      allow(Facter).to receive(:debugging?).and_return(true)
    end

    it 'writes the same debug message only once' do
      message = 'Some error message'

      expect(logger).to receive(:warn).once.with("Class - #{message}")

      log.warnonce(message)
      log.warnonce(message)
    end
  end

  describe '#error' do
    it 'writes error message with color on macosx' do
      allow(OsDetector.instance).to receive(:detect).and_return(:macosx)

      expect(logger).to receive(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}")

      log.error('error_message', true)
    end

    it 'writes error message colorized on Windows' do
      allow(OsDetector.instance).to receive(:identifier).and_return(:windows)

      expect(logger).to receive(:error).with("Class - #{Facter::RED}error_message#{Facter::RESET}")

      log.error('error_message', true)
    end

    it 'writes error message' do
      expect(logger).to receive(:error).with('Class - error_message')

      log.error('error_message')
    end
  end

  describe '#level=' do
    it 'sets the log level' do
      expect(logger).to receive(:level=).with(:error)

      Facter::Log.level = :error
    end
  end

  describe '#log_exception' do
    let(:exception) { Exception.new('Test exception') }

    it 'writes exception message without --trace option' do
      expect(logger).to receive(:error).with("Class - #{colorize('Test exception', Facter::RED)}")

      log.log_exception(exception)
    end

    it 'writes exception message and backtrace with --trace option' do
      allow(Facter::Options).to receive(:[])
      allow(Facter::Options).to receive(:[]).with(:trace).and_return(true)
      allow(exception).to receive(:backtrace).and_return(['backtrace:1'])

      expect(logger)
        .to receive(:error)
        .with("Class - #{colorize("Test exception\nbacktrace:1", Facter::RED)}")

      log.log_exception(exception)
    end
  end

  describe '.clear_messages' do
    before do
      allow(Facter).to receive(:debugging?).and_return(true)
    end

    it 'clears debugonce messages' do
      message = 'Some error message'

      expect(logger).to receive(:debug).twice.with("Class - #{message}")

      log.debugonce(message)
      Facter::Log.clear_messages
      log.debugonce(message)
    end

    it 'clears warnonce messages' do
      message = 'Some error message'

      expect(logger).to receive(:warn).twice.with("Class - #{message}")

      log.warnonce(message)
      Facter::Log.clear_messages
      log.warnonce(message)
    end
  end

  describe '.show_time' do
    before do
      allow(Facter::Log).to receive(:timing?).and_return(true)
    end

    it 'prints the message to stderr' do
      expect { Facter::Log.show_time('foo') }.to output("\e[32mfoo\e[0m\n").to_stderr
    end
  end

  describe 'setting the timing mode' do
    it 'is enabled when the given value is true' do
      Facter::Log.timing(true)
      expect(Facter::Log.timing?).to be true
    end

    it 'is disabled when the given value is false' do
      Facter::Log.timing(false)
      expect(Facter::Log.timing?).to be false
    end

    it 'is disabled when the given value is nil' do
      Facter::Log.timing(nil)
      expect(Facter::Log.timing?).to be nil
    end
  end
end