File: toml_spec.rb

package info (click to toggle)
ruby-configurate 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 200 kB
  • sloc: ruby: 992; makefile: 5
file content (112 lines) | stat: -rw-r--r-- 3,241 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
# frozen_string_literal: true

require "spec_helper"
require "configurate/provider/toml"

describe Configurate::Provider::TOML do
  PARSER = Configurate::Provider::TOML::PARSER

  let(:settings) {
    {
      "toplevel" => "bar",
      "some"     => {
        "nested" => {"some" => "lala", "setting" => "foo"}
      }
    }
  }

  describe "#initialize" do
    it "loads the file" do
      file = "foobar.toml"
      expect(PARSER).to receive(:load_file).with(file).and_return({})
      described_class.new file
    end

    it "raises if the file is not found" do
      allow(PARSER).to receive(:load_file).and_raise(Errno::ENOENT)
      expect {
        silence_stderr do
          described_class.new "foo"
        end
      }.to raise_error Errno::ENOENT
    end

    context "with a namespace" do
      it "looks in the file for that namespace" do
        namespace = "some.nested"
        allow(PARSER).to receive(:load_file).and_return(settings)
        provider = described_class.new "bla", namespace: namespace
        expect(provider.instance_variable_get(:@settings)).to eq settings["some"]["nested"]
      end

      it "raises if the namespace isn't found" do
        allow(PARSER).to receive(:load_file).and_return({})
        expect {
          silence_stderr do
            described_class.new "bla", namespace: "bar"
          end
        }.to raise_error ArgumentError
      end

      it "works with an empty namespace in the file" do
        allow(PARSER).to receive(:load_file).and_return("foo" => {"bar" => nil})
        expect {
          silence_stderr do
            described_class.new "bla", namespace: "foo.bar"
          end
        }.to_not raise_error
      end
    end

    context "with required set to false" do
      it "doesn't raise if a file isn't found" do
        allow(PARSER).to receive(:load_file).and_raise(Errno::ENOENT)
        expect {
          silence_stderr do
            described_class.new "not_me", required: false
          end
        }.not_to raise_error
      end

      it "doesn't raise if a namespace isn't found" do
        allow(PARSER).to receive(:load_file).and_return({})
        expect {
          silence_stderr do
            described_class.new "bla", namespace: "foo", required: false
          end
        }.not_to raise_error
      end
    end
  end

  describe "#lookup_path" do
    before do
      allow(PARSER).to receive(:load_file).and_return(settings)
      @provider = described_class.new "dummy"
    end

    it "looks up the whole nesting" do
      expect(@provider.lookup_path(%w[some nested some])).to eq settings["some"]["nested"]["some"]
    end

    it "returns nil if no setting is found" do
      expect(@provider.lookup_path(["not_me"])).to be_nil
    end

    context "with raise_on_missing set to true" do
      before do
        @provider = described_class.new "dummy", raise_on_missing: true
      end

      it "looks up the whole nesting" do
        expect(@provider.lookup_path(%w[some nested some])).to eq settings["some"]["nested"]["some"]
      end

      it "returns nil if no setting is found" do
        expect {
          @provider.lookup_path ["not_me"]
        }.to raise_error Configurate::MissingSetting
      end
    end
  end
end