From 03ab47be8c86dd71078d4e3fc1a97387a4ccba8a Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Mon, 13 Nov 2023 23:39:51 +0000 Subject: [PATCH] Update config path to be env dependent --- lib/kangaru/interface.rb | 4 +- sig/kangaru/interface.rbs | 6 ++- spec/features/importing_config_spec.rb | 50 ++++++++++++------- spec/kangaru/interface_spec.rb | 68 ++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 26 deletions(-) diff --git a/lib/kangaru/interface.rb b/lib/kangaru/interface.rb index 00cbb6c..4c6e676 100644 --- a/lib/kangaru/interface.rb +++ b/lib/kangaru/interface.rb @@ -14,7 +14,9 @@ def configure(env: nil, &) Kangaru.application!.configure(&) end - def config_path(path) + def config_path(path, env: nil) + return unless env_applies?(env) + Kangaru.application!.config_path = path end diff --git a/sig/kangaru/interface.rbs b/sig/kangaru/interface.rbs index 6d19766..63b9f8c 100644 --- a/sig/kangaru/interface.rbs +++ b/sig/kangaru/interface.rbs @@ -1,14 +1,16 @@ module Kangaru module Interface + type env = Symbol? + def run!: (*String) -> void def config: -> Config - def configure: (?env: Symbol?) { (Config) -> void } -> void + def configure: (?env: env) { (Config) -> void } -> void def apply_config!: -> void - def config_path: (String) -> void + def config_path: (String, ?env: env) -> void def database: -> Database? diff --git a/spec/features/importing_config_spec.rb b/spec/features/importing_config_spec.rb index 7e3dd6d..873f530 100644 --- a/spec/features/importing_config_spec.rb +++ b/spec/features/importing_config_spec.rb @@ -12,7 +12,7 @@ module SomeGem extend Kangaru::Initialiser - config_path "#{config_path}" + config_path "#{config_path}" #{env && ", env: :#{env}"} apply_config! end @@ -31,6 +31,8 @@ class CustomConfigurator < Kangaru::Configurator RUBY end + let(:env) { nil } + shared_examples :does_not_import_config do it "does not raise any errors" do expect { gem.load! }.not_to raise_error @@ -62,37 +64,47 @@ class CustomConfigurator < Kangaru::Configurator context "when application config path is defined" do let(:config_path) { gem.path("config", ext: :yml) } - context "and config file does not exist" do + context "and not running from the specified env" do + let(:env) { :another_env } + include_examples :does_not_import_config end - context "and config file exists" do - before { config_path.write(config_file) } - - context "and file is empty" do - let(:config_file) { nil } + context "and running from the specified env" do + let(:env) { Kangaru.env } + context "and config file does not exist" do include_examples :does_not_import_config end - context "and file is not empty" do - let(:config_file) do - <<~YAML - #{key}: - some_setting: "foobar" - YAML - end + context "and config file exists" do + before { config_path.write(config_file) } - context "and config settings do not correspond to a configurator" do - let(:key) { "invalid" } + context "and file is empty" do + let(:config_file) { nil } include_examples :does_not_import_config end - context "and config settings correspond to a configurator" do - let(:key) { "custom" } + context "and file is not empty" do + let(:config_file) do + <<~YAML + #{key}: + some_setting: "foobar" + YAML + end + + context "and config settings do not correspond to a configurator" do + let(:key) { "invalid" } + + include_examples :does_not_import_config + end - include_examples :imports_config, some_setting: "foobar" + context "and config settings correspond to a configurator" do + let(:key) { "custom" } + + include_examples :imports_config, some_setting: "foobar" + end end end end diff --git a/spec/kangaru/interface_spec.rb b/spec/kangaru/interface_spec.rb index e525925..959d742 100644 --- a/spec/kangaru/interface_spec.rb +++ b/spec/kangaru/interface_spec.rb @@ -131,13 +131,73 @@ end describe "#config_path" do - subject(:config_path) { target.config_path(path) } + subject(:config_path) { target.config_path(path, env:) } let(:path) { "/foo/bar/config.yml" } - include_examples :delegates_to_application, - via: :config_path=, - args: "/foo/bar/config.yml" + let(:current_env) { :some_env } + + before do + allow(Kangaru).to receive(:env).and_return(current_env) + end + + shared_examples :does_not_set_config_path do + it "does not raise any errors" do + expect { config_path }.not_to raise_error + end + + it "does not set the config path" do + config_path + expect(application).not_to have_received(:config_path=) + end + end + + shared_examples :sets_config_path do + it "does not raise any errors" do + expect { config_path }.not_to raise_error + end + + it "sets the config path" do + config_path + expect(application).to have_received(:config_path=).with(path) + end + end + + context "when application not initialised" do + let(:env) { nil } + + include_context :application_not_initialised + + it "raises an error" do + expect { config_path }.to raise_error("application not set") + end + end + + context "when application initialised" do + include_context :application_initialised + + context "and no env is specified" do + let(:env) { nil } + + include_examples :sets_config_path + end + + context "and env is specified" do + let(:env) { :some_env } + + context "and not running app in specified env" do + let(:current_env) { :another_env } + + include_examples :does_not_set_config_path + end + + context "and running app in specified env" do + let(:current_env) { env } + + include_examples :sets_config_path + end + end + end end describe "#apply_config!" do