From be1191b72d72887713048538e72a11b3940c6eb8 Mon Sep 17 00:00:00 2001 From: Kapil Chouhan Date: Fri, 10 Apr 2020 09:07:08 +0000 Subject: Update the locale resource to support Windows Signed-off-by: Kapil Chouhan --- lib/chef/resource/locale.rb | 55 ++++++++++++++++++++++++--------- spec/functional/resource/locale_spec.rb | 14 +++++++-- spec/unit/resource/locale_spec.rb | 34 -------------------- 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/lib/chef/resource/locale.rb b/lib/chef/resource/locale.rb index 0ecc440a80..5112e7e1df 100644 --- a/lib/chef/resource/locale.rb +++ b/lib/chef/resource/locale.rb @@ -30,7 +30,7 @@ class Chef LC_VARIABLES = %w{LC_ADDRESS LC_COLLATE LC_CTYPE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME}.freeze LOCALE_CONF = "/etc/locale.conf".freeze LOCALE_REGEX = /\A\S+/.freeze - LOCALE_PLATFORM_FAMILIES = %w{debian}.freeze + LOCALE_PLATFORM_FAMILIES = %w{debian windows}.freeze property :lang, String, description: "Sets the default system language.", @@ -65,13 +65,32 @@ class Chef end end + load_current_value do + if windows? + lang get_system_locale_windows + else + begin + old_content = ::File.read(LOCALE_CONF) + locale_values = Hash[old_content.split("\n").map { |v| v.split("=") }] + lang locale_values["LANG"] + rescue Errno::ENOENT => e + false + end + end + end + + # Gets the System-locale setting for the current computer. + # @see https://docs.microsoft.com/en-us/powershell/module/international/get-winsystemlocale + # @return [String] the current value of the System-locale setting. + # + def get_system_locale_windows + powershell_exec("Get-WinSystemLocale").result["Name"] + end + action :update do description "Update the system's locale." - unless up_to_date? - converge_by "Updating System Locale" do - generate_locales unless unavailable_locales.empty? - update_locale - end + converge_if_changed do + set_system_locale end end @@ -99,6 +118,21 @@ class Chef shell_out!("locale-gen #{unavailable_locales.join(" ")}") end + # Sets the system locale for the current computer. + # + def set_system_locale + if windows? + # Sets the system locale for the current computer. + # @see https://docs.microsoft.com/en-us/powershell/module/internationalcmdlets/set-winsystemlocale + # + response = powershell_exec("Set-WinSystemLocale -SystemLocale #{new_resource.lang}") + raise response.errors.join(" ") if response.error? + else + generate_locales unless unavailable_locales.empty? + update_locale + end + end + # Updates system locale by appropriately writing them in /etc/locale.conf # @note This locale change won't affect the current run. At this time it is an exercise # left to the user to restart or reboot if the locale change is required at @@ -134,15 +168,6 @@ class Chef content.sort.map { |t| t.join("=") }.join("\n") + "\n" end end - - # @return [Boolean] Whether any modification is required in /etc/locale.conf - # - def up_to_date? - old_content = ::File.read(LOCALE_CONF) - new_content == old_content - rescue - false # We need to create the file if it is not present - end end end end diff --git a/spec/functional/resource/locale_spec.rb b/spec/functional/resource/locale_spec.rb index 29cd01ad78..24a43ef0ba 100644 --- a/spec/functional/resource/locale_spec.rb +++ b/spec/functional/resource/locale_spec.rb @@ -74,8 +74,7 @@ describe Chef::Resource::Locale, :requires_root do resource.lang resource.lc_env resource.run_action(:update) - unsets_system_locale("LANG=en_US", "LC_TIME=en_IN") - sets_system_locale("") + expect(resource).not_to be_updated_by_last_action end end end @@ -94,4 +93,15 @@ describe Chef::Resource::Locale, :requires_root do expect { resource.run_action(:update) }.to raise_error(Chef::Exceptions::ProviderNotFound) end end + + context "on windows", :windows_only, requires_root: false do + describe "action: update" do + context "Sets system locale" do + it "when lang is given" do + resource.lang("en-US") + resource.run_action(:update) + end + end + end + end end diff --git a/spec/unit/resource/locale_spec.rb b/spec/unit/resource/locale_spec.rb index 7f70905ff0..e1275a6830 100644 --- a/spec/unit/resource/locale_spec.rb +++ b/spec/unit/resource/locale_spec.rb @@ -186,38 +186,4 @@ describe Chef::Resource::Locale do end end end - - 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 - - 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 -- cgit v1.2.1