From b4101be851efb95acf3ecb6da38812f5755c5cf9 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Fri, 2 Nov 2012 18:06:35 +1100 Subject: [PATCH] support setting boolean environment variables These were previously stored as a string if they are derived from the environment or configuration files. Rather than altering each of the checks, we can select a list of settings to convert to boolean values when they are accessed. --- lib/bundler/settings.rb | 13 +++++++++++-- spec/install/deploy_spec.rb | 14 ++++++++++++++ spec/other/config_spec.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index adf018505c0..0ce3565bb0e 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -7,8 +7,9 @@ def initialize(root) end def [](key) - key = key_for(key) - @local_config[key] || ENV[key] || @global_config[key] + the_key = key_for(key) + value = (@local_config[the_key] || ENV[the_key] || @global_config[the_key]) + is_bool(key) ? to_bool(value) : value end def []=(key, value) @@ -104,6 +105,14 @@ def key_for(key) "BUNDLE_#{key}" end + def is_bool(key) + %w(frozen cache_all no_prune disable_local_branch_check).include? key.to_s + end + + def to_bool(value) + !(value.nil? || value == '' || value =~ /^(false|f|no|n|0)$/i) + end + def set_key(key, value, hash, file) key = key_for(key) diff --git a/spec/install/deploy_spec.rb b/spec/install/deploy_spec.rb index 698d552ff85..fdfe64bff00 100644 --- a/spec/install/deploy_spec.rb +++ b/spec/install/deploy_spec.rb @@ -113,6 +113,20 @@ expect(out).not_to include("You have changed in the Gemfile") end + it "can have --frozen set to false via an environment variable" do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + gem "rack-obama" + G + + ENV['BUNDLE_FROZEN'] = "false" + bundle "install" + expect(out).not_to include("deployment mode") + expect(out).not_to include("You have added to the Gemfile") + expect(out).not_to include("* rack-obama") + end + it "explodes with the --frozen flag if you make a change and don't check in the lockfile" do gemfile <<-G source "file://#{gem_repo1}" diff --git a/spec/other/config_spec.rb b/spec/other/config_spec.rb index 8a30c688ae4..cb2e831feea 100644 --- a/spec/other/config_spec.rb +++ b/spec/other/config_spec.rb @@ -135,4 +135,35 @@ expect(out).to eq("local") end end + + describe "env" do + before(:each) { bundle :install } + + it "can set boolean properties via the environment" do + ENV["BUNDLE_FROZEN"] = "true" + + run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end" + expect(out).to eq("true") + end + + it "can set negative boolean properties via the environment" do + run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end" + expect(out).to eq("false") + + ENV["BUNDLE_FROZEN"] = "false" + + run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end" + expect(out).to eq("false") + + ENV["BUNDLE_FROZEN"] = "0" + + run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end" + expect(out).to eq("false") + + ENV["BUNDLE_FROZEN"] = "" + + run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end" + expect(out).to eq("false") + end + end end