Skip to content

Commit

Permalink
Update config path to be env dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Nov 14, 2023
1 parent 5f2e6a2 commit 03ab47b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 26 deletions.
4 changes: 3 additions & 1 deletion lib/kangaru/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions sig/kangaru/interface.rbs
Original file line number Diff line number Diff line change
@@ -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?

Expand Down
50 changes: 31 additions & 19 deletions spec/features/importing_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
module SomeGem
extend Kangaru::Initialiser
config_path "#{config_path}"
config_path "#{config_path}" #{env && ", env: :#{env}"}
apply_config!
end
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
68 changes: 64 additions & 4 deletions spec/kangaru/interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 03ab47b

Please sign in to comment.