Skip to content

Commit 3846ac0

Browse files
author
jordanbreen28
committed
(maint) - rubocop fixes
1 parent fb7fd8a commit 3846ac0

35 files changed

+401
-211
lines changed

.rubocop.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
inherit_from: .rubocop_todo.yml
3+
24
require: rubocop-rspec
35
AllCops:
46
TargetRubyVersion: '2.5'
@@ -12,7 +14,7 @@ AllCops:
1214
- pkg/**/*
1315
- spec/fixtures/**/*
1416
- vendor/**/*
15-
Metrics/LineLength:
17+
Layout/LineLength:
1618
Description: People have wide screens, use them.
1719
Max: 200
1820
RSpec/BeforeAfterAll:
@@ -34,8 +36,7 @@ Style/ClassAndModuleChildren:
3436
Description: Compact style reduces the required amount of indentation.
3537
EnforcedStyle: compact
3638
# this needs to be easily usable in the gemspec
37-
Exclude:
38-
- 'lib/puppet/resource_api/version.rb'
39+
Enabled: false
3940
Style/EmptyElse:
4041
Description: Enforce against empty else clauses, but allow `nil` for clarity.
4142
EnforcedStyle: empty
@@ -121,13 +122,9 @@ Lint/NestedMethodDefinition:
121122
RSpec/MessageSpies:
122123
Enabled: false
123124

124-
# This cop causes issues on our current version of Rubocop (0.57.2) and newer (>= 3.0) Rubies because of keyword arguments.
125-
Style/BracesAroundHashParameters:
126-
Enabled: false
127-
128125
# requires 2.3's squiggly HEREDOC support, which we can't use, yet
129126
# see http://www.virtuouscode.com/2016/01/06/about-the-ruby-squiggly-heredoc-syntax/
130-
Layout/IndentHeredoc:
127+
Layout/HeredocIndentation:
131128
Enabled: false
132129

133130
# Updated in 0.53 (or thereabouts)
@@ -141,7 +138,7 @@ Style/TrailingCommaInHashLiteral:
141138
EnforcedStyleForMultiline: comma
142139

143140
# Add allowing "is", as it is a technical term in puppet
144-
Naming/UncommunicativeMethodParamName:
141+
Naming/MethodParameterName:
145142
Description: Checks for method parameter names that contain capital letters, end in
146143
numbers, or do not meet a minimal length.
147144
Enabled: true

.rubocop_todo.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2023-12-13 15:50:43 UTC using RuboCop version 1.48.1.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 6
10+
# Configuration parameters: AllowedMethods.
11+
# AllowedMethods: enums
12+
Lint/ConstantDefinitionInBlock:
13+
Exclude:
14+
- 'spec/puppet/resource_api/base_context_spec.rb'
15+
- 'spec/puppet/resource_api/transport/wrapper_spec.rb'
16+
- 'spec/puppet/resource_api/transport_spec.rb'
17+
- 'spec/puppet/resource_api_spec.rb'
18+
19+
# Offense count: 1
20+
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
21+
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
22+
Naming/MemoizedInstanceVariableName:
23+
Exclude:
24+
- 'lib/puppet/resource_api/type_definition.rb'
25+
26+
# Offense count: 1
27+
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
28+
# SupportedStyles: snake_case, normalcase, non_integer
29+
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
30+
Naming/VariableNumber:
31+
Exclude:
32+
- 'spec/puppet/resource_api_spec.rb'
33+
34+
# Offense count: 1
35+
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
36+
# Include: **/*_spec*rb*, **/spec/**/*
37+
RSpec/FilePath:
38+
Exclude:
39+
- 'spec/acceptance/custom_insync_spec_hidden_property.rb'
40+
41+
# Offense count: 18
42+
# Configuration parameters: AssignmentOnly.
43+
RSpec/InstanceVariable:
44+
Exclude:
45+
- 'spec/acceptance/namevar_spec.rb'
46+
- 'spec/puppet/resource_api/data_type_handling_spec.rb'
47+
48+
# Offense count: 15
49+
RSpec/LeakyConstantDeclaration:
50+
Exclude:
51+
- 'spec/puppet/resource_api/base_context_spec.rb'
52+
- 'spec/puppet/resource_api/transport/wrapper_spec.rb'
53+
- 'spec/puppet/resource_api/transport_spec.rb'
54+
- 'spec/puppet/resource_api_spec.rb'
55+
56+
# Offense count: 126
57+
# Configuration parameters: AllowSubject.
58+
RSpec/MultipleMemoizedHelpers:
59+
Max: 14
60+
61+
# Offense count: 2
62+
RSpec/RepeatedExampleGroupDescription:
63+
Exclude:
64+
- 'spec/puppet/resource_api_spec.rb'
65+
66+
# Offense count: 25
67+
RSpec/StubbedMock:
68+
Exclude:
69+
- 'spec/puppet/resource_api/property_spec.rb'
70+
- 'spec/puppet/resource_api/transport/wrapper_spec.rb'
71+
- 'spec/puppet/resource_api/transport_spec.rb'
72+
- 'spec/puppet/resource_api_spec.rb'
73+
74+
# Offense count: 2
75+
# Configuration parameters: AllowedMethods.
76+
# AllowedMethods: respond_to_missing?
77+
Style/OptionalBooleanParameter:
78+
Exclude:
79+
- 'lib/puppet/resource_api/data_type_handling.rb'

