From d0d5d6ca1212175031492c87530d082283b29e31 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:05:11 -0800 Subject: [PATCH 1/9] (PUP-11767) Enable Style/Dir --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 9dee714e36c..e118940cbee 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -232,3 +232,6 @@ Style/ColonMethodDefinition: Style/DefWithParentheses: Enabled: true + +Style/Dir: + Enabled: true From 2d8754487641270e1b23ee86bba97d79ee85463b Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:06:03 -0800 Subject: [PATCH 2/9] (PUP-11767) Enable Style/DocumentDynamicEvalDefinition --- .rubocop.yml | 3 +++ lib/puppet/interface/documentation.rb | 16 ++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index e118940cbee..e38c0ce581b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -235,3 +235,6 @@ Style/DefWithParentheses: Style/Dir: Enabled: true + +Style/DocumentDynamicEvalDefinition: + Enabled: true diff --git a/lib/puppet/interface/documentation.rb b/lib/puppet/interface/documentation.rb index 6357d3bfd1c..091d405552c 100644 --- a/lib/puppet/interface/documentation.rb +++ b/lib/puppet/interface/documentation.rb @@ -34,14 +34,14 @@ def attr_doc(name, &validate) # without as methods. When we are 1.9 only (hah!) you can totally # replace this with some up-and-up define_method. --daniel 2011-04-29 module_eval(<<-EOT, __FILE__, __LINE__ + 1) - def #{name}(value = nil) - self.#{name} = value unless value.nil? - @#{name} - end - - def #{name}=(value) - @#{name} = Puppet::Interface::DocGen.strip_whitespace(#{get_arg}) - end + def #{name}(value = nil) # def attribute(value=nil) + self.#{name} = value unless value.nil? # self.attribute = value unless value.nil? + @#{name} # @value + end # end + + def #{name}=(value) # def attribute=(value) + @#{name} = Puppet::Interface::DocGen.strip_whitespace(#{get_arg}) # @value = Puppet::Interface::DocGen.strip_whitespace(#{get_arg}) + end # end EOT end end From 29e29fbe7d882d788f3e07bb71f4d7e6ad343439 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:07:54 -0800 Subject: [PATCH 3/9] (PUP-11767) Enable Style/DoubleCopDisableDirective --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index e38c0ce581b..4e0831f4579 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -238,3 +238,6 @@ Style/Dir: Style/DocumentDynamicEvalDefinition: Enabled: true + +Style/DoubleCopDisableDirective: + Enabled: true From d889f5d5e918bca66fcc430854f39a98082a8a43 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:11:38 -0800 Subject: [PATCH 4/9] (PUP-11767) Enable Style/EachforSimpleLoop --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 4e0831f4579..42dc00d8adf 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -241,3 +241,6 @@ Style/DocumentDynamicEvalDefinition: Style/DoubleCopDisableDirective: Enabled: true + +Style/EachForSimpleLoop: + Enabled: true From b813b5d29b28b45629302c53f369d25403d690ce Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:33:33 -0800 Subject: [PATCH 5/9] (PUP-11767) Enable Style/EachWithObject --- .rubocop.yml | 3 +++ lib/puppet/confine/exists.rb | 2 +- lib/puppet/confine/variable.rb | 2 +- lib/puppet/face/epp.rb | 3 +-- lib/puppet/face/module/list.rb | 3 +-- lib/puppet/http/dns.rb | 3 +-- lib/puppet/indirector/node/exec.rb | 4 +--- lib/puppet/indirector/request.rb | 2 +- lib/puppet/network/http/handler.rb | 3 +-- lib/puppet/parser/resource.rb | 3 +-- .../collectors/abstract_collector.rb | 2 +- .../collectors/fixed_set_collector.rb | 3 +-- lib/puppet/pops/evaluator/evaluator_impl.rb | 5 ++--- .../json_strict_literal_evaluator.rb | 3 +-- .../pops/evaluator/literal_evaluator.rb | 3 +-- lib/puppet/pops/loader/dependency_loader.rb | 2 +- lib/puppet/pops/model/factory.rb | 3 +-- lib/puppet/pops/parser/locator.rb | 2 +- lib/puppet/pops/types/string_converter.rb | 6 ++---- lib/puppet/pops/types/types.rb | 2 +- lib/puppet/property/keyvalue.rb | 3 +-- lib/puppet/provider/ldap.rb | 3 +-- lib/puppet/provider/user/user_role_add.rb | 2 +- lib/puppet/relationship.rb | 2 +- lib/puppet/resource/catalog.rb | 20 ++++++++----------- lib/puppet/transaction/event_manager.rb | 3 +-- lib/puppet/type.rb | 5 ++--- lib/puppet/type/file.rb | 3 +-- lib/puppet/type/tidy.rb | 2 +- lib/puppet/util/ldap/manager.rb | 5 ++--- lib/puppet/util/logging.rb | 3 +-- util/rspec_grouper | 3 +-- 32 files changed, 45 insertions(+), 68 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 42dc00d8adf..3654be21c4b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -244,3 +244,6 @@ Style/DoubleCopDisableDirective: Style/EachForSimpleLoop: Enabled: true + +Style/EachWithObject: + Enabled: true diff --git a/lib/puppet/confine/exists.rb b/lib/puppet/confine/exists.rb index 45752de666b..2fdf7c98591 100644 --- a/lib/puppet/confine/exists.rb +++ b/lib/puppet/confine/exists.rb @@ -16,6 +16,6 @@ def message(value) end def summary - result.zip(values).inject([]) { |array, args| val, f = args; array << f unless val; array } + result.zip(values).each_with_object([]) { |args, array| val, f = args; array << f unless val; } end end diff --git a/lib/puppet/confine/variable.rb b/lib/puppet/confine/variable.rb index d03750dbed4..4c4cf31622d 100644 --- a/lib/puppet/confine/variable.rb +++ b/lib/puppet/confine/variable.rb @@ -12,7 +12,7 @@ class Puppet::Confine::Variable < Puppet::Confine # Only returns failed values, not all required values. def self.summarize(confines) result = Hash.new { |hash, key| hash[key] = [] } - confines.inject(result) { |total, confine| total[confine.name] += confine.values unless confine.valid?; total } + confines.each_with_object(result) { |confine, total| total[confine.name] += confine.values unless confine.valid?; } end # This is set by ConfineCollection. diff --git a/lib/puppet/face/epp.rb b/lib/puppet/face/epp.rb index 72c45a2ea7c..e830bd08632 100644 --- a/lib/puppet/face/epp.rb +++ b/lib/puppet/face/epp.rb @@ -180,14 +180,13 @@ raise Puppet::Error, _("No input to parse given on command line or stdin") end else - templates, missing_files = args.reduce([[], []]) do |memo, file| + templates, missing_files = args.each_with_object([[], []]) do |file, memo| template_file = effective_template(file, compiler.environment) if template_file.nil? memo[1] << file else memo[0] << template_file end - memo end show_filename = templates.count > 1 diff --git a/lib/puppet/face/module/list.rb b/lib/puppet/face/module/list.rb index c267033adfb..8a93db58600 100644 --- a/lib/puppet/face/module/list.rb +++ b/lib/puppet/face/module/list.rb @@ -119,9 +119,8 @@ def unmet_dependencies(environment) # Prepare the unmet dependencies for display on the console. environment.modules.sort_by { |mod| mod.name }.each do |mod| unmet_grouped = Hash.new { |h, k| h[k] = [] } - unmet_grouped = mod.unmet_dependencies.inject(unmet_grouped) do |acc, dep| + unmet_grouped = mod.unmet_dependencies.each_with_object(unmet_grouped) do |dep, acc| acc[dep[:reason]] << dep - acc end unmet_grouped.each do |type, deps| unless deps.empty? diff --git a/lib/puppet/http/dns.rb b/lib/puppet/http/dns.rb index 36b6d98dcd6..d766f6d1061 100644 --- a/lib/puppet/http/dns.rb +++ b/lib/puppet/http/dns.rb @@ -146,10 +146,9 @@ def expired?(service_name) # @yields [[Resolv::DNS::Resource::IN::SRV]] a group of records of # the same priority def each_priority(records) - pri_hash = records.inject({}) do |groups, element| + pri_hash = records.each_with_object({}) do |element, groups| groups[element.priority] ||= [] groups[element.priority] << element - groups end pri_hash.keys.sort.each do |key| diff --git a/lib/puppet/indirector/node/exec.rb b/lib/puppet/indirector/node/exec.rb index 55b7088a8b9..f2acfe22e88 100644 --- a/lib/puppet/indirector/node/exec.rb +++ b/lib/puppet/indirector/node/exec.rb @@ -54,7 +54,7 @@ def create_node(name, result, facts = nil) # Translate the yaml string into Ruby objects. def translate(name, output) - Puppet::Util::Yaml.safe_load(output, [Symbol]).inject({}) do |hash, data| + Puppet::Util::Yaml.safe_load(output, [Symbol]).each_with_object({}) do |data, hash| case data[0] when String hash[data[0].intern] = data[1] @@ -63,8 +63,6 @@ def translate(name, output) else raise Puppet::Error, _("key is a %{klass}, not a string or symbol") % { klass: data[0].class } end - - hash end rescue => detail raise Puppet::Error, _("Could not load external node results for %{name}: %{detail}") % { name: name, detail: detail }, detail.backtrace diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index e0b4956cc78..df90cb03ea2 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -68,7 +68,7 @@ def initialize(indirection_name, method, key, instance, options = {}) self.indirection_name = indirection_name self.method = method - options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } + options = options.each_with_object({}) { |ary, hash| hash[ary[0].to_sym] = ary[1]; } set_attributes(options) diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index cf879d6383f..50224febd93 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -171,10 +171,9 @@ def client_cert(request) end def decode_params(params) - params.select { |key, _| allowed_parameter?(key) }.inject({}) do |result, ary| + params.select { |key, _| allowed_parameter?(key) }.each_with_object({}) do |ary, result| param, value = ary result[param.to_sym] = parse_parameter_value(param, value) - result end end diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index f4c912b5597..c94a4bf190d 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -216,7 +216,7 @@ def set_parameter(param, value = nil) alias []= set_parameter def to_hash - parse_title.merge(@parameters.reduce({}) do |result, (_, param)| + parse_title.merge(@parameters.each_with_object({}) do |(_, param), result| value = param.value value = (:undef == value) ? nil : value @@ -231,7 +231,6 @@ def to_hash result[param.name] = value end end - result end) end diff --git a/lib/puppet/pops/evaluator/collectors/abstract_collector.rb b/lib/puppet/pops/evaluator/collectors/abstract_collector.rb index ea6f0d95e15..0ea9d26af1d 100644 --- a/lib/puppet/pops/evaluator/collectors/abstract_collector.rb +++ b/lib/puppet/pops/evaluator/collectors/abstract_collector.rb @@ -63,7 +63,7 @@ def evaluate return false if objects.empty? - objects.reduce(@collected) { |c, o| c[o.ref] = o; c } + objects.each_with_object(@collected) { |o, c| c[o.ref] = o; } objects end diff --git a/lib/puppet/pops/evaluator/collectors/fixed_set_collector.rb b/lib/puppet/pops/evaluator/collectors/fixed_set_collector.rb index 117ea374b17..cf21499ba55 100644 --- a/lib/puppet/pops/evaluator/collectors/fixed_set_collector.rb +++ b/lib/puppet/pops/evaluator/collectors/fixed_set_collector.rb @@ -16,14 +16,13 @@ def initialize(scope, resources) # by the realize function def collect resolved = [] - result = @resources.reduce([]) do |memo, ref| + result = @resources.each_with_object([]) do |ref, memo| res = @scope.findresource(ref.to_s) if res res.virtual = false memo << res resolved << ref end - memo end @resources = @resources - resolved diff --git a/lib/puppet/pops/evaluator/evaluator_impl.rb b/lib/puppet/pops/evaluator/evaluator_impl.rb index a0063e97ce3..19c81a9e156 100644 --- a/lib/puppet/pops/evaluator/evaluator_impl.rb +++ b/lib/puppet/pops/evaluator/evaluator_impl.rb @@ -667,7 +667,7 @@ def eval_LiteralList o, scope # def eval_LiteralHash o, scope # optimized - o.entries.reduce({}) { |h, entry| h[evaluate(entry.key, scope)] = evaluate(entry.value, scope); h } + o.entries.each_with_object({}) { |entry, h| h[evaluate(entry.key, scope)] = evaluate(entry.value, scope); } end # Evaluates all statements and produces the last evaluated value @@ -861,7 +861,7 @@ def eval_ResourceExpression(o, scope) # Store evaluated parameters in a hash associated with the body, but do not yet create resource # since the entry containing :defaults may appear later - body_to_params[body] = body.operations.reduce({}) do |param_memo, op| + body_to_params[body] = body.operations.each_with_object({}) do |op, param_memo| params = evaluate(op, scope) params = [params] unless params.is_a?(Array) params.each do |p| @@ -871,7 +871,6 @@ def eval_ResourceExpression(o, scope) param_memo[p.name] = p end - param_memo end end diff --git a/lib/puppet/pops/evaluator/json_strict_literal_evaluator.rb b/lib/puppet/pops/evaluator/json_strict_literal_evaluator.rb index 6c2be91b82c..a04a95ef40a 100644 --- a/lib/puppet/pops/evaluator/json_strict_literal_evaluator.rb +++ b/lib/puppet/pops/evaluator/json_strict_literal_evaluator.rb @@ -74,11 +74,10 @@ def literal_LiteralList(o) end def literal_LiteralHash(o) - o.entries.reduce({}) do |result, entry| + o.entries.each_with_object({}) do |entry, result| key = literal(entry.key) throw :not_literal unless key.is_a?(String) result[key] = literal(entry.value) - result end end end diff --git a/lib/puppet/pops/evaluator/literal_evaluator.rb b/lib/puppet/pops/evaluator/literal_evaluator.rb index 4bf30c6d953..c56f08fd1cb 100644 --- a/lib/puppet/pops/evaluator/literal_evaluator.rb +++ b/lib/puppet/pops/evaluator/literal_evaluator.rb @@ -87,9 +87,8 @@ def literal_LiteralList(o) end def literal_LiteralHash(o) - o.entries.reduce({}) do |result, entry| + o.entries.each_with_object({}) do |entry, result| result[literal(entry.key)] = literal(entry.value) - result end end end diff --git a/lib/puppet/pops/loader/dependency_loader.rb b/lib/puppet/pops/loader/dependency_loader.rb index e6ff945d8c0..0204fb19037 100644 --- a/lib/puppet/pops/loader/dependency_loader.rb +++ b/lib/puppet/pops/loader/dependency_loader.rb @@ -90,6 +90,6 @@ def loaded_entry_in_dependency(typed_name, check_dependencies) # An index of module_name to module loader used to speed up lookup of qualified names def index - @index ||= @dependency_loaders.reduce({}) { |index, loader| index[loader.module_name] = loader; index } + @index ||= @dependency_loaders.each_with_object({}) { |loader, index| index[loader.module_name] = loader; } end end diff --git a/lib/puppet/pops/model/factory.rb b/lib/puppet/pops/model/factory.rb index 21fafb6a7bc..62e9353c3a2 100644 --- a/lib/puppet/pops/model/factory.rb +++ b/lib/puppet/pops/model/factory.rb @@ -1007,7 +1007,7 @@ def initialize(args, name_expr) # expression, or expression list. # def self.transform_calls(expressions) - expressions.reduce([]) do |memo, expr| + expressions.each_with_object([]) do |expr, memo| name = memo[-1] if name.instance_of?(Factory) && name.model_class <= QualifiedName && name_is_statement?(name[KEY_VALUE]) if expr.is_a?(Array) @@ -1035,7 +1035,6 @@ def self.transform_calls(expressions) expr['rval_required'] = false end end - memo end end diff --git a/lib/puppet/pops/parser/locator.rb b/lib/puppet/pops/parser/locator.rb index 5dd0fddfaf0..4bd878c7fd7 100644 --- a/lib/puppet/pops/parser/locator.rb +++ b/lib/puppet/pops/parser/locator.rb @@ -240,7 +240,7 @@ def initialize(locator, str, leading_line_count, leading_offset, has_margin, mar # The last entry is duplicated since there will be the line "after last line" that would otherwise require # conditional logic. # - @accumulated_margin = margin_per_line.reduce([0]) { |memo, val| memo << memo[-1] + val; memo } + @accumulated_margin = margin_per_line.each_with_object([0]) { |val, memo| memo << memo[-1] + val; } @accumulated_margin << @accumulated_margin[-1] end diff --git a/lib/puppet/pops/types/string_converter.rb b/lib/puppet/pops/types/string_converter.rb index 5d05bc86438..40ad15a0f7f 100644 --- a/lib/puppet/pops/types/string_converter.rb +++ b/lib/puppet/pops/types/string_converter.rb @@ -535,7 +535,7 @@ def validate_input(fmt) raise ArgumentError, "expected a hash with type to format mappings, got instance of '#{fmt.class}'" end - fmt.reduce({}) do |result, entry| + fmt.each_with_object({}) do |entry, result| key, value = entry unless key.is_a?(Types::PAnyType) raise ArgumentError, "top level keys in the format hash must be data types, got instance of '#{key.class}'" @@ -546,7 +546,6 @@ def validate_input(fmt) else result[key] = Format.new(value) end - result end end private :validate_input @@ -969,14 +968,13 @@ def string_PArrayType(val_type, val, format_map, indentation) # compute widest run in the array, skip nested arrays and hashes # then if size > width, set flag if a break on each element should be performed if format.alt? && format.width - widest = val.each_with_index.reduce([0]) do |memo, v_i| + widest = val.each_with_index.each_with_object([0]) do |v_i, memo| # array or hash breaks if is_a_or_h?(v_i[0]) memo << 0 else memo[-1] += mapped[v_i[1]].length end - memo end widest = widest.max sz_break = widest > (format.width || Float::INFINITY) diff --git a/lib/puppet/pops/types/types.rb b/lib/puppet/pops/types/types.rb index 60b49a0ef41..8a2040d4b5f 100644 --- a/lib/puppet/pops/types/types.rb +++ b/lib/puppet/pops/types/types.rb @@ -2029,7 +2029,7 @@ def normalize(guard = nil) # rubocop:disable Naming/MemoizedInstanceVariableName def hashed_elements - @hashed ||= @elements.reduce({}) { |memo, e| memo[e.name] = e; memo } + @hashed ||= @elements.each_with_object({}) { |e, memo| memo[e.name] = e; } end # rubocop:enable Naming/MemoizedInstanceVariableName diff --git a/lib/puppet/property/keyvalue.rb b/lib/puppet/property/keyvalue.rb index eef2982ef80..155cdb2a4dd 100644 --- a/lib/puppet/property/keyvalue.rb +++ b/lib/puppet/property/keyvalue.rb @@ -56,10 +56,9 @@ def hashify_should end # Here, should is an array of key/value pairs. - @should.inject({}) do |hash, key_value| + @should.each_with_object({}) do |key_value, hash| tmp = key_value.split(separator) hash[tmp[0].strip.intern] = tmp[1] - hash end end diff --git a/lib/puppet/provider/ldap.rb b/lib/puppet/provider/ldap.rb index b57b308e220..8d8854dcfc9 100644 --- a/lib/puppet/provider/ldap.rb +++ b/lib/puppet/provider/ldap.rb @@ -84,7 +84,7 @@ def initialize(*args) super - @property_hash = @property_hash.inject({}) do |result, ary| + @property_hash = @property_hash.each_with_object({}) do |ary, result| param, values = ary # Skip any attributes we don't manage. @@ -104,7 +104,6 @@ def initialize(*args) else result[param] = values end - result end # Make a duplicate, so that we have a copy for comparison diff --git a/lib/puppet/provider/user/user_role_add.rb b/lib/puppet/provider/user/user_role_add.rb index a3aeec3677f..1bf6fe254a9 100644 --- a/lib/puppet/provider/user/user_role_add.rb +++ b/lib/puppet/provider/user/user_role_add.rb @@ -145,7 +145,7 @@ def managed_attributes def remove_managed_attributes managed = managed_attributes - user_attributes.select { |k, _v| !managed.include?(k) }.inject({}) { |hash, array| hash[array[0]] = array[1]; hash } + user_attributes.select { |k, _v| !managed.include?(k) }.each_with_object({}) { |array, hash| hash[array[0]] = array[1]; } end def keys diff --git a/lib/puppet/relationship.rb b/lib/puppet/relationship.rb index e7297e99714..34c72c9d76f 100644 --- a/lib/puppet/relationship.rb +++ b/lib/puppet/relationship.rb @@ -36,7 +36,7 @@ def event=(event) def initialize(source, target, options = {}) @source, @target = source, target - options = (options || {}).inject({}) { |h, a| h[a[0].to_sym] = a[1]; h } + options = (options || {}).each_with_object({}) { |a, h| h[a[0].to_sym] = a[1]; } [:callback, :event].each do |option| value = options[option] send(option.to_s + "=", value) if value diff --git a/lib/puppet/resource/catalog.rb b/lib/puppet/resource/catalog.rb index 29fb6522f3b..9f2a2c3cba7 100644 --- a/lib/puppet/resource/catalog.rb +++ b/lib/puppet/resource/catalog.rb @@ -452,16 +452,14 @@ def self.from_data_hash(data) result.add_class(*data['classes']) if data['classes'] - result.metadata = data['metadata'].inject({}) { |h, (k, v)| h[k] = Puppet::FileServing::Metadata.from_data_hash(v); h } if data['metadata'] + result.metadata = data['metadata'].transform_values { |v| Puppet::FileServing::Metadata.from_data_hash(v); } if data['metadata'] recursive_metadata = data['recursive_metadata'] if recursive_metadata - result.recursive_metadata = recursive_metadata.inject({}) do |h, (title, source_to_meta_hash)| - h[title] = source_to_meta_hash.inject({}) do |inner_h, (source, metas)| - inner_h[source] = metas.map { |meta| Puppet::FileServing::Metadata.from_data_hash(meta) } - inner_h + result.recursive_metadata = recursive_metadata.transform_values do |source_to_meta_hash| + source_to_meta_hash.transform_values do |metas| + metas.map { |meta| Puppet::FileServing::Metadata.from_data_hash(meta) } end - h end end @@ -469,13 +467,11 @@ def self.from_data_hash(data) end def to_data_hash - metadata_hash = metadata.inject({}) { |h, (k, v)| h[k] = v.to_data_hash; h } - recursive_metadata_hash = recursive_metadata.inject({}) do |h, (title, source_to_meta_hash)| - h[title] = source_to_meta_hash.inject({}) do |inner_h, (source, metas)| - inner_h[source] = metas.map { |meta| meta.to_data_hash } - inner_h + metadata_hash = metadata.transform_values { |v| v.to_data_hash; } + recursive_metadata_hash = recursive_metadata.transform_values do |source_to_meta_hash| + source_to_meta_hash.transform_values do |metas| + metas.map { |meta| meta.to_data_hash } end - h end { diff --git a/lib/puppet/transaction/event_manager.rb b/lib/puppet/transaction/event_manager.rb index 0e5cb71033c..aa5aca212f7 100644 --- a/lib/puppet/transaction/event_manager.rb +++ b/lib/puppet/transaction/event_manager.rb @@ -54,10 +54,9 @@ def queue_events(resource, events) # Do some basic normalization so we're not doing so many # graph queries for large sets of events. - events.inject({}) do |collection, event| + events.each_with_object({}) do |event, collection| collection[event.name] ||= [] collection[event.name] << event - collection end.collect do |_name, list| # It doesn't matter which event we use - they all have the same source # and name here. diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 07263c75226..a7de064a20e 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -1117,7 +1117,7 @@ def currentpropvalues # is the first property, which is important for skipping 'retrieve' on # all the properties if the resource is absent. ensure_state = false - return properties.inject({}) do |prophash, property| + return properties.each_with_object({}) do |property, prophash| if property.name == :ensure ensure_state = property.retrieve prophash[property] = ensure_state @@ -1128,7 +1128,6 @@ def currentpropvalues prophash[property] = property.retrieve end end - prophash end end @@ -1203,7 +1202,7 @@ def self.providers_by_source # @return [Puppet::Resource] the resource created from the hash # @raise [Puppet::Error] if a title is missing in the given hash def self.hash2resource(hash) - hash = hash.inject({}) { |result, ary| result[ary[0].to_sym] = ary[1]; result } + hash = hash.each_with_object({}) { |ary, result| result[ary[0].to_sym] = ary[1]; } title = hash.delete(:title) title ||= hash[:name] diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 8d6fca2ed54..46f5cb3a6cc 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -701,11 +701,10 @@ def recurse_local result = perform_recursion(self[:path]) return {} unless result - result.inject({}) do |hash, meta| + result.each_with_object({}) do |meta, hash| next hash if meta.relative_path == "." hash[meta.relative_path] = newchild(meta.relative_path) - hash end end diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb index c0c96227adb..dad3ffa1369 100644 --- a/lib/puppet/type/tidy.rb +++ b/lib/puppet/type/tidy.rb @@ -304,7 +304,7 @@ def generate # Now make sure that all directories require the files they contain, if all are available, # so that a directory is emptied before we try to remove it. - files_by_name = result.inject({}) { |hash, file| hash[file[:path]] = file; hash } + files_by_name = result.each_with_object({}) { |file, hash| hash[file[:path]] = file; } files_by_name.keys.sort { |a, b| b <=> a }.each do |path| dir = ::File.dirname(path) diff --git a/lib/puppet/util/ldap/manager.rb b/lib/puppet/util/ldap/manager.rb index 451ef7dcce1..a643ceda273 100644 --- a/lib/puppet/util/ldap/manager.rb +++ b/lib/puppet/util/ldap/manager.rb @@ -178,7 +178,7 @@ def maps(attributes) @puppet2ldap = attributes # and the ldap attributes as the keys. - @ldap2puppet = attributes.inject({}) { |map, ary| map[ary[1]] = ary[0]; map } + @ldap2puppet = attributes.each_with_object({}) { |ary, map| map[ary[1]] = ary[0]; } self end @@ -277,10 +277,9 @@ def valid? # Convert a hash of attributes to ldap-like forms. This mostly means # getting rid of :ensure and making sure everything's an array of strings. def ldap_convert(attributes) - attributes.reject { |param, value| value == :absent or param == :ensure }.inject({}) do |result, ary| + attributes.reject { |param, value| value == :absent or param == :ensure }.each_with_object({}) do |ary, result| value = (ary[1].is_a?(Array) ? ary[1] : [ary[1]]).collect { |v| v.to_s } result[ldap_name(ary[0])] = value - result end end end diff --git a/lib/puppet/util/logging.rb b/lib/puppet/util/logging.rb index 14c36fe0d88..2b910ff658c 100644 --- a/lib/puppet/util/logging.rb +++ b/lib/puppet/util/logging.rb @@ -290,9 +290,8 @@ def is_resource_parameter? end def log_metadata - [:file, :line, :tags].inject({}) do |result, attr| + [:file, :line, :tags].each_with_object({}) do |attr, result| result[attr] = send(attr) if respond_to?(attr) - result end end diff --git a/util/rspec_grouper b/util/rspec_grouper index be6b9ce0166..756ce61a13d 100755 --- a/util/rspec_grouper +++ b/util/rspec_grouper @@ -30,12 +30,11 @@ module Parallel # Populate a map of spec file => example count, sorted ascending by count # NOTE: this uses a private API of RSpec and is may break if the gem is updated - @files = ::RSpec.world.example_groups.inject({}) do |files, group| + @files = ::RSpec.world.example_groups.each_with_object({}) do |group, files| file = group.metadata[:example_group_block].source_location[0] count = count_examples(group) files[file] = (files[file] || 0) + count @total_examples += count - files end.sort_by { |_, v| v } # Group the spec files From f93b9fd14d5b7dfe01e6db89ee39b7cf16ec15b7 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:34:50 -0800 Subject: [PATCH 6/9] (PUP-11767) Enable Style/EmptyBlockParameter --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 3654be21c4b..0f5cabda00d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -247,3 +247,6 @@ Style/EachForSimpleLoop: Style/EachWithObject: Enabled: true + +Style/EmptyBlockParameter: + Enabled: true From ac0f97f32dcc9a4e8279f306b8f87e40202ecfed Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:35:49 -0800 Subject: [PATCH 7/9] (PUP-11767) Enable Style/EmptyCaseCondition --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 0f5cabda00d..f0302c39cf3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -250,3 +250,6 @@ Style/EachWithObject: Style/EmptyBlockParameter: Enabled: true + +Style/EmptyCaseCondition: + Enabled: true From 8988a6ce2fd0afbf2fc87ad4b5751f695a11df9e Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 11:46:19 -0800 Subject: [PATCH 8/9] (PUP-11767) Enable several more rubocop Style cops --- .rubocop.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index f0302c39cf3..cffda3497fa 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -253,3 +253,18 @@ Style/EmptyBlockParameter: Style/EmptyCaseCondition: Enabled: true + +Style/EmptyLambdaParameter: + Enabled: true + +Style/EmptyLiteral: + Enabled: true + +Style/EvalWithLocation: + Enabled: true + +Style/EvenOdd: + Enabled: true + +Style/ExpandPathArguments: + Enabled: true From 121ed919d4d3fa5313da1dfc2bc62e77a2426e66 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Tue, 30 Jan 2024 14:45:46 -0800 Subject: [PATCH 9/9] (PUP-11767) Enable Style/FetchEnvVar --- .rubocop.yml | 3 +++ ext/windows/service/daemon.rb | 6 +++--- lib/puppet/application/resource.rb | 2 +- lib/puppet/defaults.rb | 6 +++--- lib/puppet/file_system/uniquefile.rb | 2 +- lib/puppet/http/proxy.rb | 4 ++-- lib/puppet/node/environment.rb | 2 +- lib/puppet/provider/package/gem.rb | 2 +- lib/puppet/provider/package/pkgutil.rb | 2 +- lib/puppet/provider/package/puppet_gem.rb | 4 ++-- .../provider/package/puppetserver_gem.rb | 2 +- lib/puppet/util.rb | 8 ++++---- lib/puppet/util/run_mode.rb | 2 +- lib/puppet/util/windows/process.rb | 2 +- spec/unit/defaults_spec.rb | 4 ++-- spec/unit/provider/package/puppet_gem_spec.rb | 7 +++---- spec/unit/util_spec.rb | 20 +++++++++---------- util/rspec_grouper | 2 +- 18 files changed, 41 insertions(+), 39 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index cffda3497fa..5cdb5d2f190 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -268,3 +268,6 @@ Style/EvenOdd: Style/ExpandPathArguments: Enabled: true + +Style/FetchEnvVar: + Enabled: true diff --git a/ext/windows/service/daemon.rb b/ext/windows/service/daemon.rb index ecf429c9dd6..c0e76f89a86 100755 --- a/ext/windows/service/daemon.rb +++ b/ext/windows/service/daemon.rb @@ -18,7 +18,7 @@ class WindowsDaemon < Puppet::Util::Windows::Daemon @run_thread = nil @LOG_TO_FILE = false @loglevel = 0 - LOG_FILE = File.expand_path(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log')) + LOG_FILE = File.expand_path(File.join(ENV.fetch('ALLUSERSPROFILE', nil), 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log')) LEVELS = [:debug, :info, :notice, :warning, :err, :alert, :emerg, :crit] LEVELS.each do |level| define_method("log_#{level}") do |msg| @@ -203,10 +203,10 @@ def load_env(base_dir) ENV['Path'] = [ File.join(base_dir, 'puppet', 'bin'), File.join(base_dir, 'bin'), - ].join(';').tr('/', '\\') + ';' + ENV['Path'] + ].join(';').tr('/', '\\') + ';' + ENV.fetch('Path', nil) # ENV that uses forward slashes - ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV['RUBYLIB']}" + ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV.fetch('RUBYLIB', nil)}" rescue => e log_exception(e) end diff --git a/lib/puppet/application/resource.rb b/lib/puppet/application/resource.rb index cf1b58aefcc..80a54d09946 100644 --- a/lib/puppet/application/resource.rb +++ b/lib/puppet/application/resource.rb @@ -188,7 +188,7 @@ def handle_editing(text) tmpfile.puts text # edit the content - system(ENV["EDITOR"] || 'vi', tmpfile.path) + system(ENV.fetch("EDITOR", nil) || 'vi', tmpfile.path) # ...and, now, pass that file to puppet to apply. Because # many editors rename or replace the original file we need to diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index 85a563fe3c9..da285ab1473 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -49,7 +49,7 @@ def self.default_cadir def self.default_basemodulepath if Puppet::Util::Platform.windows? path = ['$codedir/modules'] - installdir = ENV["FACTER_env_windows_installdir"] + installdir = ENV.fetch("FACTER_env_windows_installdir", nil) if installdir path << "#{installdir}/puppet/modules" end @@ -61,7 +61,7 @@ def self.default_basemodulepath def self.default_vendormoduledir if Puppet::Util::Platform.windows? - installdir = ENV["FACTER_env_windows_installdir"] + installdir = ENV.fetch("FACTER_env_windows_installdir", nil) if installdir "#{installdir}\\puppet\\vendor_modules" else @@ -373,7 +373,7 @@ def self.initialize_default_settings!(settings) Puppet::Util::Platform.default_paths.each do |path| next if paths.include?(path) - ENV['PATH'] = ENV['PATH'] + File::PATH_SEPARATOR + path + ENV['PATH'] = ENV.fetch('PATH', nil) + File::PATH_SEPARATOR + path end value end diff --git a/lib/puppet/file_system/uniquefile.rb b/lib/puppet/file_system/uniquefile.rb index 1fdd797ba2d..fc2a1a1ca42 100644 --- a/lib/puppet/file_system/uniquefile.rb +++ b/lib/puppet/file_system/uniquefile.rb @@ -153,7 +153,7 @@ def try_convert_to_hash(h) def tmpdir tmp = '.' - for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp'] + for dir in [ENV.fetch('TMPDIR', nil), ENV.fetch('TMP', nil), ENV.fetch('TEMP', nil), @@systmpdir, '/tmp'] stat = File.stat(dir) if dir if stat && stat.directory? && stat.writable? tmp = dir diff --git a/lib/puppet/http/proxy.rb b/lib/puppet/http/proxy.rb index 88d315f1185..666b12b2f1b 100644 --- a/lib/puppet/http/proxy.rb +++ b/lib/puppet/http/proxy.rb @@ -18,7 +18,7 @@ def self.proxy(uri) def self.http_proxy_env # Returns a URI object if proxy is set, or nil - proxy_env = ENV["http_proxy"] || ENV["HTTP_PROXY"] + proxy_env = ENV.fetch("http_proxy", nil) || ENV.fetch("HTTP_PROXY", nil) begin return URI.parse(proxy_env) if proxy_env rescue URI::InvalidURIError @@ -124,7 +124,7 @@ def self.http_proxy_password end def self.no_proxy - no_proxy_env = ENV["no_proxy"] || ENV["NO_PROXY"] + no_proxy_env = ENV.fetch("no_proxy", nil) || ENV.fetch("NO_PROXY", nil) if no_proxy_env return no_proxy_env diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb index e8e08b89042..904214518e8 100644 --- a/lib/puppet/node/environment.rb +++ b/lib/puppet/node/environment.rb @@ -554,7 +554,7 @@ def hash # not private so it can be called in tests def self.extralibs if ENV['PUPPETLIB'] - split_path(ENV['PUPPETLIB']) + split_path(ENV.fetch('PUPPETLIB', nil)) else [] end diff --git a/lib/puppet/provider/package/gem.rb b/lib/puppet/provider/package/gem.rb index fe5983bdf17..e0f40c9b152 100644 --- a/lib/puppet/provider/package/gem.rb +++ b/lib/puppet/provider/package/gem.rb @@ -77,7 +77,7 @@ def self.execute_gem_command(command, command_options, custom_environment = {}) validate_command(command) cmd = [command] << command_options - custom_environment = { 'HOME' => ENV['HOME'] }.merge(custom_environment) + custom_environment = { 'HOME' => ENV.fetch('HOME', nil) }.merge(custom_environment) if Puppet::Util::Platform.windows? custom_environment[:PATH] = windows_path_without_puppet_bin diff --git a/lib/puppet/provider/package/pkgutil.rb b/lib/puppet/provider/package/pkgutil.rb index 85a1e9e66b8..6e09674422c 100644 --- a/lib/puppet/provider/package/pkgutil.rb +++ b/lib/puppet/provider/package/pkgutil.rb @@ -12,7 +12,7 @@ confine 'os.family' => :solaris has_command(:pkguti, pkgutil_bin) do - environment :HOME => ENV['HOME'] + environment :HOME => ENV.fetch('HOME', nil) end def self.healthcheck diff --git a/lib/puppet/provider/package/puppet_gem.rb b/lib/puppet/provider/package/puppet_gem.rb index bdaea0b8312..3adde08a2d9 100644 --- a/lib/puppet/provider/package/puppet_gem.rb +++ b/lib/puppet/provider/package/puppet_gem.rb @@ -9,9 +9,9 @@ confine :true => Puppet.runtime[:facter].value(:aio_agent_version) def self.windows_gemcmd - puppet_dir = ENV['PUPPET_DIR'] + puppet_dir = ENV.fetch('PUPPET_DIR', nil) if puppet_dir - File.join(ENV['PUPPET_DIR'].to_s, 'bin', 'gem.bat') + File.join(puppet_dir.to_s, 'bin', 'gem.bat') else File.join(Gem.default_bindir, 'gem.bat') end diff --git a/lib/puppet/provider/package/puppetserver_gem.rb b/lib/puppet/provider/package/puppetserver_gem.rb index 0e8b5aca431..88e30c32af9 100644 --- a/lib/puppet/provider/package/puppetserver_gem.rb +++ b/lib/puppet/provider/package/puppetserver_gem.rb @@ -33,7 +33,7 @@ def self.provider_command # CommandDefiner in provider.rb will set failonfail, combine, and environment. has_command(:puppetservercmd, '/opt/puppetlabs/bin/puppetserver') do - environment(HOME: ENV['HOME']) + environment(HOME: ENV.fetch('HOME', nil)) end def self.gemlist(options) diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index 8ca8f362a9b..70ccbbaa5be 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -44,7 +44,7 @@ def create_erb(content) # @deprecated Use ENV instead # @api private def get_env(name, mode = default_env) - ENV[name] + ENV.fetch(name, nil) end module_function :get_env @@ -208,16 +208,16 @@ def which(bin) if absolute_path?(bin) return bin if FileTest.file? bin and FileTest.executable? bin else - exts = ENV['PATHEXT'] + exts = ENV.fetch('PATHEXT', nil) exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD] - ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir| + ENV.fetch('PATH').split(File::PATH_SEPARATOR).each do |dir| begin dest = File.expand_path(File.join(dir, bin)) rescue ArgumentError => e # if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get # an ArgumentError here. Let's check to see if that is the case; if not, re-raise whatever error # was thrown. - if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "") + if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV.fetch('HOME', nil) == "") # if we get here they have a tilde in their PATH. We'll issue a single warning about this and then # ignore this path element and carry on with our lives. # TRANSLATORS PATH and HOME are environment variables and should not be translated diff --git a/lib/puppet/util/run_mode.rb b/lib/puppet/util/run_mode.rb index 00179eb805a..32e6cd4b731 100644 --- a/lib/puppet/util/run_mode.rb +++ b/lib/puppet/util/run_mode.rb @@ -117,7 +117,7 @@ def log_dir private def windows_common_base(*extra) - [ENV['ALLUSERSPROFILE'], "PuppetLabs"] + extra + [ENV.fetch('ALLUSERSPROFILE', nil), "PuppetLabs"] + extra end end end diff --git a/lib/puppet/util/windows/process.rb b/lib/puppet/util/windows/process.rb index 45a8dc56151..9a871a042c1 100644 --- a/lib/puppet/util/windows/process.rb +++ b/lib/puppet/util/windows/process.rb @@ -54,7 +54,7 @@ def wait_process(handle) # and since it's read-only, we can't set it. But we can execute a # a shell that simply returns the desired exit status, which has the # desired effect. - %x{#{ENV['COMSPEC']} /c exit #{exit_status}} + %x{#{ENV.fetch('COMSPEC', nil)} /c exit #{exit_status}} end exit_status diff --git a/spec/unit/defaults_spec.rb b/spec/unit/defaults_spec.rb index de1569cad18..9f322b38ec9 100644 --- a/spec/unit/defaults_spec.rb +++ b/spec/unit/defaults_spec.rb @@ -101,7 +101,7 @@ let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' } it 'includes user and system modules' do - allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir) + allow(ENV).to receive(:fetch).with("FACTER_env_windows_installdir", anything).and_return(installdir) expect( Puppet.default_basemodulepath @@ -129,7 +129,7 @@ let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' } it 'includes the default vendormoduledir' do - allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir) + allow(ENV).to receive(:fetch).with("FACTER_env_windows_installdir", anything).and_return(installdir) expect( Puppet.default_vendormoduledir diff --git a/spec/unit/provider/package/puppet_gem_spec.rb b/spec/unit/provider/package/puppet_gem_spec.rb index 1ebca520048..ea95d7aec1a 100644 --- a/spec/unit/provider/package/puppet_gem_spec.rb +++ b/spec/unit/provider/package/puppet_gem_spec.rb @@ -35,8 +35,8 @@ describe '.windows_gemcmd' do context 'when PUPPET_DIR is not set' do before do - allow(ENV).to receive(:[]).and_call_original - allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return(nil) + # allow(ENV).to receive(:fetch, anything).and_call_original + allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil) allow(Gem).to receive(:default_bindir).and_return('default_gem_bin') end @@ -48,8 +48,7 @@ context 'when PUPPET_DIR is set' do before do - allow(ENV).to receive(:[]).and_call_original - allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return('puppet_dir') + allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir') end it 'uses Gem.default_bindir' do diff --git a/spec/unit/util_spec.rb b/spec/unit/util_spec.rb index 3014e7aa1dd..192f13bfb96 100644 --- a/spec/unit/util_spec.rb +++ b/spec/unit/util_spec.rb @@ -706,8 +706,8 @@ def withenv_utf8(&block) end it "should walk the search PATH returning the first executable" do - allow(ENV).to receive(:[]).with('PATH').and_return(File.expand_path('/bin')) - allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil) + allow(ENV).to receive(:fetch).with('PATH').and_return(File.expand_path('/bin')) + allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(nil) expect(Puppet::Util.which('foo')).to eq(path) end @@ -723,8 +723,8 @@ def withenv_utf8(&block) describe "when a file extension is specified" do it "should walk each directory in PATH ignoring PATHEXT" do - allow(ENV).to receive(:[]).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR)) - allow(ENV).to receive(:[]).with('PATHEXT').and_return('.FOOBAR') + allow(ENV).to receive(:fetch).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR)) + allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return('.FOOBAR') expect(FileTest).to receive(:file?).with(File.join(File.expand_path('/bar'), 'foo.CMD')).and_return(false) @@ -735,8 +735,8 @@ def withenv_utf8(&block) describe "when a file extension is not specified" do it "should walk each extension in PATHEXT until an executable is found" do bar = File.expand_path('/bar') - allow(ENV).to receive(:[]).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}") - allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE#{File::PATH_SEPARATOR}.CMD") + allow(ENV).to receive(:fetch).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}") + allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(".EXE#{File::PATH_SEPARATOR}.CMD") expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.EXE')).and_return(false) expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.CMD')).and_return(false) @@ -747,8 +747,8 @@ def withenv_utf8(&block) end it "should walk the default extension path if the environment variable is not defined" do - allow(ENV).to receive(:[]).with('PATH').and_return(base) - allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil) + allow(ENV).to receive(:fetch).with('PATH').and_return(base) + allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(nil) %w[.COM .EXE .BAT].each do |ext| expect(FileTest).to receive(:file?).ordered().with(File.join(base, "foo#{ext}")).and_return(false) @@ -759,8 +759,8 @@ def withenv_utf8(&block) end it "should fall back if no extension matches" do - allow(ENV).to receive(:[]).with('PATH').and_return(base) - allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE") + allow(ENV).to receive(:fetch).with('PATH').and_return(base) + allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(".EXE") allow(FileTest).to receive(:file?).with(File.join(base, 'foo.EXE')).and_return(false) allow(FileTest).to receive(:file?).with(File.join(base, 'foo')).and_return(true) diff --git a/util/rspec_grouper b/util/rspec_grouper index 756ce61a13d..41f33a2e6fd 100755 --- a/util/rspec_grouper +++ b/util/rspec_grouper @@ -20,7 +20,7 @@ module Parallel def initialize(group_size) config = ::RSpec::Core::Configuration.new - options = ::RSpec::Core::ConfigurationOptions.new((ENV['TEST'] || ENV['TESTS'] || 'spec').split(';')) + options = ::RSpec::Core::ConfigurationOptions.new((ENV.fetch('TEST', nil) || ENV.fetch('TESTS', nil) || 'spec').split(';')) options.configure config # This will scan and load all spec examples