Skip to content

Commit

Permalink
Review fixes + additional specs added
Browse files Browse the repository at this point in the history
  • Loading branch information
fargelus authored and palkan committed Mar 11, 2022
1 parent 5af5e2d commit e000e67
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -562,7 +562,7 @@ development:
port: 3000
```

Behavior for load all data under single environment enable by default:
If `Anyway::Settings.current_environment` is missed we assume that the YAML contains values for a single environment:

```yml
host: localhost
Expand Down
2 changes: 1 addition & 1 deletion lib/anyway/config.rb
Expand Up @@ -137,7 +137,7 @@ def required(*names, env: nil)
end

def filter_by_env(names, env)
return names if ["", current_env].any?(env.to_s)
return names if env.nil? || env.to_s == current_env

filtered_names = if env.is_a?(Hash)
names_with_exclude_env_option(names, env)
Expand Down
2 changes: 1 addition & 1 deletion lib/anyway/loaders/yaml.rb
Expand Up @@ -38,7 +38,7 @@ def environmental?(parsed_yml)

def config_with_env(config)
env_config = config[Settings.current_environment] || {}
return env_config if Settings.default_environmental_key.blank?
return env_config unless Settings.default_environmental_key?

default_config = config[Settings.default_environmental_key] || {}
Utils.deep_merge!(default_config, env_config)
Expand Down
2 changes: 1 addition & 1 deletion lib/anyway/settings.rb
Expand Up @@ -78,7 +78,7 @@ def app_root
end

def default_environmental_key?
[nil, ""].none?(default_environmental_key)
!default_environmental_key.nil?
end
end

Expand Down
42 changes: 39 additions & 3 deletions spec/config_spec.rb
Expand Up @@ -721,16 +721,52 @@ def self.name
end

context "when required env params missed" do
let(:missed_keys) { %i[user password] }
context "when env param string" do
let(:missed_keys) { [:api_key] }

it_behaves_like "raises ValidationError"
it_behaves_like "raises ValidationError"
end

context "when env param symbol" do
let(:app_config) do
Class.new(described_class) do
config_name "app"
attr_config :host, :port

required :host, env: :production
end
end

let(:missed_keys) { [:host] }

it_behaves_like "raises ValidationError"
end

context "when env is array of strings" do
let(:missed_keys) { %i[user password] }

it_behaves_like "raises ValidationError"
end

context "when env param is array of symbols" do
let(:app_config) do
Class.new(described_class) do
config_name "app"
attr_config :host, :port

required :host, env: %i[production demo]
end
end

let(:missed_keys) { [:host] }
it_behaves_like "raises ValidationError"
end
end

context "when current env match env option under except key" do
before { allow(Anyway::Settings).to receive(:current_environment).and_return("test") }

let(:missed_keys) { [:redis_host] }
let(:config_values) { {host: "localhost", port: 80} }

it "not raises ValidationError" do
expect { subject }.to_not raise_error(Anyway::Config::ValidationError, error_msg)
Expand Down

0 comments on commit e000e67

Please sign in to comment.