Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boardfish committed Mar 19, 2024
1 parent 0b347fe commit 8e125c4
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/nvar/environment_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def typecast_value
end

def fetch_value(passthrough: false, default_value: nil)
return default_value || name.to_s.force_encoding("UTF-8") if Nvar.env.test? && !passthrough
return default_value || name.to_s if Nvar.env.test? && !passthrough

required ? ENV.fetch(name.to_s) : ENV[name.to_s]
end
Expand Down
155 changes: 143 additions & 12 deletions spec/nvar/environment_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@

let(:environment_variable) { described_class.new(**args) }

around do |example|
Nvar.env.then do |old_env|
Nvar.env = ActiveSupport::StringInquirer.new(nvar_env.to_s)
example.run
ensure
Nvar.env = old_env
end
end

let(:nvar_env) { :development }

describe "#initialize" do
subject(:initializer) { environment_variable }

Expand Down Expand Up @@ -112,85 +123,205 @@
context "when passthrough is false and a default value is provided" do
let(:args) { base_args.merge(passthrough: false, default_value: "default_value") }

it { is_expected.to eq("default_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("default_value") }
end
end

context "when passthrough is true and a default value is provided" do
let(:args) { base_args.merge(passthrough: true, default_value: "default_value") }

it { is_expected.to eq("passthrough_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("passthrough_value") }
end
end

context "when passthrough is false and a default value is not provided" do
let(:args) { base_args.merge(passthrough: false, default_value: nil) }

it { is_expected.to eq(environment_variable.name) }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq(environment_variable.name) }
end
end

context "when passthrough is true and a default value is not provided" do
let(:args) { base_args.merge(passthrough: true, default_value: nil) }

it { is_expected.to eq("passthrough_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("passthrough_value") }
end
end

context "when passthrough is unset in the environment and a default value is not provided" do
let(:args) { base_args.except(:passthrough).merge(default_value: nil) }

it { is_expected.to eq(environment_variable.name) }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq(environment_variable.name) }
end
end

context "when passthrough is unset in the environment and a default value is provided" do
let(:args) { base_args.except(:passthrough).merge(default_value: "default_value") }

it { is_expected.to eq("default_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("default_value") }
end
end

context "when passthrough is enabled through the environment and a default value is provided" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "SOME_OTHER_VAR,#{base_args[:name]}") { example.run } }

let(:args) { base_args.except(:passthrough).merge(default_value: "default_value") }

it { is_expected.to eq("passthrough_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("passthrough_value") }
end
end

context "when passthrough is enabled through the environment and a default value is not provided" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "SOME_OTHER_VAR,#{base_args[:name]}") { example.run } }

let(:args) { base_args.except(:passthrough).merge(default_value: nil) }

it { is_expected.to eq("passthrough_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("passthrough_value") }
end
end

context "when passthrough is enabled through the environment, but is overridden by the config" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "SOME_OTHER_VAR,#{base_args[:name]}") { example.run } }

let(:args) { base_args.merge(passthrough: false, default_value: nil) }

it { is_expected.to eq(environment_variable.name) }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq(environment_variable.name) }
end
end

context "when passthrough is disabled through the environment and a default value is provided" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "SOME_OTHER_VAR") { example.run } }

let(:args) { base_args.except(:passthrough).merge(default_value: "default_value") }

it { is_expected.to eq("default_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("default_value") }
end
end

context "when passthrough is disabled through the environment and a default value is not provided" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "false") { example.run } }

let(:args) { base_args.except(:passthrough).merge(default_value: nil) }

it { is_expected.to eq(environment_variable.name) }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq(environment_variable.name) }
end
end

context "when passthrough is disabled through the environment, but is overridden by the config" do
around { |example| ClimateControl.modify(NVAR_PASSTHROUGH: "SOME_OTHER_VAR") { example.run } }

let(:args) { base_args.merge(passthrough: true) }

it { is_expected.to eq("passthrough_value") }
context "and the environment is development" do
let(:nvar_env) { :development }

it { is_expected.to eq("passthrough_value") }
end

context "and the environment is test" do
let(:nvar_env) { :test }

it { is_expected.to eq("passthrough_value") }
end
end
end
end

0 comments on commit 8e125c4

Please sign in to comment.