File: options_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 (117 lines) | stat: -rw-r--r-- 3,613 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
# frozen_string_literal: true

describe Facter::Options do
  subject(:options) { Facter::Options }

  describe '#init_from_cli' do
    let(:option_store) { class_spy('Facter::OptionStore') }
    let(:config_file_options) { class_spy('Facter::ConfigFileOptions') }
    let(:options_validator) { class_spy('Facter::OptionsValidator') }

    before do
      stub_const('Facter::ConfigFileOptions', config_file_options)
      stub_const('Facter::OptionStore', option_store)
      stub_const('Facter::OptionsValidator', options_validator)
      allow(config_file_options).to receive(:get).and_return({})
    end

    it 'calls OptionStore with cli' do
      Facter::Options.init_from_cli

      expect(option_store).to have_received(:cli=).with(true)
    end

    it 'calls OptionStore with show_legacy' do
      Facter::Options.init_from_cli

      expect(option_store).to have_received(:show_legacy=).with(false)
    end

    context 'with config_file' do
      let(:config_file_opts) { { 'debug' => true, 'ruby' => true } }

      before do
        allow(config_file_options).to receive(:get).and_return(config_file_opts)
      end

      it 'calls ConfigFileOptions.init with config_path' do
        Facter::Options.init_from_cli(config: 'path/to/config')

        expect(config_file_options).to have_received(:init).with('path/to/config')
      end

      it 'calls OptionStore.set.init with cli_options' do
        Facter::Options.init_from_cli

        config_file_opts.each do |key, value|
          expect(option_store).to have_received(:set).with(key, value)
        end
      end
    end

    context 'with cli_options' do
      let(:cli_options) { { 'debug' => true, 'ruby' => true } }

      it 'calls OptionStore.set.init with cli_options' do
        Facter::Options.init_from_cli(cli_options)

        cli_options.each do |key, value|
          expect(option_store).to have_received(:set).with(key, value)
        end
      end

      context 'with log_level as option' do
        it 'munges the value none to unknown' do
          Facter::Options.init_from_cli({ 'log_level' => 'none' })

          expect(option_store).to have_received(:set).with('log_level', 'unknown')
        end

        it 'munges the value log_level to empty string' do
          Facter::Options.init_from_cli({ 'log_level' => 'log_level' })

          expect(option_store).to have_received(:set).with('log_level', '')
        end

        it 'leaves known log level unmunged' do
          Facter::Options.init_from_cli({ 'log_level' => 'debug' })

          expect(option_store).to have_received(:set).with('log_level', 'debug')
        end
      end
    end
  end

  describe '#init' do
    let(:option_store) { class_spy('Facter::OptionStore') }
    let(:config_file_options) { class_spy('Facter::ConfigFileOptions') }

    before do
      stub_const('Facter::ConfigFileOptions', config_file_options)
      stub_const('Facter::OptionStore', option_store)
      allow(config_file_options).to receive(:get).and_return({})
    end

    it 'calls OptionStore with cli' do
      Facter::Options.init

      expect(option_store).to have_received(:cli=).with(false)
    end

    context 'with config_file' do
      let(:config_file_opts) { { 'debug' => true, 'ruby' => true } }

      before do
        allow(config_file_options).to receive(:get).and_return(config_file_opts)
      end

      it 'calls OptionStore.set.init with cli_options' do
        Facter::Options.init

        config_file_opts.each do |key, value|
          expect(option_store).to have_received(:set).with(key, value)
        end
      end
    end
  end
end