Skip to content

Commit

Permalink
Enable passthrough by specifying environment variable name in NVAR_PA…
Browse files Browse the repository at this point in the history
…SSTHROUGH
  • Loading branch information
boardfish committed Feb 28, 2024
1 parent 4879732 commit b13600a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/nvar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "nvar/environment_variable"
require "nvar/engine" if defined?(Rails)
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/hash/reverse_merge"

# Centralized configuration for required environment variables in your Ruby app.
module Nvar
Expand Down Expand Up @@ -62,7 +63,6 @@ def filter_from_vcr_cassettes(config)

def all
variables.map do |variable_name, config|
# TODO: Passthrough from environment behaviour might need to go here?
EnvironmentVariable.new(**(config || {}).merge(name: variable_name))
end.partition(&:set?)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nvar/environment_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(name:, type: "String", filter_from_requests: nil, **args)
@type = type
@required = args[:required].nil? ? true : args[:required]
@filter_from_requests = filter_from_requests.yield_self { |f| [true, false].include?(f) ? f : f&.to_sym }
@value = fetch_value(**args.slice(:passthrough, :default_value))
@value = fetch_value(**args.slice(:passthrough, :default_value).with_defaults(passthrough: ENV.fetch("NVAR_PASSTHROUGH", "").split(",").include?(name)))
@defined = true
rescue KeyError
@value = args[:default_value]
Expand Down
60 changes: 60 additions & 0 deletions spec/nvar/environment_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,65 @@

it { is_expected.to eq("passthrough_value") }
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) }
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") }
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") }
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") }
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) }
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") }
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) }
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") }
end
end
end
2 changes: 2 additions & 0 deletions spec/nvar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@

context "when the env file exists and an optional environment variable is unset" do
let!(:env) { base_env.except(:OPTIONAL_ENV_VAR) }

it { is_expected.to be_empty }
end

context "when the env file exists and an environment variable with a default value is unset" do
Expand Down

0 comments on commit b13600a

Please sign in to comment.