File: logging_spec.rb

package info (click to toggle)
ruby-neovim 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 548 kB
  • sloc: ruby: 4,178; sh: 23; makefile: 4
file content (129 lines) | stat: -rw-r--r-- 3,979 bytes parent folder | download | duplicates (3)
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
require "helper"
require "multi_json"

module Neovim
  RSpec.describe Logging do
    around do |spec|
      old_logger = Logging.logger

      begin
        Logging.send(:remove_instance_variable, :@logger)
        spec.run
      ensure
        Logging.logger = old_logger
      end
    end

    describe ".logger" do
      it "fetches the output from $NVIM_RUBY_LOG_FILE" do
        logger = instance_double(Logger, :level= => nil, :formatter= => nil)
        expect(Logger).to receive(:new).with("/tmp/nvim.log").and_return(logger)
        Logging.logger("NVIM_RUBY_LOG_FILE" => "/tmp/nvim.log")
        expect(Logging.logger).to be(logger)
      end

      it "defaults the output to STDERR" do
        logger = instance_double(Logger, :level= => nil, :formatter= => nil)
        expect(Logger).to receive(:new).with(STDERR).and_return(logger)
        Logging.logger({})
        expect(Logging.logger).to be(logger)
      end

      it "fetches the level from $NVIM_RUBY_LOG_LEVEL as a string" do
        logger = instance_double(Logger, :formatter= => nil)
        expect(Logger).to receive(:new).and_return(logger)
        expect(logger).to receive(:level=).with(Logger::DEBUG)
        Logging.logger("NVIM_RUBY_LOG_LEVEL" => "DEBUG")
        expect(Logging.logger).to be(logger)
      end

      it "fetches the level from $NVIM_RUBY_LOG_LEVEL as an integer" do
        logger = instance_double(Logger, :formatter= => nil)
        expect(Logger).to receive(:new).and_return(logger)
        expect(logger).to receive(:level=).with(0)
        Logging.logger("NVIM_RUBY_LOG_LEVEL" => "0")
        expect(Logging.logger).to be(logger)
      end

      it "defaults the level to WARN" do
        logger = instance_double(Logger, :formatter= => nil)
        expect(Logger).to receive(:new).and_return(logger)
        expect(logger).to receive(:level=).with(Logger::WARN)
        Logging.logger({})
        expect(Logging.logger).to be(logger)
      end
    end

    describe Logging::Helpers do
      let!(:log) do
        StringIO.new.tap do |io|
          logger = Logger.new(io)
          logger.level = Logger::DEBUG
          Neovim.logger = logger
        end
      end

      let(:klass) do
        Class.new do
          include Logging

          def public_log(level, fields)
            log(level) { fields }
          end

          def public_log_exception(*args)
            log_exception(*args)
          end
        end
      end

      let(:obj) { klass.new }

      describe "#log" do
        it "logs JSON at the specified level" do
          obj.public_log(:info, foo: "bar")
          logged = MultiJson.decode(log.string)

          expect(logged).to match(
            "_level" => "INFO",
            "_class" => klass.to_s,
            "_method" => "public_log",
            "_time" => /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+/,
            "foo" => "bar"
          )
        end
      end

      describe "#log_exception" do
        it "logs JSON at the specified level and debugs the backtrace" do
          ex = RuntimeError.new("BOOM")
          ex.set_backtrace(["one", "two"])
          obj.public_log_exception(:fatal, ex, :some_method)
          lines = log.string.lines.to_a

          fatal = MultiJson.decode(lines[0])
          debug = MultiJson.decode(lines[1])

          expect(fatal).to match(
            "_level" => "FATAL",
            "_class" => klass.to_s,
            "_method" => "some_method",
            "_time" => /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+/,
            "exception" => "RuntimeError",
            "message" => "BOOM"
          )

          expect(debug).to match(
            "_level" => "DEBUG",
            "_class" => klass.to_s,
            "_method" => "some_method",
            "_time" => /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+/,
            "exception" => "RuntimeError",
            "message" => "BOOM",
            "backtrace" => ["one", "two"]
          )
        end
      end
    end
  end
end