lib/puppet/resource_api.rb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def rsapi_canonicalized_target_state
139139
@rsapi_canonicalized_target_state ||= begin
140140
# skip puppet's injected metaparams
141141
actual_params = @parameters.select { |k, _v| type_definition.attributes.key? k }
142-
target_state = Hash[actual_params.map { |k, v| [k, v.rs_value] }]
142+
target_state = actual_params.transform_values { |v| v.rs_value }
143143
target_state = my_provider.canonicalize(context, [target_state]).first if type_definition.feature?('canonicalize')
144144
target_state
145145
end
@@ -150,21 +150,23 @@ def rsapi_canonicalized_target_state
150150
def generate
151151
# If feature `custom_generate` has been set then call the generate function within the provider and return the given results
152152
return unless type_definition&.feature?('custom_generate')
153+
153154
should_hash = rsapi_canonicalized_target_state
154155
is_hash = rsapi_current_state
155156
title = rsapi_title
156157

157158
# Ensure that a custom `generate` method has been created within the provider
158159
raise(Puppet::DevError, 'No generate method found within the types provider') unless my_provider.respond_to?(:generate)
160+
159161
# Call the providers custom `generate` method
160-
rules_resources = my_provider.generate(context, title, is_hash, should_hash)
162+
my_provider.generate(context, title, is_hash, should_hash)
161163

162164
# Return array of resources
163-
rules_resources
164165
end
165166

166167
def rsapi_current_state
167168
return @rsapi_current_state if @rsapi_current_state
169+
168170
# If the current state is not set, then check the cache and, if a value is
169171
# found, ensure it passes strict_check before allowing it to be used:
170172
cached_value = rsapi_provider_get_cache.get(rsapi_title)
@@ -238,10 +240,8 @@ def to_resource_shim(resource)
238240
definition[:attributes].each do |name, options|
239241
# puts "#{name}: #{options.inspect}"
240242

241-
if options[:behaviour]
242-
unless [:read_only, :namevar, :parameter, :init_only].include? options[:behaviour]
243-
raise Puppet::ResourceError, "`#{options[:behaviour]}` is not a valid behaviour value"
244-
end
243+
if options[:behaviour] && !([:read_only, :namevar, :parameter, :init_only].include? options[:behaviour])
244+
raise Puppet::ResourceError, "`#{options[:behaviour]}` is not a valid behaviour value"
245245
end
246246

247247
# TODO: using newparam everywhere would suppress change reporting
@@ -365,6 +365,7 @@ def flush
365365
if Puppet.settings[:strict] != :off && rsapi_current_state && (rsapi_current_state[:ensure] == 'present' && target_state[:ensure] == 'present')
366366
target_state.each do |name, value|
367367
next unless type_definition.attributes[name][:behaviour] == :init_only && value != rsapi_current_state[name]
368+
368369
message = "Attempting to change `#{name}` init_only attribute value from `#{rsapi_current_state[name]}` to `#{value}`"
369370
case Puppet.settings[:strict]
370371
when :warning
@@ -390,12 +391,12 @@ def flush
390391
end
391392

