summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-11-17 14:45:24 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2014-11-17 14:45:24 -0800
commit3a69a92f622c1bcf49be712a6c41a1c0828346e5 (patch)
tree1788954a0be84149dd03e5e7b92a326aeeba0924
parent8d0aeb1716bda762cf6506d8a8f3e570cde600b2 (diff)
downloadchef-lcg/memoize-locale.tar.gz
stop recomputing locale -a constantlylcg/memoize-locale
fixes an outstanding issue on the locale -a guessing also fixes perf issues in the rspec tests (e.g. deploy provider)
-rw-r--r--lib/chef/config.rb66
-rw-r--r--spec/unit/config_spec.rb6
2 files changed, 36 insertions, 36 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index d3871c38e8..451f466e01 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -623,44 +623,44 @@ class Chef
#
# If there is no 'locale -a' then we return 'en_US.UTF-8' since that is the most commonly
# available English UTF-8 locale. However, all modern POSIXen should support 'locale -a'.
- default :internal_locale do
- begin
- # https://github.com/opscode/chef/issues/2181
- # Some systems have the `locale -a` command, but the result has
- # invalid characters for the default encoding.
- #
- # For example, on CentOS 6 with ENV['LANG'] = "en_US.UTF-8",
- # `locale -a`.split fails with ArgumentError invalid UTF-8 encoding.
- locales = shell_out_with_systems_locale("locale -a").stdout.split
- case
- when locales.include?('C.UTF-8')
- 'C.UTF-8'
- when locales.include?('en_US.UTF-8'), locales.include?('en_US.utf8')
- 'en_US.UTF-8'
- when locales.include?('en.UTF-8')
- 'en.UTF-8'
- else
- # Will match en_ZZ.UTF-8, en_ZZ.utf-8, en_ZZ.UTF8, en_ZZ.utf8
- guesses = locales.select { |l| l =~ /^en_.*UTF-?8$/i }
- unless guesses.empty?
- guessed_locale = guesses.first
- # Transform into the form en_ZZ.UTF-8
- guessed_locale.gsub(/UTF-?8$/i, "UTF-8")
- else
- Chef::Log.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support."
- 'C'
- end
- end
- rescue
- if Chef::Platform.windows?
- Chef::Log.debug "Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else."
+ def self.guess_internal_locale
+ # https://github.com/opscode/chef/issues/2181
+ # Some systems have the `locale -a` command, but the result has
+ # invalid characters for the default encoding.
+ #
+ # For example, on CentOS 6 with ENV['LANG'] = "en_US.UTF-8",
+ # `locale -a`.split fails with ArgumentError invalid UTF-8 encoding.
+ locales = shell_out_with_systems_locale("locale -a").stdout.split
+ case
+ when locales.include?('C.UTF-8')
+ 'C.UTF-8'
+ when locales.include?('en_US.UTF-8'), locales.include?('en_US.utf8')
+ 'en_US.UTF-8'
+ when locales.include?('en.UTF-8')
+ 'en.UTF-8'
+ else
+ # Will match en_ZZ.UTF-8, en_ZZ.utf-8, en_ZZ.UTF8, en_ZZ.utf8
+ guesses = locales.select { |l| l =~ /^en_.*UTF-?8$/i }
+ unless guesses.empty?
+ guessed_locale = guesses.first
+ # Transform into the form en_ZZ.UTF-8
+ guessed_locale.gsub(/UTF-?8$/i, "UTF-8")
else
- Chef::Log.debug "No usable locale -a command found, assuming you have en_US.UTF-8 installed."
+ Chef::Log.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support."
+ 'C'
end
- 'en_US.UTF-8'
end
+ rescue
+ if Chef::Platform.windows?
+ Chef::Log.debug "Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else."
+ else
+ Chef::Log.debug "No usable locale -a command found, assuming you have en_US.UTF-8 installed."
+ end
+ 'en_US.UTF-8'
end
+ default :internal_locale, guess_internal_locale
+
# Force UTF-8 Encoding, for when we fire up in the 'C' locale or other strange locales (e.g.
# japanese windows encodings). If we do not do this, then knife upload will fail when a cookbook's
# README.md has UTF-8 characters that do not encode in whatever surrounding encoding we have been
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb
index fb782da2c7..58fb229c96 100644
--- a/spec/unit/config_spec.rb
+++ b/spec/unit/config_spec.rb
@@ -434,7 +434,7 @@ describe Chef::Config do
expect(Chef::Log).to_not receive(:warn).with(/Please install an English UTF-8 locale for Chef to use/)
expect(Chef::Log).to_not receive(:debug).with(/Defaulting to locale en_US.UTF-8 on Windows/)
expect(Chef::Log).to_not receive(:debug).with(/No usable locale -a command found/)
- expect(Chef::Config[:internal_locale]).to eq expected_locale
+ expect(Chef::Config.guess_internal_locale).to eq expected_locale
end
end
@@ -485,7 +485,7 @@ describe Chef::Config do
it "should fall back to C locale" do
expect(Chef::Log).to receive(:warn).with("Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.")
- expect(Chef::Config[:internal_locale]).to eq 'C'
+ expect(Chef::Config.guess_internal_locale).to eq 'C'
end
end
@@ -502,7 +502,7 @@ describe Chef::Config do
else
expect(Chef::Log).to receive(:debug).with("No usable locale -a command found, assuming you have en_US.UTF-8 installed.")
end
- expect(Chef::Config[:internal_locale]).to eq "en_US.UTF-8"
+ expect(Chef::Config.guess_internal_locale).to eq "en_US.UTF-8"
end
end
end