File: logging_spec.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (68 lines) | stat: -rw-r--r-- 2,246 bytes parent folder | download | duplicates (5)
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
require 'spec_helper'
require 'r10k/logging'

describe R10K::Logging do

  describe "parsing a log level" do
    it "parses 'true:TrueClass' as INFO" do
      expect(described_class.parse_level(true)).to eq Log4r::INFO
    end

    it "parses 'true:String' as nil" do
      expect(described_class.parse_level("true")).to be_nil
    end

    it "parses a numeric string as an integer" do
      expect(described_class.parse_level('2')).to eq 2
    end

    it "parses a log level string as a log level" do
      expect(described_class.parse_level('debug')).to eq Log4r::DEBUG
    end

    it "returns nil when given an invalid log level" do
      expect(described_class.parse_level('deblag')).to be_nil
    end
  end

  describe "setting the log level" do
    after(:all) { R10K::Logging.level = 'warn' }

    it "sets the outputter log level" do
      expect(described_class.outputter).to receive(:level=).with(Log4r::DEBUG)
      described_class.level = 'debug'
    end

    it "stores the new log level" do
      allow(described_class.outputter).to receive(:level=)
      described_class.level = 'debug'
      expect(described_class.level).to eq(Log4r::DEBUG)
    end

    it "raises an exception when given an invalid log level" do
      expect {
        described_class.level = 'deblag'
      }.to raise_error(ArgumentError, /Invalid log level/)
    end

    describe "switching the formatter" do
      before do
        allow(described_class.outputter).to receive(:level=)
      end

      it "switches to the debug formatter if the new log level is debug or greater" do
        debug_formatter = double('debug formatter')
        expect(described_class).to receive(:debug_formatter).and_return(debug_formatter)
        expect(described_class.outputter).to receive(:formatter=).with(debug_formatter)
        described_class.level = 'debug'
      end

      it "switches to the default formatter if the new log level is info or less" do
        default_formatter = double('default formatter')
        expect(described_class).to receive(:default_formatter).and_return(default_formatter)
        expect(described_class.outputter).to receive(:formatter=).with(default_formatter)
        described_class.level = 'info'
      end
    end
  end
end