From 3a379b9f66a9943c221b0db84a2e7d3183b94db0 Mon Sep 17 00:00:00 2001 From: Pavel Skrylev Date: Tue, 16 Sep 2025 17:06:08 +0300 Subject: [PATCH] feat: allow support for ruby system locale and spec.default paths + ruby system locale path used in find along with posix default path + default path mostly used in alt linux tree * test specs --- lib/puppet/gettext/config.rb | 7 ++++--- lib/puppet/util/run_mode.rb | 23 +++++++++++++---------- spec/unit/defaults_spec.rb | 6 +++++- spec/unit/util/run_mode_spec.rb | 26 ++++++++++++++++++++++---- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/lib/puppet/gettext/config.rb b/lib/puppet/gettext/config.rb index e9a9bcfb508..95edce96414 100644 --- a/lib/puppet/gettext/config.rb +++ b/lib/puppet/gettext/config.rb @@ -6,6 +6,7 @@ module Puppet::GettextConfig LOCAL_PATH = File.absolute_path('../../../locales', File.dirname(__FILE__)) POSIX_PATH = File.absolute_path('../../../../../share/locale', File.dirname(__FILE__)) + RUBY_CONFIG_PATH = RbConfig::CONFIG["localedir"] WINDOWS_PATH = File.absolute_path('../../../../../../puppet/share/locale', File.dirname(__FILE__)) # This is the only domain name that won't be a symbol, making it unique from environments. @@ -194,8 +195,8 @@ def self.puppet_locale_path LOCAL_PATH elsif Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(WINDOWS_PATH) WINDOWS_PATH - elsif !Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(POSIX_PATH) - POSIX_PATH + elsif !Puppet::Util::Platform.windows? + [POSIX_PATH, RUBY_CONFIG_PATH].find {|path| Puppet::FileSystem.exist?(path) } else nil end @@ -206,7 +207,7 @@ def self.puppet_locale_path # @param [String] conf_path the path to the gettext config file # @return [Symbol] :mo if in a package structure, :po otherwise def self.translation_mode(conf_path) - if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path + if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path || RUBY_CONFIG_PATH == conf_path :mo else :po diff --git a/lib/puppet/util/run_mode.rb b/lib/puppet/util/run_mode.rb index a64198150f4..83033697f1b 100644 --- a/lib/puppet/util/run_mode.rb +++ b/lib/puppet/util/run_mode.rb @@ -46,6 +46,9 @@ def log_dir private + def find_or_first(*dirs) + dirs.find {|d| File.directory?(d) } || dirs.first + end ## # select the system or the user directory depending on the context of # this process. The most common use is determining filesystem path @@ -65,43 +68,43 @@ def which_dir(system, user) class UnixRunMode < RunMode def conf_dir - which_dir("/etc/puppetlabs/puppet", "~/.puppetlabs/etc/puppet") + which_dir(find_or_first("/etc/puppetlabs/puppet", "/etc/puppet"), "~/.puppetlabs/etc/puppet") end def code_dir - which_dir("/etc/puppetlabs/code", "~/.puppetlabs/etc/code") + which_dir(find_or_first("/etc/puppetlabs/code", "/etc/puppet/code"), "~/.puppetlabs/etc/code") end def var_dir - which_dir("/opt/puppetlabs/puppet/cache", "~/.puppetlabs/opt/puppet/cache") + which_dir(find_or_first("/opt/puppetlabs/puppet/cache", "/var/cache/puppet"), "~/.puppetlabs/opt/puppet/cache") end def public_dir - which_dir("/opt/puppetlabs/puppet/public", "~/.puppetlabs/opt/puppet/public") + which_dir(find_or_first("/opt/puppetlabs/puppet/public", "/usr/share/puppet/public"), "~/.puppetlabs/opt/puppet/public") end def run_dir - which_dir("/var/run/puppetlabs", "~/.puppetlabs/var/run") + which_dir(find_or_first("/var/run/puppetlabs", "/run/puppet"), "~/.puppetlabs/var/run") end def log_dir - which_dir("/var/log/puppetlabs/puppet", "~/.puppetlabs/var/log") + which_dir(find_or_first("/var/log/puppetlabs/puppet", "/var/log/puppet"), "~/.puppetlabs/var/log") end def pkg_config_path - '/opt/puppetlabs/puppet/lib/pkgconfig' + find_or_first('/opt/puppetlabs/puppet/lib/pkgconfig', "/usr/share/pkgconfig", "/usr/lib64/pkgconfig", "/usr/lib/pkgconfig") end def gem_cmd - '/opt/puppetlabs/puppet/bin/gem' + find_or_first('/opt/puppetlabs/puppet/bin/gem', "/usr/bin/gem") end def common_module_dir - '/opt/puppetlabs/puppet/modules' + find_or_first('/opt/puppetlabs/puppet/modules', "/usr/local/lib/puppet-modules") end def vendor_module_dir - '/opt/puppetlabs/puppet/vendor_modules' + find_or_first('/opt/puppetlabs/puppet/vendor_modules', "/usr/lib/puppet-modules") end end diff --git a/spec/unit/defaults_spec.rb b/spec/unit/defaults_spec.rb index 9f322b38ec9..1166152f089 100644 --- a/spec/unit/defaults_spec.rb +++ b/spec/unit/defaults_spec.rb @@ -120,8 +120,12 @@ describe 'vendormoduledir' do it 'includes the default vendormoduledir', :unless => Puppet::Util::Platform.windows? do + @run_mode = Puppet::Util::UnixRunMode.new('fake') + allow(Puppet).to receive(:run_mode) { @run_mode } + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/vendor_modules') + expect( - Puppet[:vendormoduledir] + Puppet.default_vendormoduledir ).to eq('/opt/puppetlabs/puppet/vendor_modules') end diff --git a/spec/unit/util/run_mode_spec.rb b/spec/unit/util/run_mode_spec.rb index cd7d3f72d6a..3530146b0f3 100644 --- a/spec/unit/util/run_mode_spec.rb +++ b/spec/unit/util/run_mode_spec.rb @@ -12,6 +12,7 @@ describe "#conf_dir" do it "has confdir /etc/puppetlabs/puppet when run as root" do + allow(@run_mode).to receive(:find_or_first).and_return('/etc/puppetlabs/puppet') as_root { expect(@run_mode.conf_dir).to eq(File.expand_path('/etc/puppetlabs/puppet')) } end @@ -32,6 +33,7 @@ describe "#code_dir" do it "has codedir /etc/puppetlabs/code when run as root" do + allow(@run_mode).to receive(:find_or_first).and_return('/etc/puppetlabs/code') as_root { expect(@run_mode.code_dir).to eq(File.expand_path('/etc/puppetlabs/code')) } end @@ -52,6 +54,7 @@ describe "#var_dir" do it "has vardir /opt/puppetlabs/puppet/cache when run as root" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/cache') as_root { expect(@run_mode.var_dir).to eq(File.expand_path('/opt/puppetlabs/puppet/cache')) } end @@ -62,6 +65,7 @@ describe "#public_dir" do it "has publicdir /opt/puppetlabs/puppet/public when run as root" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/public') as_root { expect(@run_mode.public_dir).to eq(File.expand_path('/opt/puppetlabs/puppet/public')) } end @@ -73,6 +77,7 @@ describe "#log_dir" do describe "when run as root" do it "has logdir /var/log/puppetlabs/puppet" do + allow(@run_mode).to receive(:find_or_first).and_return('/var/log/puppetlabs/puppet') as_root { expect(@run_mode.log_dir).to eq(File.expand_path('/var/log/puppetlabs/puppet')) } end end @@ -87,6 +92,7 @@ describe "#run_dir" do describe "when run as root" do it "has rundir /var/run/puppetlabs" do + allow(@run_mode).to receive(:find_or_first).and_return('/var/run/puppetlabs') as_root { expect(@run_mode.run_dir).to eq(File.expand_path('/var/run/puppetlabs')) } end end @@ -99,19 +105,31 @@ end describe "#pkg_config_path" do - it { expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig') } + it "has pkg config path /opt/puppetlabs/puppet/lib/pkgconfig" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/lib/pkgconfig') + expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig') + end end describe "#gem_cmd" do - it { expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem') } + it "has gem cmd /opt/puppetlabs/puppet/bin/gem" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/bin/gem') + expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem') + end end describe "#common_module_dir" do - it { expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules') } + it "has common module dir /opt/puppetlabs/puppet/modules" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/modules') + expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules') + end end describe "#vendor_module_dir" do - it { expect(@run_mode.vendor_module_dir).to eq('/opt/puppetlabs/puppet/vendor_modules') } + it "has vendor module dir /opt/puppetlabs/puppet/vendor_modules" do + allow(@run_mode).to receive(:find_or_first).and_return('/opt/puppetlabs/puppet/vendor_modules') + expect(@run_mode.vendor_module_dir).to eq('/opt/puppetlabs/puppet/vendor_modules') + end end end