392393
def raise_missing_attrs
393-
error_msg = "The following mandatory attributes were not provided:\n * " + @missing_attrs.join(", \n * ")
394+
error_msg = "The following mandatory attributes were not provided:\n * #{@missing_attrs.join(", \n * ")}"
394395
raise Puppet::ResourceError, error_msg if @missing_attrs.any? && (value(:ensure) != :absent && !value(:ensure).nil?)
395396
end
396397

397398
def raise_missing_params
398-
error_msg = "The following mandatory parameters were not provided:\n * " + @missing_params.join(", \n * ")
399+
error_msg = "The following mandatory parameters were not provided:\n * #{@missing_params.join(", \n * ")}"
399400
raise Puppet::ResourceError, error_msg
400401
end
401402

@@ -426,14 +427,14 @@ def strict_check_canonicalize(current_state)
426427
# compare the clone against the current state to see if changes have been made by canonicalize
427428
return unless state_clone && (current_state != state_clone)
428429

429-
#:nocov:
430+
# :nocov:
430431
# codecov fails to register this multiline as covered, even though simplecov does.
431432
message = <<MESSAGE.strip
432433
#{type_definition.name}[#{@title}]#get has not provided canonicalized values.
433434
Returned values: #{current_state.inspect}
434435
Canonicalized values: #{state_clone.inspect}
435436
MESSAGE
436-
#:nocov:
437+
# :nocov:
437438
strict_message(message)
438439
end
439440

@@ -448,6 +449,7 @@ def strict_check_title_parameter(current_state)
448449
self.class.title_patterns.each do |regexp, symbols|
449450
captures = regexp.match(current_state[:title])
450451
next if captures.nil?
452+
451453
symbols.zip(captures[1..-1]).each do |symbol_and_lambda, capture|
452454
# The Resource API does not support passing procs in title_patterns
453455
# so, unlike Puppet::Resource, we do not need to handle that here.
@@ -461,15 +463,15 @@ def strict_check_title_parameter(current_state)
461463

462464
namevars = type_definition.namevars.reject { |namevar| title_hash[namevar] == rsapi_title[namevar] }
463465

464-
#:nocov:
466+
# :nocov:
465467
# codecov fails to register this multiline as covered, even though simplecov does.
466468
message = <<MESSAGE.strip
467469
#{type_definition.name}[#{@title}]#get has provided a title attribute which does not match all namevars.
468470
Namevars which do not match: #{namevars.inspect}
469471
Returned parsed title hash: #{title_hash.inspect}
470472
Expected hash: #{rsapi_title.inspect}
471473
MESSAGE
472-
#:nocov:
474+
# :nocov:
473475
strict_message(message)
474476
end
475477

@@ -528,7 +530,7 @@ def self.parse_title_patterns(patterns)
528530
end
529531
end
530532
end
531-
module_function :register_type # rubocop:disable Style/AccessModifierDeclarations
533+
module_function :register_type
532534

533535
def load_provider(type_name)
534536
class_name = class_name_from_type_name(type_name)
@@ -557,14 +559,14 @@ def load_provider(type_name)
557559
raise Puppet::DevError, "provider class Puppet::Provider::#{class_name}::#{class_name} not found in puppet/provider/#{type_name}/#{type_name}"
558560
end
559561
end
560-
module_function :load_provider # rubocop:disable Style/AccessModifierDeclarations
562+
module_function :load_provider
561563

562564
def load_default_provider(class_name, type_name_sym)
563565
# loads the "puppet/provider/#{type_name}/#{type_name}" file through puppet
564566
Puppet::Type.type(type_name_sym).provider(type_name_sym)
565567
Puppet::Provider.const_get(class_name, false).const_get(class_name, false)
566568
end
567-
module_function :load_default_provider # rubocop:disable Style/AccessModifierDeclarations
569+
module_function :load_default_provider
568570

