spec/unit/loader_spec.rb in a9n-0.3.0 vs spec/unit/loader_spec.rb in a9n-0.3.1

- old
+ new

@@ -11,107 +11,85 @@ it { expect(subject.local_file).to eq(file_path) } it { expect(subject.example_file).to eq("#{file_path}.example") } end describe "#load" do - let(:example_env_config) { - { app_url: "http://127.0.0.1:3000", api_key: "base1234" } + let(:example_config) { + { app_url: "http://127.0.0.1:3000", api_key: "example1234" } } - let(:local_env_config) { + let(:local_config) { { app_host: "127.0.0.1:3000", api_key: "local1234" } } - let(:example_default_config) { - { page_title: "Base Kielbasa", api_key: "example1234default" } - } - let(:local_default_config) { - { page_title: "Local Kielbasa", api_key: "local1234default" } - } let(:env){ "tropical" } let(:config) { subject.get } context "when no configuration file exists" do before do expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(nil) expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.example_file, "defaults").and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.local_file, "defaults").and_return(nil) expect(subject).to receive(:verify!).never end it "raises expection" do expect { subject.load }.to raise_error(A9n::MissingConfigurationData) end end - context "when example configuration file exists with defaults" do + context "when only example configuration file exists" do before do - expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_env_config) + expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_config) expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.example_file, "defaults").and_return(example_default_config) - expect(described_class).to receive(:load_yml).with(subject.local_file, "defaults").and_return(nil) - expect(described_class).to receive(:verify!).never subject.load end it { expect(config.app_url).to eq("http://127.0.0.1:3000") } - it { expect(config.page_title).to eq("Base Kielbasa") } - it { expect(config.api_key).to eq("base1234") } + it { expect(config.api_key).to eq("example1234") } it { expect { config.app_host }.to raise_error(A9n::NoSuchConfigurationVariable) } end - context "when local configuration file exists with defaults" do + context "when only local configuration file exists" do before do expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(local_env_config) - expect(described_class).to receive(:load_yml).with(subject.example_file, "defaults").and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.local_file, "defaults").and_return(local_default_config) + expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(local_config) expect(described_class).to receive(:verify!).never subject.load end it { expect(config.app_host).to eq("127.0.0.1:3000") } - it { expect(config.page_title).to eq("Local Kielbasa") } it { expect(config.api_key).to eq("local1234") } it { expect { config.app_url }.to raise_error(A9n::NoSuchConfigurationVariable) } end context "when both local and base configuration file exists without defaults" do context "with same data" do before do - expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_env_config) - expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(example_env_config) - expect(described_class).to receive(:load_yml).with(subject.example_file, "defaults").and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.local_file, "defaults").and_return(nil) + expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_config) + expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(example_config) subject.load end it { expect(config.app_url).to eq("http://127.0.0.1:3000") } - it { expect(config.api_key).to eq("base1234") } + it { expect(config.api_key).to eq("example1234") } it { - expect { config.page_title }.to raise_error(A9n::NoSuchConfigurationVariable) - } - it { expect { config.app_host }.to raise_error(A9n::NoSuchConfigurationVariable) } end context "with different data" do before do - expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_env_config) - expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(local_env_config) - expect(described_class).to receive(:load_yml).with(subject.example_file, "defaults").and_return(nil) - expect(described_class).to receive(:load_yml).with(subject.local_file, "defaults").and_return(nil) + expect(described_class).to receive(:load_yml).with(subject.example_file, env).and_return(example_config) + expect(described_class).to receive(:load_yml).with(subject.local_file, env).and_return(local_config) end it "raises expection" do expect { subject.load }.to raise_error(A9n::MissingConfigurationVariables) @@ -123,26 +101,35 @@ describe ".load_yml" do let(:env) { "test" } subject { described_class.load_yml(file_path, env) } context "when file not exists" do - let(:file_path) { "file_not_existing_in_universe.yml" } + let(:file_path) { "file_not_existing_in_the_universe.yml" } it{ expect(subject).to be_nil } end context "when file exists" do - let(:file_path) { File.join(root, "config/configuration.yml") } + shared_examples "non-empty config file" do + it "returns non-empty hash" do + expect(subject).to be_kind_of(Hash) + expect(subject.keys).to_not be_empty + end + end before { ENV["DWARF"] = "erbized dwarf" } - context "and has data" do - it "returns non-empty hash" do - expect(subject).to be_kind_of(Hash) - expect(subject.keys).to_not be_empty + context "having env and defaults data" do + let(:file_path) { File.join(root, "config/configuration.yml") } + + it_behaves_like "non-empty config file" + + it "contains keys from defaults scope" do + expect(subject[:default_dwarf]).to eq("default dwarf") + expect(subject[:overriden_dwarf]).to eq("already overriden dwarf") end it "has symbolized keys" do expect(subject.keys.first).to be_kind_of(Symbol) expect(subject[:hash_dwarf]).to be_kind_of(Hash) @@ -152,12 +139,33 @@ it "parses erb" do expect(subject[:erb_dwarf]).to eq("erbized dwarf") end end - context "and has no data" do + context "having no env and only defaults data" do + let(:file_path) { File.join(root, "config/configuration.yml") } let(:env) { "production" } - it{ expect(subject).to be_nil } + + it_behaves_like "non-empty config file" + + it "contains keys from defaults scope" do + expect(subject[:default_dwarf]).to eq("default dwarf") + expect(subject[:overriden_dwarf]).to eq("not yet overriden dwarf") + end + end + + context "having only env and no default data" do + let(:file_path) { File.join(root, "config/no_defaults.yml") } + + context "valid env" do + let(:env) { "production" } + it_behaves_like "non-empty config file" + end + + context "invalid env" do + let(:env) { "tropical" } + it { expect(subject).to be_nil } + end end end end end