From d18bc75250c2f587ed7cfe02402cd5d0e6cc88a2 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Fri, 26 Jan 2024 12:41:02 -0800 Subject: [PATCH 1/3] (PUP-11767) Changes for rubocop Style/AutoResourceCleanup Prefer usage of `File.new` to `File.open` to differentiate when the file object is expected to persist and stay open and to enable AutoResourceCleanup entirely. --- .rubocop.yml | 4 ++++ lib/puppet/util/at_fork/solaris.rb | 6 +++--- lib/puppet/util/log/destinations.rb | 2 +- lib/puppet/util/selinux.rb | 2 +- spec/unit/util/selinux_spec.rb | 8 ++++---- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 71540019f6c..88b9bd82956 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -199,3 +199,7 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7 RSpec/Rails/AvoidSetupHook: # new in 2.4 Enabled: true + +Style/AutoResourceCleanup: + Enabled: true + diff --git a/lib/puppet/util/at_fork/solaris.rb b/lib/puppet/util/at_fork/solaris.rb index c7c24152b0e..5930695dc4f 100644 --- a/lib/puppet/util/at_fork/solaris.rb +++ b/lib/puppet/util/at_fork/solaris.rb @@ -64,7 +64,7 @@ def raise_if_error(&block) def activate_new_contract_template begin - tmpl = File.open(CTFS_PR_TEMPLATE, File::RDWR) + tmpl = File.new(CTFS_PR_TEMPLATE, File::RDWR) begin tmpl_fd = tmpl.fileno @@ -109,7 +109,7 @@ def deactivate_contract_template(parent) def get_latest_child_contract_id begin - stat = File.open(CTFS_PR_LATEST, File::RDONLY) + stat = File.new(CTFS_PR_LATEST, File::RDONLY) begin stathdl = Fiddle::Pointer.new(0) @@ -133,7 +133,7 @@ def abandon_latest_child_contract return if ctid.nil? begin - ctl = File.open(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY) + ctl = File.new(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY) begin raise_if_error { ct_ctl_abandon(ctl.fileno) } diff --git a/lib/puppet/util/log/destinations.rb b/lib/puppet/util/log/destinations.rb index 9ace2cd9f71..67dd4de93cf 100644 --- a/lib/puppet/util/log/destinations.rb +++ b/lib/puppet/util/log/destinations.rb @@ -91,7 +91,7 @@ def initialize(path) end end - file = File.open(path, File::WRONLY | File::CREAT | File::APPEND) + file = File.new(path, File::WRONLY | File::CREAT | File::APPEND) file.puts('[') if need_array_start @file = file diff --git a/lib/puppet/util/selinux.rb b/lib/puppet/util/selinux.rb index 75a0701f0ba..074be48e8bd 100644 --- a/lib/puppet/util/selinux.rb +++ b/lib/puppet/util/selinux.rb @@ -242,7 +242,7 @@ def read_mounts if File.method_defined? "read_nonblock" # If possible we use read_nonblock in a loop rather than read to work- # a linux kernel bug. See ticket #1963 for details. - mountfh = File.open("/proc/mounts") + mountfh = File.new("/proc/mounts") loop do mounts += mountfh.read_nonblock(1024) end diff --git a/spec/unit/util/selinux_spec.rb b/spec/unit/util/selinux_spec.rb index ab6547e1d5d..21c1fea4801 100644 --- a/spec/unit/util/selinux_spec.rb +++ b/spec/unit/util/selinux_spec.rb @@ -29,7 +29,7 @@ end it "should return nil if /proc/mounts does not exist" do - allow(File).to receive(:open).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts") + allow(File).to receive(:new).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts") expect(read_mounts).to eq(nil) end end @@ -37,8 +37,8 @@ describe "read_mounts" do before :each do fh = double('fh', :close => nil) - allow(File).to receive(:open).and_call_original() - allow(File).to receive(:open).with("/proc/mounts").and_return(fh) + allow(File).to receive(:new).and_call_original() + allow(File).to receive(:new).with("/proc/mounts").and_return(fh) times_fh_called = 0 expect(fh).to receive(:read_nonblock) do times_fh_called += 1 @@ -304,7 +304,7 @@ describe "set_selinux_context" do before :each do fh = double('fh', :close => nil) - allow(File).to receive(:open).with("/proc/mounts").and_return(fh) + allow(File).to receive(:new).with("/proc/mounts").and_return(fh) times_fh_called = 0 allow(fh).to receive(:read_nonblock) do times_fh_called += 1 From 676c2fc41cce8ad3799e35415d7a85b46f093083 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Fri, 26 Jan 2024 13:43:08 -0800 Subject: [PATCH 2/3] (PUP-11767) Add Style/CaseEquality Cop --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 88b9bd82956..466ccd5ed26 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -203,3 +203,5 @@ RSpec/Rails/AvoidSetupHook: # new in 2.4 Style/AutoResourceCleanup: Enabled: true +Style/CaseEquality: + Enabled: true From 22bf7abd332a0fa92a650c360ab87b99333631d7 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Fri, 26 Jan 2024 13:43:56 -0800 Subject: [PATCH 3/3] (PUP-11767) Add Style/CaseLikeIf Cop --- .rubocop.yml | 3 +++ lib/hiera/scope.rb | 7 +++--- lib/puppet/etc.rb | 5 ++-- lib/puppet/face/epp.rb | 5 ++-- lib/puppet/face/module/install.rb | 5 ++-- lib/puppet/face/module/upgrade.rb | 5 ++-- lib/puppet/functions/abs.rb | 5 ++-- lib/puppet/indirector/catalog/compiler.rb | 5 ++-- lib/puppet/network/formats.rb | 10 +++++--- lib/puppet/node/facts.rb | 10 +++----- lib/puppet/parser/scope.rb | 7 +++--- lib/puppet/pops/evaluator/compare_operator.rb | 10 +++++--- .../pops/evaluator/deferred_resolver.rb | 14 ++++++----- lib/puppet/pops/evaluator/evaluator_impl.rb | 5 ++-- .../pops/evaluator/runtime3_converter.rb | 7 +++--- lib/puppet/pops/lookup/hiera_config.rb | 5 ++-- lib/puppet/pops/merge_strategy.rb | 5 ++-- lib/puppet/pops/parser/lexer2.rb | 5 ++-- lib/puppet/pops/parser/lexer_support.rb | 7 +++--- .../pops/serialization/from_data_converter.rb | 15 ++++++----- lib/puppet/pops/types/p_object_type.rb | 5 ++-- lib/puppet/pops/types/p_uri_type.rb | 7 +++--- lib/puppet/pops/types/string_converter.rb | 5 ++-- lib/puppet/pops/types/type_calculator.rb | 11 ++++---- lib/puppet/pops/types/type_factory.rb | 7 +++--- .../pops/types/type_mismatch_describer.rb | 25 +++++++++++-------- lib/puppet/pops/types/type_parser.rb | 10 +++++--- lib/puppet/pops/types/types.rb | 20 +++++++++------ .../provider/nameservice/directoryservice.rb | 5 ++-- lib/puppet/provider/package/yum.rb | 5 ++-- lib/puppet/reference/configuration.rb | 19 +++++++------- lib/puppet/resource.rb | 5 ++-- lib/puppet/resource/status.rb | 5 ++-- lib/puppet/settings.rb | 10 +++++--- .../util/command_line/puppet_option_parser.rb | 5 ++-- lib/puppet/util/execution.rb | 5 ++-- lib/puppet/util/plist.rb | 7 +++--- lib/puppet/util/rdoc/code_objects.rb | 5 ++-- .../util/rdoc/parser/puppet_parser_core.rb | 16 ++++++------ lib/puppet/util/symbolic_file_mode.rb | 5 ++-- 40 files changed, 188 insertions(+), 134 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 466ccd5ed26..c196c11d094 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -205,3 +205,6 @@ Style/AutoResourceCleanup: Style/CaseEquality: Enabled: true + +Style/CaseLikeIf: + Enabled: true diff --git a/lib/hiera/scope.rb b/lib/hiera/scope.rb index d754ec971cc..2d419c0bca8 100644 --- a/lib/hiera/scope.rb +++ b/lib/hiera/scope.rb @@ -20,11 +20,12 @@ def initialize(real) end def [](key) - if key == CALLING_CLASS + case key + when CALLING_CLASS ans = find_hostclass(@real) - elsif key == CALLING_CLASS_PATH + when CALLING_CLASS_PATH ans = find_hostclass(@real).gsub(/::/, '/') - elsif key == CALLING_MODULE + when CALLING_MODULE ans = safe_lookupvar(MODULE_NAME) else ans = safe_lookupvar(key) diff --git a/lib/puppet/etc.rb b/lib/puppet/etc.rb index 48c5d8c9eb9..de2d30a7878 100644 --- a/lib/puppet/etc.rb +++ b/lib/puppet/etc.rb @@ -165,10 +165,11 @@ def override_field_values_to_utf8(struct) new_struct = struct.is_a?(Etc::Passwd) ? puppet_etc_passwd_class.new : puppet_etc_group_class.new struct.each_pair do |member, value| - if value.is_a?(String) + case value + when String new_struct["canonical_#{member}".to_sym] = value.dup new_struct[member] = Puppet::Util::CharacterEncoding.override_encoding_to_utf_8(value).scrub - elsif value.is_a?(Array) + when Array new_struct["canonical_#{member}".to_sym] = value.inject([]) { |acc, elem| acc << elem.dup } new_struct[member] = value.inject([]) do |acc, elem| acc << Puppet::Util::CharacterEncoding.override_encoding_to_utf_8(elem).scrub diff --git a/lib/puppet/face/epp.rb b/lib/puppet/face/epp.rb index 853465477f5..72c45a2ea7c 100644 --- a/lib/puppet/face/epp.rb +++ b/lib/puppet/face/epp.rb @@ -406,9 +406,10 @@ def get_values(compiler, options) values_file = options[:values_file] if values_file begin - if values_file =~ /\.yaml$/ + case values_file + when /\.yaml$/ template_values = Puppet::Util::Yaml.safe_load_file(values_file, [Symbol]) - elsif values_file =~ /\.pp$/ + when /\.pp$/ evaluating_parser = Puppet::Pops::Parser::EvaluatingParser.new template_values = evaluating_parser.evaluate_file(compiler.topscope, values_file) else diff --git a/lib/puppet/face/module/install.rb b/lib/puppet/face/module/install.rb index 4d08edad0b9..b0cdb429363 100644 --- a/lib/puppet/face/module/install.rb +++ b/lib/puppet/face/module/install.rb @@ -131,10 +131,11 @@ end when_rendering :console do |return_value, name, _options| - if return_value[:result] == :noop + case return_value[:result] + when :noop Puppet.notice _("Module %{name} %{version} is already installed.") % { name: name, version: return_value[:version] } exit 0 - elsif return_value[:result] == :failure + when :failure Puppet.err(return_value[:error][:multiline]) exit 1 else diff --git a/lib/puppet/face/module/upgrade.rb b/lib/puppet/face/module/upgrade.rb index a85024cd0f0..d99c5ad10d0 100644 --- a/lib/puppet/face/module/upgrade.rb +++ b/lib/puppet/face/module/upgrade.rb @@ -71,10 +71,11 @@ end when_rendering :console do |return_value| - if return_value[:result] == :noop + case return_value[:result] + when :noop Puppet.notice return_value[:error][:multiline] exit 0 - elsif return_value[:result] == :failure + when :failure Puppet.err(return_value[:error][:multiline]) exit 1 else diff --git a/lib/puppet/functions/abs.rb b/lib/puppet/functions/abs.rb index 7d5eff5aaca..9751f876120 100644 --- a/lib/puppet/functions/abs.rb +++ b/lib/puppet/functions/abs.rb @@ -52,9 +52,10 @@ def on_string(x) # These patterns for conversion are backwards compatible with the stdlib # version of this function. # - if x =~ %r{^-?(?:\d+)(?:\.\d+){1}$} + case x + when %r{^-?(?:\d+)(?:\.\d+){1}$} x.to_f.abs - elsif x =~ %r{^-?\d+$} + when %r{^-?\d+$} x.to_i.abs else raise(ArgumentError, 'abs(): Requires float or integer to work with - was given non decimal string') diff --git a/lib/puppet/indirector/catalog/compiler.rb b/lib/puppet/indirector/catalog/compiler.rb index a9a4b2a9020..584b0850bfe 100644 --- a/lib/puppet/indirector/catalog/compiler.rb +++ b/lib/puppet/indirector/catalog/compiler.rb @@ -116,11 +116,12 @@ def require_environment? # @return [Puppet::Node::Facts] facts object deserialized from supplied string # @api private def convert_wire_facts(facts, format) - if format == 'pson' + case format + when 'pson' # We unescape here because the corresponding code in Puppet::Configurer::FactHandler encodes with Puppet::Util.uri_query_encode # PSON is deprecated, but continue to accept from older agents return Puppet::Node::Facts.convert_from('pson', CGI.unescape(facts)) - elsif format == 'application/json' + when 'application/json' return Puppet::Node::Facts.convert_from('json', CGI.unescape(facts)) else raise ArgumentError, _("Unsupported facts format") diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 55dbddd5c22..fc67e7d27d7 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -193,11 +193,12 @@ def render_multiple(data) :weight => 0) do def flatten_hash(hash) hash.each_with_object({}) do |(k, v), h| - if v.is_a? Hash + case v + when Hash flatten_hash(v).map do |h_k, h_v| h["#{k}.#{h_k}"] = h_v end - elsif v.is_a? Array + when Array v.each_with_index do |el, i| if el.is_a? Hash flatten_hash(el).map do |el_k, el_v| @@ -240,10 +241,11 @@ def render(datum) return datum if datum.is_a?(String) || datum.is_a?(Numeric) # Simple hash - if datum.is_a?(Hash) + case datum + when Hash data = flatten_hash(datum) return construct_output(data) - elsif datum.is_a?(Array) + when Array data = flatten_array(datum) return construct_output(data) end diff --git a/lib/puppet/node/facts.rb b/lib/puppet/node/facts.rb index e7f25a44617..fc0b572b90d 100644 --- a/lib/puppet/node/facts.rb +++ b/lib/puppet/node/facts.rb @@ -138,16 +138,14 @@ def quote_special_strings(fact_hash) end def sanitize_fact(fact) - if fact.is_a? Hash then + case fact + when Hash ret = {} fact.each_pair { |k, v| ret[sanitize_fact k] = sanitize_fact v } ret - elsif fact.is_a? Array then + when Array fact.collect { |i| sanitize_fact i } - elsif fact.is_a? Numeric \ - or fact.is_a? TrueClass \ - or fact.is_a? FalseClass \ - or fact.is_a? String + when Numeric, TrueClass, FalseClass, String fact else result = fact.to_s diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index 6fd5223744a..a67dd237795 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -743,11 +743,12 @@ def merge_settings(env_name, set_in_this_scope = true) end def transform_setting(val) - if val.is_a?(String) || val.is_a?(Numeric) || true == val || false == val || nil == val + case val + when String, Numeric, true, false, nil val - elsif val.is_a?(Array) + when Array val.map { |entry| transform_setting(entry) } - elsif val.is_a?(Hash) + when Hash result = {} val.each { |k, v| result[transform_setting(k)] = transform_setting(v) } result diff --git a/lib/puppet/pops/evaluator/compare_operator.rb b/lib/puppet/pops/evaluator/compare_operator.rb index 4015d990fe9..a43c58c6834 100644 --- a/lib/puppet/pops/evaluator/compare_operator.rb +++ b/lib/puppet/pops/evaluator/compare_operator.rb @@ -61,9 +61,10 @@ def equals_String(a, b) end def cmp_Numeric(a, b) - if b.is_a?(Numeric) + case b + when Numeric a <=> b - elsif b.is_a?(Time::Timespan) || b.is_a?(Time::Timestamp) + when Time::Timespan, Time::Timestamp -(b <=> a) # compare other way and invert result else raise ArgumentError.new(_("A Numeric is not comparable to non Numeric")) @@ -218,9 +219,10 @@ def match_Regexp(regexp, left, scope) # Matches against semvers and strings def match_Version(version, left, scope) - if left.is_a?(SemanticPuppet::Version) + case left + when SemanticPuppet::Version version == left - elsif left.is_a? String + when String begin version == SemanticPuppet::Version.parse(left) rescue ArgumentError diff --git a/lib/puppet/pops/evaluator/deferred_resolver.rb b/lib/puppet/pops/evaluator/deferred_resolver.rb index 2322fbbe7c7..5cb8d2c7707 100644 --- a/lib/puppet/pops/evaluator/deferred_resolver.rb +++ b/lib/puppet/pops/evaluator/deferred_resolver.rb @@ -92,12 +92,13 @@ def resolve_futures(catalog) # If the value is instance of Sensitive - assign the unwrapped value # and mark it as sensitive if not already marked # - if resolved.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive) + case resolved + when Puppet::Pops::Types::PSensitiveType::Sensitive resolved = resolved.unwrap mark_sensitive_parameters(r, k) # If the value is a DeferredValue and it has an argument of type PSensitiveType, mark it as sensitive # The DeferredValue.resolve method will unwrap it during catalog application - elsif resolved.is_a?(Puppet::Pops::Evaluator::DeferredValue) + when Puppet::Pops::Evaluator::DeferredValue if v.arguments.any? { |arg| arg.is_a?(Puppet::Pops::Types::PSensitiveType) } mark_sensitive_parameters(r, k) end @@ -136,15 +137,16 @@ def resolve(x) end def resolve_lazy_args(x) - if x.is_a?(DeferredValue) + case x + when DeferredValue x.resolve - elsif x.is_a?(Array) + when Array x.map { |v| resolve_lazy_args(v) } - elsif x.is_a?(Hash) + when Hash result = {} x.each_pair { |k, v| result[k] = resolve_lazy_args(v) } result - elsif x.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive) + when Puppet::Pops::Types::PSensitiveType::Sensitive # rewrap in a new Sensitive after resolving any nested deferred values Puppet::Pops::Types::PSensitiveType::Sensitive.new(resolve_lazy_args(x.unwrap)) else diff --git a/lib/puppet/pops/evaluator/evaluator_impl.rb b/lib/puppet/pops/evaluator/evaluator_impl.rb index 318f57a4d47..a0063e97ce3 100644 --- a/lib/puppet/pops/evaluator/evaluator_impl.rb +++ b/lib/puppet/pops/evaluator/evaluator_impl.rb @@ -225,13 +225,14 @@ def assign_Object(name, value, o, scope) end def assign_Array(lvalues, values, o, scope) - if values.is_a?(Hash) + case values + when Hash lvalues.map do |lval| assign(lval, values.fetch(lval) { |k| fail(Issues::MISSING_MULTI_ASSIGNMENT_KEY, o, :key => k) }, o, scope) end - elsif values.is_a?(Puppet::Pops::Types::PClassType) + when Puppet::Pops::Types::PClassType if Puppet[:tasks] fail(Issues::CATALOG_OPERATION_NOT_SUPPORTED_WHEN_SCRIPTING, o, { :operation => _('multi var assignment from class') }) end diff --git a/lib/puppet/pops/evaluator/runtime3_converter.rb b/lib/puppet/pops/evaluator/runtime3_converter.rb index f536ef95def..745a91cb4a5 100644 --- a/lib/puppet/pops/evaluator/runtime3_converter.rb +++ b/lib/puppet/pops/evaluator/runtime3_converter.rb @@ -206,11 +206,12 @@ def convert_Timestamp(o, scope, undef_value) # Converts result back to 4.x by replacing :undef with nil in Array and Hash objects # def self.convert_return(val3x) - if val3x == :undef + case val3x + when :undef nil - elsif val3x.is_a?(Array) + when Array val3x.map { |v| convert_return(v) } - elsif val3x.is_a?(Hash) + when Hash hsh = {} val3x.each_pair { |k, v| hsh[convert_return(k)] = convert_return(v) } hsh diff --git a/lib/puppet/pops/lookup/hiera_config.rb b/lib/puppet/pops/lookup/hiera_config.rb index 4bf22927c8a..957e8f29af7 100644 --- a/lib/puppet/pops/lookup/hiera_config.rb +++ b/lib/puppet/pops/lookup/hiera_config.rb @@ -232,13 +232,14 @@ def find_line_matching(regexp, start_line = 1) quote = nil stripped = ''.dup line.each_codepoint do |cp| - if cp == 0x22 || cp == 0x27 # double or single quote + case cp + when 0x22, 0x27 # double or single quote if quote == cp quote = nil elsif quote.nil? quote = cp end - elsif cp == 0x23 # unquoted hash mark + when 0x23 # unquoted hash mark break end stripped << cp diff --git a/lib/puppet/pops/merge_strategy.rb b/lib/puppet/pops/merge_strategy.rb index 0d18a5c2ef7..1d992e02500 100644 --- a/lib/puppet/pops/merge_strategy.rb +++ b/lib/puppet/pops/merge_strategy.rb @@ -377,11 +377,12 @@ def checked_merge(e1, e2) end def deep_clone(value) - if value.is_a?(Hash) + case value + when Hash result = value.clone value.each { |k, v| result[k] = deep_clone(v) } result - elsif value.is_a?(Array) + when Array value.map { |v| deep_clone(v) } else value diff --git a/lib/puppet/pops/parser/lexer2.rb b/lib/puppet/pops/parser/lexer2.rb index 5b2bbad957a..b69ed6102a8 100644 --- a/lib/puppet/pops/parser/lexer2.rb +++ b/lib/puppet/pops/parser/lexer2.rb @@ -254,9 +254,10 @@ def initialize() '@' => lambda do scn = @scanner la = scn.peek(2) - if la[1] == '@' + case la[1] + when '@' emit(TOKEN_ATAT, scn.pos) # TODO; Check if this is good for the grammar - elsif la[1] == '(' + when '(' heredoc else emit(TOKEN_AT, scn.pos) diff --git a/lib/puppet/pops/parser/lexer_support.rb b/lib/puppet/pops/parser/lexer_support.rb index b7a003c5d6d..2e9464ec271 100644 --- a/lib/puppet/pops/parser/lexer_support.rb +++ b/lib/puppet/pops/parser/lexer_support.rb @@ -82,13 +82,14 @@ def create_lex_error(issue, args = {}, pos = nil) # An error is raised if the given value does not comply. # def assert_numeric(value, pos) - if value =~ /^0[xX]/ + case value + when /^0[xX]/ lex_error(Issues::INVALID_HEX_NUMBER, { :value => value }, pos) unless value =~ /^0[xX][0-9A-Fa-f]+$/ - elsif value =~ /^0[^.]/ + when /^0[^.]/ lex_error(Issues::INVALID_OCTAL_NUMBER, { :value => value }, pos) unless value =~ /^0[0-7]+$/ - elsif value =~ /^\d+[eE.]/ + when /^\d+[eE.]/ lex_error(Issues::INVALID_DECIMAL_NUMBER, { :value => value }, pos) unless value =~ /^\d+(?:\.\d+)?(?:[eE]-?\d+)?$/ else diff --git a/lib/puppet/pops/serialization/from_data_converter.rb b/lib/puppet/pops/serialization/from_data_converter.rb index 06106bf4c58..ce79ba2561a 100644 --- a/lib/puppet/pops/serialization/from_data_converter.rb +++ b/lib/puppet/pops/serialization/from_data_converter.rb @@ -20,9 +20,10 @@ def []=(key, value) def resolve unless @resolved @resolved = true - if @values.is_a?(Array) + case @values + when Array @values.each_with_index { |v, idx| @values[idx] = v.resolve if v.is_a?(Builder) } - elsif @values.is_a?(Hash) + when Hash @values.each_pair { |k, v| @values[k] = v.resolve if v.is_a?(Builder) } end end @@ -153,14 +154,15 @@ def initialize(options = EMPTY_HASH) # # @api public def convert(value) - if value.is_a?(Hash) + case value + when Hash pcore_type = value[PCORE_TYPE_KEY] if pcore_type && (pcore_type.is_a?(String) || pcore_type.is_a?(Hash)) @pcore_type_procs[pcore_type].call(value, pcore_type) else build({}) { value.each_pair { |key, elem| with(key) { convert(elem) } } } end - elsif value.is_a?(Array) + when Array build([]) { value.each_with_index { |elem, idx| with(idx) { convert(elem) } } } else build(value) @@ -207,7 +209,8 @@ def build_object(builder, &block) end def pcore_type_hash_to_value(pcore_type, value) - if value.is_a?(Hash) + case value + when Hash # Complex object if value.empty? build(pcore_type.create) @@ -216,7 +219,7 @@ def pcore_type_hash_to_value(pcore_type, value) else build_object(ObjectArrayBuilder.new(pcore_type.allocate)) { value.each_pair { |key, elem| with(key) { convert(elem) } } } end - elsif value.is_a?(String) + when String build(pcore_type.create(value)) else raise SerializationError, _('Cannot create a %{type_name} from a %{arg_class}') % diff --git a/lib/puppet/pops/types/p_object_type.rb b/lib/puppet/pops/types/p_object_type.rb index 2ecd4fded95..fe065d0b52d 100644 --- a/lib/puppet/pops/types/p_object_type.rb +++ b/lib/puppet/pops/types/p_object_type.rb @@ -1043,14 +1043,15 @@ def type_parameters(include_parent = false) # An Object type is only assignable from another Object type. The other type # or one of its parents must be equal to this type. def _assignable?(o, guard) - if o.is_a?(PObjectType) + case o + when PObjectType if DEFAULT == self || self == o true else op = o.parent op.nil? ? false : assignable?(op, guard) end - elsif o.is_a?(PObjectTypeExtension) + when PObjectTypeExtension assignable?(o.base_type, guard) else false diff --git a/lib/puppet/pops/types/p_uri_type.rb b/lib/puppet/pops/types/p_uri_type.rb index 2474fbfc194..c7780aa25b4 100644 --- a/lib/puppet/pops/types/p_uri_type.rb +++ b/lib/puppet/pops/types/p_uri_type.rb @@ -105,12 +105,13 @@ def from_hash(init_hash) attr_reader :parameters def initialize(parameters = nil) - if parameters.is_a?(String) + case parameters + when String parameters = TypeAsserter.assert_instance_of('URI-Type parameter', Pcore::TYPE_URI, parameters, true) @parameters = uri_to_hash(URI.parse(parameters)) - elsif parameters.is_a?(URI) + when URI @parameters = uri_to_hash(parameters) - elsif parameters.is_a?(Hash) + when Hash params = TypeAsserter.assert_instance_of('URI-Type parameter', TYPE_URI_PARAM_TYPE, parameters, true) @parameters = params.empty? ? nil : params end diff --git a/lib/puppet/pops/types/string_converter.rb b/lib/puppet/pops/types/string_converter.rb index 243d87b682e..5d05bc86438 100644 --- a/lib/puppet/pops/types/string_converter.rb +++ b/lib/puppet/pops/types/string_converter.rb @@ -591,11 +591,12 @@ def string_PRuntimeType(val_type, val, format_map, indent) # Before giving up on this, and use a string representation of the unknown # object, a check is made to see if the object can present itself as # a hash or an array. If it can, then that representation is used instead. - if val.is_a?(Hash) + case val + when Hash hash = val.to_hash # Ensure that the returned value isn't derived from Hash return string_PHashType(val_type, hash, format_map, indent) if hash.instance_of?(Hash) - elsif val.is_a?(Array) + when Array array = val.to_a # Ensure that the returned value isn't derived from Array return string_PArrayType(val_type, array, format_map, indent) if array.instance_of?(Array) diff --git a/lib/puppet/pops/types/type_calculator.rb b/lib/puppet/pops/types/type_calculator.rb index d283e7cf5bc..70faea508f7 100644 --- a/lib/puppet/pops/types/type_calculator.rb +++ b/lib/puppet/pops/types/type_calculator.rb @@ -256,15 +256,16 @@ def generalize(o) def infer(o) # Optimize the most common cases into direct calls. # Explicit if/elsif/else is faster than case - if o.is_a?(String) + case o + when String infer_String(o) - elsif o.is_a?(Integer) # need subclasses for Ruby < 2.4 + when Integer # need subclasses for Ruby < 2.4 infer_Integer(o) - elsif o.is_a?(Array) + when Array infer_Array(o) - elsif o.is_a?(Hash) + when Hash infer_Hash(o) - elsif o.is_a?(Evaluator::PuppetProc) + when Evaluator::PuppetProc infer_PuppetProc(o) else @infer_visitor.visit_this_0(self, o) diff --git a/lib/puppet/pops/types/type_factory.rb b/lib/puppet/pops/types/type_factory.rb index dbf3d585bdf..bd3e977a1e5 100644 --- a/lib/puppet/pops/types/type_factory.rb +++ b/lib/puppet/pops/types/type_factory.rb @@ -560,11 +560,12 @@ def self.uri(string_uri_or_hash = nil) # a classname. # def self.type_of(o) - if o.is_a?(Class) + case o + when Class @type_calculator.type(o) - elsif o.is_a?(PAnyType) + when PAnyType o - elsif o.is_a?(String) + when String PRuntimeType.new(:ruby, o) else @type_calculator.infer_generic(o) diff --git a/lib/puppet/pops/types/type_mismatch_describer.rb b/lib/puppet/pops/types/type_mismatch_describer.rb index d777e63b2e1..5683be152ee 100644 --- a/lib/puppet/pops/types/type_mismatch_describer.rb +++ b/lib/puppet/pops/types/type_mismatch_describer.rb @@ -677,9 +677,10 @@ def describe_signature_arguments(signature, args_tuple, path) params_tuple = signature.type.param_types params_size_t = params_tuple.size_type || TypeFactory.range(*params_tuple.size_range) - if args_tuple.is_a?(PTupleType) + case args_tuple + when PTupleType arg_types = args_tuple.types - elsif args_tuple.is_a?(PArrayType) + when PArrayType arg_types = Array.new(params_tuple.types.size, args_tuple.element_type || PUndefType::DEFAULT) else return [TypeMismatch.new(path, params_tuple, args_tuple)] @@ -798,7 +799,8 @@ def describe_PTypeAliasType(expected, original, actual, path) def describe_PArrayType(expected, original, actual, path) descriptions = [] element_type = expected.element_type || PAnyType::DEFAULT - if actual.is_a?(PTupleType) + case actual + when PTupleType types = actual.types expected_size = expected.size_type || PCollectionType::DEFAULT_SIZE actual_size = actual.size_type || PIntegerType.new(types.size, types.size) @@ -809,7 +811,7 @@ def describe_PArrayType(expected, original, actual, path) else descriptions << SizeMismatch.new(path, expected_size, actual_size) end - elsif actual.is_a?(PArrayType) + when PArrayType expected_size = expected.size_type actual_size = actual.size_type || PCollectionType::DEFAULT_SIZE if expected_size.nil? || expected_size.assignable?(actual_size) @@ -827,7 +829,8 @@ def describe_PHashType(expected, original, actual, path) descriptions = [] key_type = expected.key_type || PAnyType::DEFAULT value_type = expected.value_type || PAnyType::DEFAULT - if actual.is_a?(PStructType) + case actual + when PStructType elements = actual.elements expected_size = expected.size_type || PCollectionType::DEFAULT_SIZE actual_size = PIntegerType.new(elements.count { |a| !a.key_type.assignable?(PUndefType::DEFAULT) }, elements.size) @@ -839,7 +842,7 @@ def describe_PHashType(expected, original, actual, path) else descriptions << SizeMismatch.new(path, expected_size, actual_size) end - elsif actual.is_a?(PHashType) + when PHashType expected_size = expected.size_type actual_size = actual.size_type || PCollectionType::DEFAULT_SIZE if expected_size.nil? || expected_size.assignable?(actual_size) @@ -856,7 +859,8 @@ def describe_PHashType(expected, original, actual, path) def describe_PStructType(expected, original, actual, path) elements = expected.elements descriptions = [] - if actual.is_a?(PStructType) + case actual + when PStructType h2 = actual.hashed_elements.clone elements.each do |e1| key = e1.name @@ -869,7 +873,7 @@ def describe_PStructType(expected, original, actual, path) end end h2.each_key { |key| descriptions << ExtraneousKey.new(path, key) } - elsif actual.is_a?(PHashType) + when PHashType actual_size = actual.size_type || PCollectionType::DEFAULT_SIZE expected_size = PIntegerType.new(elements.count { |e| !e.key_type.assignable?(PUndefType::DEFAULT) }, elements.size) if expected_size.assignable?(actual_size) @@ -896,7 +900,8 @@ def describe_tuple(expected, original, actual, path, size_mismatch_class) expected_size = expected.size_type || TypeFactory.range(*expected.size_range) - if actual.is_a?(PTupleType) + case actual + when PTupleType actual_size = actual.size_type || TypeFactory.range(*actual.size_range) # not assignable if the number of types in actual is outside number of types in expected @@ -913,7 +918,7 @@ def describe_tuple(expected, original, actual, path, size_mismatch_class) else [size_mismatch_class.new(path, expected_size, actual_size)] end - elsif actual.is_a?(PArrayType) + when PArrayType t2_entry = actual.element_type if t2_entry.nil? diff --git a/lib/puppet/pops/types/type_parser.rb b/lib/puppet/pops/types/type_parser.rb index a9106cc6fc0..b12b7c392f6 100644 --- a/lib/puppet/pops/types/type_parser.rb +++ b/lib/puppet/pops/types/type_parser.rb @@ -563,9 +563,10 @@ def interpret_AccessExpression(ast, context) TypeFactory.string(size_type) when 'sensitive' - if parameters.size == 0 + case parameters.size + when 0 TypeFactory.sensitive - elsif parameters.size == 1 + when 1 param = parameters[0] assert_type(ast, param) TypeFactory.sensitive(param) @@ -644,9 +645,10 @@ def interpret_AccessExpression(ast, context) private def create_resource(name, parameters) - if parameters.size == 1 + case parameters.size + when 1 TypeFactory.resource(name) - elsif parameters.size == 2 + when 2 TypeFactory.resource(name, parameters[1]) else raise_invalid_parameters_error('Resource', '1 or 2', parameters.size) diff --git a/lib/puppet/pops/types/types.rb b/lib/puppet/pops/types/types.rb index eb961222f99..92433ecac20 100644 --- a/lib/puppet/pops/types/types.rb +++ b/lib/puppet/pops/types/types.rb @@ -489,9 +489,10 @@ def from_string(type_string) end def instance?(o, guard = nil) - if o.is_a?(PAnyType) + case o + when PAnyType type.nil? || type.assignable?(o, guard) - elsif o.is_a?(Module) || o.is_a?(Puppet::Resource) || o.is_a?(Puppet::Parser::Resource) + when Module, Puppet::Resource, Puppet::Parser::Resource @type.nil? ? true : assignable?(TypeCalculator.infer(o)) else false @@ -1577,9 +1578,10 @@ def size_type end def derived_size_type - if @size_type_or_value.is_a?(PIntegerType) + case @size_type_or_value + when PIntegerType @size_type_or_value - elsif @size_type_or_value.is_a?(String) + when String sz = @size_type_or_value.size PIntegerType.new(sz, sz) else @@ -2101,7 +2103,8 @@ def new_function # @api private def _assignable?(o, guard) - if o.is_a?(Types::PStructType) + case o + when Types::PStructType h2 = o.hashed_elements matched = 0 elements.all? do |e1| @@ -2113,7 +2116,7 @@ def _assignable?(o, guard) e1.key_type.assignable?(e2.key_type, guard) && e1.value_type.assignable?(e2.value_type, guard) end end && matched == h2.size - elsif o.is_a?(Types::PHashType) + when Types::PHashType required = 0 required_elements_assignable = elements.all? do |e| key_type = e.key_type @@ -2645,7 +2648,8 @@ def on_error(from, _ = false) # Array is assignable if o is an Array and o's element type is assignable, or if o is a Tuple # @api private def _assignable?(o, guard) - if o.is_a?(PTupleType) + case o + when PTupleType o_types = o.types size_s = size_type || DEFAULT_SIZE size_o = o.size_type @@ -2654,7 +2658,7 @@ def _assignable?(o, guard) size_o = PIntegerType.new(type_count, type_count) end size_s.assignable?(size_o) && o_types.all? { |ot| @element_type.assignable?(ot, guard) } - elsif o.is_a?(PArrayType) + when PArrayType super && @element_type.assignable?(o.element_type, guard) else false diff --git a/lib/puppet/provider/nameservice/directoryservice.rb b/lib/puppet/provider/nameservice/directoryservice.rb index bb97138a2ff..f7f516a8cce 100644 --- a/lib/puppet/provider/nameservice/directoryservice.rb +++ b/lib/puppet/provider/nameservice/directoryservice.rb @@ -288,9 +288,10 @@ def self.convert_binary_to_hash(plist_data) # By default we restrict to a reasonably sane range for system accounts def self.next_system_id(id_type, min_id = 20) dscl_args = ['.', '-list'] - if id_type == 'uid' + case id_type + when 'uid' dscl_args << '/Users' << 'uid' - elsif id_type == 'gid' + when 'gid' dscl_args << '/Groups' << 'gid' else fail(_("Invalid id_type %{id_type}. Only 'uid' and 'gid' supported") % { id_type: id_type }) diff --git a/lib/puppet/provider/package/yum.rb b/lib/puppet/provider/package/yum.rb index 7e6e0ede8ae..e582d5cb590 100644 --- a/lib/puppet/provider/package/yum.rb +++ b/lib/puppet/provider/package/yum.rb @@ -121,9 +121,10 @@ def self.check_updates(disablerepo, enablerepo, disableexcludes) output = Puppet::Util::Execution.execute(args, :failonfail => false, :combine => false) updates = {} - if output.exitstatus == 100 + case output.exitstatus + when 100 updates = parse_updates(output) - elsif output.exitstatus == 0 + when 0 self.debug "#{command(:cmd)} check-update exited with 0; no package updates available." else self.warning _("Could not check for updates, '%{cmd} check-update' exited with %{status}") % { cmd: command(:cmd), status: output.exitstatus } diff --git a/lib/puppet/reference/configuration.rb b/lib/puppet/reference/configuration.rb index c6da13c7fce..66cd7f801bf 100644 --- a/lib/puppet/reference/configuration.rb +++ b/lib/puppet/reference/configuration.rb @@ -24,23 +24,24 @@ # Now print the data about the item. val = object.default - if name.to_s == 'vardir' + case name.to_s + when 'vardir' val = 'Unix/Linux: /opt/puppetlabs/puppet/cache -- Windows: C:\ProgramData\PuppetLabs\puppet\cache -- Non-root user: ~/.puppetlabs/opt/puppet/cache' - elsif name.to_s == 'publicdir' + when 'publicdir' val = 'Unix/Linux: /opt/puppetlabs/puppet/public -- Windows: C:\ProgramData\PuppetLabs\puppet\public -- Non-root user: ~/.puppetlabs/opt/puppet/public' - elsif name.to_s == 'confdir' + when 'confdir' val = 'Unix/Linux: /etc/puppetlabs/puppet -- Windows: C:\ProgramData\PuppetLabs\puppet\etc -- Non-root user: ~/.puppetlabs/etc/puppet' - elsif name.to_s == 'codedir' + when 'codedir' val = 'Unix/Linux: /etc/puppetlabs/code -- Windows: C:\ProgramData\PuppetLabs\code -- Non-root user: ~/.puppetlabs/etc/code' - elsif name.to_s == 'rundir' + when 'rundir' val = 'Unix/Linux: /var/run/puppetlabs -- Windows: C:\ProgramData\PuppetLabs\puppet\var\run -- Non-root user: ~/.puppetlabs/var/run' - elsif name.to_s == 'logdir' + when 'logdir' val = 'Unix/Linux: /var/log/puppetlabs/puppet -- Windows: C:\ProgramData\PuppetLabs\puppet\var\log -- Non-root user: ~/.puppetlabs/var/log' - elsif name.to_s == 'hiera_config' + when 'hiera_config' val = '$confdir/hiera.yaml. However, for backwards compatibility, if a file exists at $codedir/hiera.yaml, Puppet uses that instead.' - elsif name.to_s == 'certname' + when 'certname' val = "the Host's fully qualified domain name, as determined by Facter" - elsif name.to_s == 'srv_domain' + when 'srv_domain' val = 'example.com' end diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index 83fcb8e89f4..a39c1cbf19f 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -282,9 +282,10 @@ def initialize(type, title = nil, attributes = EMPTY_HASH) @title = src.title src.to_hash.each do |p, v| - if v.is_a?(Puppet::Resource) + case v + when Puppet::Resource v = v.copy_as_resource - elsif v.is_a?(Array) + when Array # flatten resource references arrays v = v.flatten if v.flatten.find { |av| av.is_a?(Puppet::Resource) } v = v.collect do |av| diff --git a/lib/puppet/resource/status.rb b/lib/puppet/resource/status.rb index 97e5c5c66bf..b80be7656b2 100644 --- a/lib/puppet/resource/status.rb +++ b/lib/puppet/resource/status.rb @@ -121,9 +121,10 @@ def <<(event) def add_event(event) @events << event - if event.status == 'failure' + case event.status + when 'failure' self.failed = true - elsif event.status == 'success' + when 'success' @change_count += 1 @changed = true end diff --git a/lib/puppet/settings.rb b/lib/puppet/settings.rb index 47c4d3beda9..375d8f98633 100644 --- a/lib/puppet/settings.rb +++ b/lib/puppet/settings.rb @@ -121,9 +121,10 @@ def stringifyhash(hash) newhash = {} hash.each do |key, val| key = key.to_s - if val.is_a? Hash + case val + when Hash newhash[key] = stringifyhash(val) - elsif val.is_a? Symbol + when Symbol newhash[key] = val.to_s else newhash[key] = val @@ -475,9 +476,10 @@ def setting(param) def handlearg(opt, value = nil) @cache.clear - if value.is_a?(FalseClass) + case value + when FalseClass value = "false" - elsif value.is_a?(TrueClass) + when TrueClass value = "true" end diff --git a/lib/puppet/util/command_line/puppet_option_parser.rb b/lib/puppet/util/command_line/puppet_option_parser.rb index 684b005618d..20f98373cf1 100644 --- a/lib/puppet/util/command_line/puppet_option_parser.rb +++ b/lib/puppet/util/command_line/puppet_option_parser.rb @@ -40,9 +40,10 @@ def ignore_invalid_options=(value) def on(*args, &block) # The 2nd element is an optional "short" representation. - if args.length == 3 + case args.length + when 3 long, desc, type = args - elsif args.length == 4 + when 4 long, short, desc, type = args else raise ArgumentError, _("this method only takes 3 or 4 arguments. Given: %{args}") % { args: args.inspect } diff --git a/lib/puppet/util/execution.rb b/lib/puppet/util/execution.rb index 82d48cf7c2f..150cd22be84 100644 --- a/lib/puppet/util/execution.rb +++ b/lib/puppet/util/execution.rb @@ -153,10 +153,11 @@ def self.execute(command, options = NoOptionsSpecified) options = default_options.merge(options) - if command.is_a?(Array) + case command + when Array command = command.flatten.map(&:to_s) command_str = command.join(" ") - elsif command.is_a?(String) + when String command_str = command end diff --git a/lib/puppet/util/plist.rb b/lib/puppet/util/plist.rb index d02f1fe8bdf..131391ea3c3 100644 --- a/lib/puppet/util/plist.rb +++ b/lib/puppet/util/plist.rb @@ -130,11 +130,12 @@ def read_file_with_offset(file_path, offset) end def to_format(format) - if format.to_sym == :xml + case format.to_sym + when :xml CFPropertyList::List::FORMAT_XML - elsif format.to_sym == :binary + when :binary CFPropertyList::List::FORMAT_BINARY - elsif format.to_sym == :plain + when :plain CFPropertyList::List::FORMAT_PLAIN else raise FormatError.new "Unknown plist format #{format}" diff --git a/lib/puppet/util/rdoc/code_objects.rb b/lib/puppet/util/rdoc/code_objects.rb index 329cdf8f04f..8f06718ca4d 100644 --- a/lib/puppet/util/rdoc/code_objects.rb +++ b/lib/puppet/util/rdoc/code_objects.rb @@ -46,10 +46,11 @@ def add_plugin(plugin) meth.document_self = true meth.singleton = false meth.comment = plugin.comment - if type == 'function' + case type + when 'function' @function_container ||= add_module(NormalModule, "__functions__") @function_container.add_method(meth) - elsif type == 'type' + when 'type' @type_container ||= add_module(NormalModule, "__types__") @type_container.add_method(meth) end diff --git a/lib/puppet/util/rdoc/parser/puppet_parser_core.rb b/lib/puppet/util/rdoc/parser/puppet_parser_core.rb index 505355675c0..3ead0bfca29 100644 --- a/lib/puppet/util/rdoc/parser/puppet_parser_core.rb +++ b/lib/puppet/util/rdoc/parser/puppet_parser_core.rb @@ -153,16 +153,17 @@ def parse_fact(container) File.open(@input_file_name) do |of| of.each do |line| # fetch comments - if line =~ /^[ \t]*# ?(.*)$/ + case line + when /^[ \t]*# ?(.*)$/ comments += $1 + "\n" - elsif line =~ /^[ \t]*(Facter.add|Puppet\.runtime\[:facter\].add)\(['"](.*?)['"]\)/ + when /^[ \t]*(Facter.add|Puppet\.runtime\[:facter\].add)\(['"](.*?)['"]\)/ current_fact = RDoc::Fact.new($1, {}) look_for_directives_in(container, comments) unless comments.empty? current_fact.comment = comments parsed_facts << current_fact comments = "" Puppet.debug "rdoc: found custom fact #{current_fact.name}" - elsif line =~ /^[ \t]*confine[ \t]*:(.*?)[ \t]*=>[ \t]*(.*)$/ + when /^[ \t]*confine[ \t]*:(.*?)[ \t]*=>[ \t]*(.*)$/ current_fact.confine = { :type => $1, :value => $2 } unless current_fact.nil? else # unknown line type comments = "" @@ -184,9 +185,10 @@ def parse_puppet_plugin(container) File.open(@input_file_name) do |of| of.each do |line| # fetch comments - if line =~ /^[ \t]*# ?(.*)$/ + case line + when /^[ \t]*# ?(.*)$/ comments += $1 + "\n" - elsif line =~ /^[ \t]*(?:Puppet::Parser::Functions::)?newfunction[ \t]*\([ \t]*:(.*?)[ \t]*,[ \t]*:type[ \t]*=>[ \t]*(:rvalue|:lvalue)/ + when /^[ \t]*(?:Puppet::Parser::Functions::)?newfunction[ \t]*\([ \t]*:(.*?)[ \t]*,[ \t]*:type[ \t]*=>[ \t]*(:rvalue|:lvalue)/ current_plugin = RDoc::Plugin.new($1, "function") look_for_directives_in(container, comments) unless comments.empty? current_plugin.comment = comments @@ -194,7 +196,7 @@ def parse_puppet_plugin(container) container.add_plugin(current_plugin) comments = "" Puppet.debug "rdoc: found new function plugins #{current_plugin.name}" - elsif line =~ /^[ \t]*Puppet::Type.newtype[ \t]*\([ \t]*:(.*?)\)/ + when /^[ \t]*Puppet::Type.newtype[ \t]*\([ \t]*:(.*?)\)/ current_plugin = RDoc::Plugin.new($1, "type") look_for_directives_in(container, comments) unless comments.empty? current_plugin.comment = comments @@ -202,7 +204,7 @@ def parse_puppet_plugin(container) container.add_plugin(current_plugin) comments = "" Puppet.debug "rdoc: found new type plugins #{current_plugin.name}" - elsif line =~ /module Puppet::Parser::Functions/ + when /module Puppet::Parser::Functions/ # skip else # unknown line type comments = "" diff --git a/lib/puppet/util/symbolic_file_mode.rb b/lib/puppet/util/symbolic_file_mode.rb index addc3cd35fe..1b2a7c937c0 100644 --- a/lib/puppet/util/symbolic_file_mode.rb +++ b/lib/puppet/util/symbolic_file_mode.rb @@ -38,9 +38,10 @@ def normalize_symbolic_mode(value) # "A numeric mode is from one to four octal digits (0-7), derived by adding # up the bits with values 4, 2, and 1. Omitted digits are assumed to be # leading zeros." - if value.is_a? Numeric + case value + when Numeric value.to_s(8) - elsif value =~ /^0?[0-7]{1,4}$/ + when /^0?[0-7]{1,4}$/ value.to_i(8).to_s(8) # strip leading 0's else value