569571
def load_device_provider(class_name, type_name_sym, device_class_name, device_name_sym)
570572
# loads the "puppet/provider/#{type_name}/#{device_name}" file through puppet
@@ -576,13 +578,13 @@ def load_device_provider(class_name, type_name_sym, device_class_name, device_na
576578
load_default_provider(class_name, type_name_sym)
577579
end
578580
end
579-
module_function :load_device_provider # rubocop:disable Style/AccessModifierDeclarations
581+
module_function :load_device_provider
580582

581583
# keeps the existing register API format. e.g. Puppet::ResourceApi.register_type
582584
def register_transport(schema)
583585
Puppet::ResourceApi::Transport.register(schema)
584586
end
585-
module_function :register_transport # rubocop:disable Style/AccessModifierDeclarations
587+
module_function :register_transport
586588

587589
def self.class_name_from_type_name(type_name)
588590
type_name.to_s.split('_').map(&:capitalize).join

lib/puppet/resource_api/base_context.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def failing(titles, message: 'Failing')
9292

9393
def processing(title, is, should, message = 'Processing')
9494
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
95+
9596
start_time = Time.now
9697
setup_context(title, message)
9798
begin
@@ -114,11 +115,13 @@ def processing(title, is, should, message = 'Processing')
114115

115116
def processed(title, is, should)
116117
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
118+
117119
notice("Processed #{title} from #{is} to #{should}")
118120
end
119121

120122
def attribute_changed(title, attribute, is, should, message: nil)
121123
raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each)
124+
122125
printable_is = 'nil'
123126
printable_should = 'nil'
124127
if is
@@ -156,7 +159,7 @@ def send_log(_level, _message)
156159
private
157160

158161
def format_titles(titles)
159-
if titles.length.zero? && !titles.is_a?(String)
162+
if titles.empty? && !titles.is_a?(String)
160163
@type.name
161164
else
162165
"#{@type.name}[#{[titles].flatten.compact.join(', ')}]"
@@ -170,6 +173,7 @@ def setup_context(titles, message = nil)
170173

171174
def format_seconds(seconds)
172175
return '%.6f' % seconds if seconds < 1
176+
173177
'%.2f' % seconds
174178
end
175179
end

lib/puppet/resource_api/data_type_handling.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation,Style/ClassAndModuleChildren
3+
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation
44

55
# This module is used to handle data inside types, contains methods for munging
66
# and validation of the type values.
@@ -49,6 +49,7 @@ def self.mungify_core(type, value, error_msg_prefix, unpack_strings = false)
4949
# to 1 (an integer)
5050
cleaned_value, error_msg = try_mungify(type, value, error_msg_prefix)
5151
raise Puppet::ResourceError, error_msg if error_msg
52+
5253
cleaned_value
5354
elsif value == :false # rubocop:disable Lint/BooleanSymbol
5455
# work around https://tickets.puppetlabs.com/browse/PUP-2368
@@ -121,6 +122,7 @@ def self.try_mungify(type, value, error_msg_prefix)
121122

122123
error_msg = try_validate(type, value, error_msg_prefix)
123124
return [nil, error_msg] if error_msg # an error
125+
124126
[value, nil] # match
125127
end
126128

@@ -171,16 +173,16 @@ def self.try_validate(type, value, error_msg_prefix)
171173

172174
# an error :-(
173175
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
174-
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(
176+
Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(
175177
error_msg_prefix,
176178
type,
177179
inferred_type,
178180
)
179-
error_msg
180181
end
181182

182183
def self.validate_ensure(definition)
183184
return unless definition[:attributes].key? :ensure
185+
184186
options = definition[:attributes][:ensure]
185187
type = parse_puppet_type(:ensure, options[:type])
186188

lib/puppet/resource_api/glue.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'yaml'
44

55
module Puppet; end # rubocop:disable Style/Documentation
6+
67
module Puppet::ResourceApi
78
# A trivial class to provide the functionality required to push data through the existing type/provider parts of puppet
89
class ResourceShim
@@ -45,7 +46,7 @@ def to_hiera_hash
4546
# Convert our resource to yaml for Hiera purposes.
4647
def to_hierayaml
4748
attributes = Hash[filtered_keys.map { |k| [k.to_s, values[k]] }]
48-
YAML.dump('type' => { title => attributes }).split("\n").drop(2).join("\n") + "\n"
49+
"#{YAML.dump('type' => { title => attributes }).split("\n").drop(2).join("\n")}\n"
4950
end
5051

5152
def to_json(*)

lib/puppet/resource_api/parameter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'puppet/util'
44
require 'puppet/parameter'
55

6-
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation,Style/ClassAndModuleChildren
6+
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation
77

88
# Class containing parameter functionality for ResourceApi.
99
class Puppet::ResourceApi::Parameter < Puppet::Parameter

0 commit comments

Comments
 (0)