diff options
author | Nimesh-Msys <nimesh.patni@msystechnologies.com> | 2019-03-26 22:49:39 +0530 |
---|---|---|
committer | Nimesh-Msys <nimesh.patni@msystechnologies.com> | 2019-04-04 22:11:16 +0530 |
commit | 9b19748867fcb05fab550262449a1bd825db15cf (patch) | |
tree | b192ca1f69f754929a5409c856e8d3e82348d14b /spec | |
parent | 3c264946e8dd051e56418751f093eb3afa3c1387 (diff) | |
download | chef-9b19748867fcb05fab550262449a1bd825db15cf.tar.gz |
locale resource: Add support to set all LC ENV variables
- Deprecated `lc_all`
- Provided other features like generate locale if it is not available
- Removed system dependancy
- Added unit and functional test cases
- Added YARD format comments
- Ensured Chefstyle
- Fixes: MSYS-988
Signed-off-by: Nimesh-Msys <nimesh.patni@msystechnologies.com>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/resource/locale_spec.rb | 78 | ||||
-rw-r--r-- | spec/spec_helper.rb | 1 | ||||
-rw-r--r-- | spec/unit/resource/locale_spec.rb | 207 |
3 files changed, 264 insertions, 22 deletions
diff --git a/spec/functional/resource/locale_spec.rb b/spec/functional/resource/locale_spec.rb new file mode 100644 index 0000000000..61ef4630bc --- /dev/null +++ b/spec/functional/resource/locale_spec.rb @@ -0,0 +1,78 @@ +# +# Author:: Nimesh Patni (<nimesh.patni@msystechnologies.com>) +# Copyright:: Copyright 2008-2018, Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Resource::Locale, :requires_root, :not_supported_on_windows do + + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:resource) { Chef::Resource::Locale.new("fakey_fakerton", run_context) } + + def sets_system_locale(*locales) + system_locales = File.readlines("/etc/locale.conf") + expect(system_locales.map(&:strip)).to eq(locales) + end + + def unsets_system_locale(*locales) + system_locales = File.readlines("/etc/locale.conf") + expect(system_locales.map(&:strip)).not_to eq(locales) + end + + describe "action: update" do + context "Sets system variable" do + it "when LC var is given" do + resource.lc_env({ "LC_MESSAGES" => "en_US" }) + resource.run_action(:update) + sets_system_locale("LC_MESSAGES=en_US") + end + it "when lang is given" do + resource.lang("en_US") + resource.run_action(:update) + sets_system_locale("LANG=en_US") + end + it "when both lang & LC vars are given" do + resource.lang("en_US") + resource.lc_env({ "LC_TIME" => "en_IN" }) + resource.run_action(:update) + sets_system_locale("LANG=en_US", "LC_TIME=en_IN") + end + end + + context "Unsets system variable" do + it "when LC var is not given" do + resource.lc_env() + resource.run_action(:update) + unsets_system_locale("LC_MESSAGES=en_US") + end + it "when lang is not given" do + resource.lang() + resource.run_action(:update) + unsets_system_locale("LANG=en_US") + end + it "when both lang & LC vars are not given" do + resource.lang() + resource.lc_env() + resource.run_action(:update) + unsets_system_locale("LANG=en_US", "LC_TIME=en_IN") + sets_system_locale("") + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d13453b778..027be2e619 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -143,6 +143,7 @@ RSpec.configure do |config| config.filter_run_excluding skip_travis: true if ENV["TRAVIS"] config.filter_run_excluding windows_only: true unless windows? + config.filter_run_excluding not_supported_on_windows: true if windows? config.filter_run_excluding not_supported_on_macos: true if mac_osx? config.filter_run_excluding macos_only: true if !mac_osx? config.filter_run_excluding not_supported_on_aix: true if aix? diff --git a/spec/unit/resource/locale_spec.rb b/spec/unit/resource/locale_spec.rb index 7e1292d211..544f6342c5 100644 --- a/spec/unit/resource/locale_spec.rb +++ b/spec/unit/resource/locale_spec.rb @@ -1,5 +1,5 @@ # -# Author:: Vincent AUBERT (<vincentaubert88@gmail.com>) +# Author:: Nimesh Patni (<nimesh.patni@msystechnologies.com>) # Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # @@ -21,40 +21,203 @@ require "spec_helper" describe Chef::Resource::Locale do let(:resource) { Chef::Resource::Locale.new("fakey_fakerton") } + let(:provider) { resource.provider_for_action(:update) } - it "has a name of locale" do - expect(resource.resource_name).to eq(:locale) + describe "default:" do + it "name would be locale" do + expect(resource.resource_name).to eq(:locale) + end + it "lang would be nil" do + expect(resource.lang).to be_nil + end + it "lc_env would be an empty hash" do + expect(resource.lc_env).to be_a(Hash) + expect(resource.lc_env).to be_empty + end + it "action would be :update" do + expect(resource.action).to eql([:update]) + end end - it "the lang property is equal to en_US.utf8" do - expect(resource.lang).to eql("en_US.utf8") - end + describe "validations:" do + let(:validation) { Chef::Exceptions::ValidationFailed } + context "lang" do + it "is non empty" do + expect { resource.lang("") }.to raise_error(validation) + end + it "does not contain any leading whitespaces" do + expect { resource.lang(" XX") }.to raise_error(validation) + end + end - it "the lc_all property is equal to en_US.utf8" do - expect(resource.lc_all).to eql("en_US.utf8") + context "lc_env" do + it "is non empty" do + expect { resource.lc_env({ "LC_TIME" => "" }) }.to raise_error(validation) + end + it "does not contain any leading whitespaces" do + expect { resource.lc_env({ "LC_TIME" => " XX" }) }.to raise_error(validation) + end + it "keys are valid and case sensitive" do + expect { resource.lc_env({ "LC_TIMES" => " XX" }) }.to raise_error(validation) + expect { resource.lc_env({ "Lc_Time" => " XX" }) }.to raise_error(validation) + expect(resource.lc_env({ "LC_TIME" => "XX" })).to eql({ "LC_TIME" => "XX" }) + end + end end - it "sets the default action as :update" do - expect(resource.action).to eql([:update]) - end + describe "#unavailable_locales" do + let(:available_locales) do + <<~LOC + C + C.UTF-8 + en_AG + en_AG.utf8 + en_US + POSIX + LOC + end + before do + dummy = Mixlib::ShellOut.new + allow_any_instance_of(Chef::Mixin::ShellOut).to receive(:shell_out!).with("locale -a").and_return(dummy) + allow(dummy).to receive(:stdout).and_return(available_locales) + end + context "when all locales are available on system" do + context "with both properties" do + it "returns an empty array" do + resource.lang("en_US") + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_AG.utf8" }) + expect(provider.unavailable_locales).to eq([]) + end + end + context "without lang" do + it "returns an empty array" do + resource.lang() + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_AG.utf8" }) + expect(provider.unavailable_locales).to eq([]) + end + end + context "without lc_env" do + it "returns an empty array" do + resource.lang("en_US") + resource.lc_env() + expect(provider.unavailable_locales).to eq([]) + end + end + context "without both" do + it "returns an empty array" do + resource.lang() + resource.lc_env() + expect(provider.unavailable_locales).to eq([]) + end + end + end - it "supports :update action" do - expect { resource.action :update }.not_to raise_error + context "when some locales are not available" do + context "with both properties" do + it "returns list" do + resource.lang("de_DE") + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_US.utf8" }) + expect(provider.unavailable_locales).to eq(["de_DE", "en_US.utf8"]) + end + end + context "without lang" do + it "returns list" do + resource.lang() + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_US.utf8" }) + expect(provider.unavailable_locales).to eq(["en_US.utf8"]) + end + end + context "without lc_env" do + it "returns list" do + resource.lang("de_DE") + resource.lc_env() + expect(provider.unavailable_locales).to eq(["de_DE"]) + end + end + context "without both" do + it "returns an empty array" do + resource.lang() + resource.lc_env() + expect(provider.unavailable_locales).to eq([]) + end + end + end end - describe "when the language is not the default one" do - let(:resource) { Chef::Resource::Locale.new("fakey_fakerton") } - before do - resource.lang("fr_FR.utf8") - resource.lc_all("fr_FR.utf8") + describe "#new_content" do + context "with both properties" do + before do + resource.lang("en_US") + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_AG.utf8" }) + end + it "returns string" do + expect(provider.new_content).to be_a(String) + expect(provider.new_content).not_to be_empty + end + it "keys will be sorted" do + expect(provider.new_content.split("\n").map { |x| x.split("=") }.collect(&:first)).to eq(%w{LANG LC_MESSAGES LC_TIME}) + end + it "ends with a new-line character" do + expect(provider.new_content[-1]).to eq("\n") + end + it "returns a valid string" do + expect(provider.new_content).to eq("LANG=en_US\nLC_MESSAGES=en_AG.utf8\nLC_TIME=en_AG.utf8\n") + end end + context "without lang" do + it "returns a valid string" do + resource.lang() + resource.lc_env({ "LC_TIME" => "en_AG.utf8", "LC_MESSAGES" => "en_AG.utf8" }) + expect(provider.new_content).to eq("LC_MESSAGES=en_AG.utf8\nLC_TIME=en_AG.utf8\n") + end + end + context "without lc_env" do + it "returns a valid string" do + resource.lang("en_US") + resource.lc_env() + expect(provider.new_content).to eq("LANG=en_US\n") + end + end + context "without both" do + it "returns string with only new-line character" do + resource.lang() + resource.lc_env() + expect(provider.new_content).to eq("\n") + end + end + end - it "the lang property is equal to fr_FR.utf8" do - expect(resource.lang).to eql("fr_FR.utf8") + describe "#up_to_date?" do + context "when file does not exists" do + it "returns false" do + allow(File).to receive(:read).and_raise(Errno::ENOENT, "No such file or directory") + expect(provider.up_to_date?).to be_falsy + end end - it "the lc_all property is equal to fr_FR.utf8" do - expect(resource.lc_all).to eql("fr_FR.utf8") + context "when file exists" do + let(:content) { "LANG=en_US\nLC_MESSAGES=en_AG.utf8\nLC_TIME=en_AG.utf8\n" } + before do + allow(provider).to receive(:new_content).and_return(content) + end + context "but is empty" do + it "returns false" do + allow(File).to receive(:read).and_return("") + expect(provider.up_to_date?).to be_falsy + end + end + context "and contains old key-vals" do + it "returns false" do + allow(File).to receive(:read).and_return("LC_MESSAGES=en_AG.utf8\n") + expect(provider.up_to_date?).to be_falsy + end + end + context "and contains new key-vals" do + it "returns true" do + allow(File).to receive(:read).and_return(content) + expect(provider.up_to_date?).to be_truthy + end + end end end end |