File: test_custom.rb

package info (click to toggle)
ruby-sshkit 1.21.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 700 kB
  • sloc: ruby: 3,522; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,547 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
69
70
71
require 'helper'

module SSHKit
  # Try to maintain backwards compatibility with Custom formatters defined by other people
  class TestCustom < UnitTest

    def setup
      super
      SSHKit.config.output_verbosity = Logger::DEBUG
    end

    def output
      @output ||= String.new
    end

    def custom
      @custom ||= CustomFormatter.new(output)
    end

    {
        log:   'LM 1 Test',
        fatal: 'LM 4 Test',
        error: 'LM 3 Test',
        warn:  'LM 2 Test',
        info:  'LM 1 Test',
        debug: 'LM 0 Test'
    }.each do |level, expected_output|
      define_method("test_#{level}_logging") do
        custom.send(level, 'Test')
        assert_log_output expected_output
      end
    end

    def test_write_logs_commands
      custom.write(Command.new(:ls))

      assert_log_output 'C 1 /usr/bin/env ls'
    end

    def test_double_chevron_logs_commands
      custom << Command.new(:ls)

      assert_log_output 'C 1 /usr/bin/env ls'
    end

    def test_accepts_options_hash
      custom = CustomFormatter.new(output, :foo => 'value')
      assert_equal('value', custom.options[:foo])
    end

    private

    def assert_log_output(expected_output)
      assert_equal expected_output, output
    end

  end

  class CustomFormatter < SSHKit::Formatter::Abstract
    def write(obj)
      original_output << \
        case obj
        when SSHKit::Command    then "C #{obj.verbosity} #{obj}"
        when SSHKit::LogMessage then "LM #{obj.verbosity} #{obj}"
        end
    end
    alias :<< :write

  end

end