Skip to content

Commit 50cff16

Browse files
authored
Merge pull request #182 from bastelfreak/rubocop4
rubocop: Fix Layout cops
2 parents a0eb4d5 + 453848c commit 50cff16

File tree

6 files changed

+216
-169
lines changed

6 files changed

+216
-169
lines changed

.rubocop.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ inherit_from: .rubocop_todo.yml
44
inherit_gem:
55
voxpupuli-rubocop: rubocop.yml
66

7-
Style:
8-
Enabled: false
9-
10-
Layout:
7+
Layout/LineLength:
118
Enabled: false
129

1310
# To match the gem name

.rubocop_todo.yml

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-06-09 13:09:14 UTC using RuboCop version 1.63.5.
3+
# on 2024-06-09 17:48:43 UTC using RuboCop version 1.63.5.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -52,7 +52,7 @@ RSpec/MessageSpies:
5252
RSpec/MultipleExpectations:
5353
Max: 2
5454

55-
# Offense count: 49
55+
# Offense count: 40
5656
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
5757
# SupportedStyles: always, named_only
5858
RSpec/NamedSubject:
@@ -74,3 +74,60 @@ RSpec/StubbedMock:
7474
Rake/Desc:
7575
Exclude:
7676
- 'Rakefile'
77+
78+
# Offense count: 6
79+
# This cop supports unsafe autocorrection (--autocorrect-all).
80+
# Configuration parameters: EnforcedStyle.
81+
# SupportedStyles: always, always_true, never
82+
Style/FrozenStringLiteralComment:
83+
Exclude:
84+
- 'Gemfile'
85+
- 'Rakefile'
86+
- 'lib/rspec-puppet-facts.rb'
87+
- 'lib/rspec-puppet-facts/version.rb'
88+
- 'spec/rspec_puppet_facts_spec.rb'
89+
- 'spec/spec_helper.rb'
90+
91+
# Offense count: 1
92+
Style/MixinUsage:
93+
Exclude:
94+
- 'spec/spec_helper.rb'
95+
96+
# Offense count: 3
97+
# This cop supports unsafe autocorrection (--autocorrect-all).
98+
# Configuration parameters: EnforcedStyle.
99+
# SupportedStyles: literals, strict
100+
Style/MutableConstant:
101+
Exclude:
102+
- 'lib/rspec-puppet-facts.rb'
103+
- 'lib/rspec-puppet-facts/version.rb'
104+
- 'spec/rspec_puppet_facts_spec.rb'
105+
106+
# Offense count: 1
107+
# This cop supports unsafe autocorrection (--autocorrect-all).
108+
# Configuration parameters: Methods.
109+
Style/RedundantArgument:
110+
Exclude:
111+
- 'lib/rspec-puppet-facts.rb'
112+
113+
# Offense count: 1
114+
# This cop supports unsafe autocorrection (--autocorrect-all).
115+
Style/RedundantSort:
116+
Exclude:
117+
- 'lib/rspec-puppet-facts.rb'
118+
119+
# Offense count: 2
120+
# This cop supports unsafe autocorrection (--autocorrect-all).
121+
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
122+
# AllowedMethods: present?, blank?, presence, try, try!
123+
Style/SafeNavigation:
124+
Exclude:
125+
- 'lib/rspec-puppet-facts.rb'
126+
127+
# Offense count: 2
128+
# This cop supports unsafe autocorrection (--autocorrect-all).
129+
# Configuration parameters: RequireEnglish, EnforcedStyle.
130+
# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names
131+
Style/SpecialGlobalVars:
132+
Exclude:
133+
- 'lib/rspec-puppet-facts.rb'

Gemfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ source ENV['GEM_SOURCE'] || 'https://rubygems.org'
22

33
gemspec
44

5-
gem 'facter', ENV['FACTER_GEM_VERSION'], :require => false
5+
gem 'facter', ENV.fetch('FACTER_GEM_VERSION', nil), require: false
66

77
group :release do
88
gem 'faraday-retry', '~> 2.1', require: false
99
gem 'github_changelog_generator', '~> 1.16.4', require: false
1010
end
1111

12-
group :coverage, optional: ENV['COVERAGE']!='yes' do
13-
gem 'codecov', :require => false
14-
gem 'simplecov-console', :require => false
12+
group :coverage, optional: ENV['COVERAGE'] != 'yes' do
13+
gem 'codecov', require: false
14+
gem 'simplecov-console', require: false
1515
end

Rakefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
PUPPET_VERSIONS_PATH = File.join(__dir__, 'ext', 'puppet_agent_components.json')
22

33
begin
4-
require 'rspec/core/rake_task'
5-
require 'yard'
6-
RSpec::Core::RakeTask.new(:spec)
7-
YARD::Rake::YardocTask.new
4+
require 'rspec/core/rake_task'
5+
require 'yard'
6+
RSpec::Core::RakeTask.new(:spec)
7+
YARD::Rake::YardocTask.new
88
rescue LoadError
99
# yard is optional
1010
end
@@ -40,8 +40,8 @@ namespace :puppet_versions do
4040

4141
output = `git status --porcelain #{PUPPET_VERSIONS_PATH}`
4242
unless output.strip.empty?
43-
$stderr.puts "#{PUPPET_VERSIONS_PATH} is out of date."
44-
$stderr.puts 'Run the puppet_versions:update task to update it and commit the changes.'
43+
warn "#{PUPPET_VERSIONS_PATH} is out of date."
44+
warn 'Run the puppet_versions:update task to update it and commit the changes.'
4545
raise
4646
end
4747
end
@@ -54,7 +54,7 @@ rescue LoadError
5454
else
5555
require 'rubygems'
5656
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
57-
config.exclude_labels = %w{duplicate question invalid wontfix wont-fix skip-changelog github_actions}
57+
config.exclude_labels = %w[duplicate question invalid wontfix wont-fix skip-changelog github_actions]
5858
config.user = 'voxpupuli'
5959
config.project = 'rspec-puppet-facts'
6060
gem_version = Gem::Specification.load("#{config.project}.gemspec").version

lib/rspec-puppet-facts.rb

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ def on_supported_os(opts = {})
5858
# @api private
5959
def on_supported_os_implementation(opts = {})
6060
unless /\A\d+\.\d+(?:\.\d+)*\z/.match?((facterversion = opts[:facterversion]))
61-
raise ArgumentError, ":facterversion must be in the format 'n.n' or " \
62-
"'n.n.n' (n is numeric), not '#{facterversion}'"
61+
raise ArgumentError, ":facterversion must be in the format 'n.n' or 'n.n.n' (n is numeric), not '#{facterversion}'"
6362
end
6463

6564
filter = []
6665
opts[:supported_os].map do |os_sup|
6766
if os_sup['operatingsystemrelease']
6867
Array(os_sup['operatingsystemrelease']).map do |operatingsystemmajrelease|
6968
opts[:hardwaremodels].each do |hardwaremodel|
70-
7169
os_release_filter = "/^#{Regexp.escape(operatingsystemmajrelease.split(' ')[0])}/"
7270
case os_sup['operatingsystem']
7371
when /BSD/i
@@ -90,7 +88,7 @@ def on_supported_os_implementation(opts = {})
9088
os_release_filter = "\"#{operatingsystemmajrelease}\""
9189
when /Amazon/i
9290
# Tighten the regex for Amazon Linux 2 so that we don't pick up Amazon Linux 2016 or 2017 facts
93-
os_release_filter = "/^2$/" if operatingsystemmajrelease == '2'
91+
os_release_filter = '/^2$/' if operatingsystemmajrelease == '2'
9492
end
9593

9694
filter << {
@@ -110,26 +108,26 @@ def on_supported_os_implementation(opts = {})
110108
end
111109
end
112110

113-
strict_requirement = RspecPuppetFacts::facter_version_to_strict_requirement(facterversion)
111+
strict_requirement = RspecPuppetFacts.facter_version_to_strict_requirement(facterversion)
114112

115-
loose_requirement = RspecPuppetFacts::facter_version_to_loose_requirement(facterversion)
113+
loose_requirement = RspecPuppetFacts.facter_version_to_loose_requirement(facterversion)
116114
received_facts = []
117115

118116
# FacterDB may have newer versions of facter data for which it contains a subset of all possible
119117
# facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to
120118
# cycle through and downgrade Facter versions per platform type until we find matching Facter data.
121119
filter.each do |filter_spec|
122-
versions = FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h { |facts| [Gem::Version.new(facts[:facterversion]), facts] }
120+
versions = FacterDB.get_facts(filter_spec, symbolize_keys: !RSpec.configuration.facterdb_string_keys).to_h do |facts|
121+
[Gem::Version.new(facts[:facterversion]), facts]
122+
end
123123

124124
version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }
125125

126126
unless version
127127
version, facts = versions.select { |v, _f| loose_requirement =~ v }.max_by { |v, _f| v } if loose_requirement
128128
next unless version
129129

130-
if RspecPuppetFacts.spec_facts_strict?
131-
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, aborting"
132-
end
130+
raise ArgumentError, "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, aborting" if RspecPuppetFacts.spec_facts_strict?
133131

134132
RspecPuppetFacts.warning "No facts were found in the FacterDB for Facter v#{facterversion} on #{filter_spec}, using v#{version} instead"
135133
end
@@ -152,6 +150,7 @@ def on_supported_os_implementation(opts = {})
152150

153151
os = "#{os_fact['name'].downcase}-#{os_fact['release']['major']}-#{os_fact['hardware']}"
154152
next if RspecPuppetFacts.spec_facts_os_filter && !os.start_with?(RspecPuppetFacts.spec_facts_os_filter)
153+
155154
facts.merge! RspecPuppetFacts.common_facts
156155
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
157156
end
@@ -193,7 +192,7 @@ def add_custom_fact(name, value, options = {})
193192
def self.register_custom_fact(name, value, options)
194193
@custom_facts ||= {}
195194
name = RSpec.configuration.facterdb_string_keys ? name.to_s : name.to_sym
196-
@custom_facts[name] = {:options => options, :value => value}
195+
@custom_facts[name] = { options: options, value: value }
197196
end
198197

199198
# Adds any custom facts according to the rules defined for the operating
@@ -212,7 +211,7 @@ def self.with_custom_facts(os, facts)
212211
value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
213212
# if merge_facts passed, merge supplied facts into facts hash
214213
if fact[:options][:merge_facts]
215-
facts.deep_merge!({name => value})
214+
facts.deep_merge!({ name => value })
216215
else
217216
facts[name] = value
218217
end
@@ -235,7 +234,7 @@ def self.custom_facts
235234
# @return [nil,String]
236235
# @api private
237236
def self.spec_facts_os_filter
238-
ENV['SPEC_FACTS_OS']
237+
ENV.fetch('SPEC_FACTS_OS', nil)
239238
end
240239

241240
# If SPEC_FACTS_STRICT is set to `yes`, RspecPuppetFacts will error on missing FacterDB entries, instead of warning & skipping the tests, or using an older facter version.
@@ -251,17 +250,16 @@ def self.spec_facts_strict?
251250
# @return [Hash <Symbol => String>]
252251
def self.common_facts
253252
return @common_facts if @common_facts
253+
254254
@common_facts = {
255-
:puppetversion => Puppet.version,
256-
:rubysitedir => RbConfig::CONFIG['sitelibdir'],
257-
:rubyversion => RUBY_VERSION,
255+
puppetversion: Puppet.version,
256+
rubysitedir: RbConfig::CONFIG['sitelibdir'],
257+
rubyversion: RUBY_VERSION,
258258
}
259259

260260
@common_facts[:mco_version] = MCollective::VERSION if mcollective?
261261

262-
if augeas?
263-
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
264-
end
262+
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version') if augeas?
265263
@common_facts = stringify_keys(@common_facts) if RSpec.configuration.facterdb_string_keys
266264

267265
@common_facts
@@ -298,9 +296,8 @@ def self.mcollective?
298296
# @return [Array<Hash>]
299297
# @api private
300298
def self.meta_supported_os
301-
unless metadata['operatingsystem_support'].is_a? Array
302-
fail StandardError, 'Unknown operatingsystem support in the metadata file!'
303-
end
299+
raise StandardError, 'Unknown operatingsystem support in the metadata file!' unless metadata['operatingsystem_support'].is_a? Array
300+
304301
metadata['operatingsystem_support']
305302
end
306303

@@ -311,9 +308,8 @@ def self.meta_supported_os
311308
# @api private
312309
def self.metadata
313310
return @metadata if @metadata
314-
unless File.file? metadata_file
315-
fail StandardError, "Can't find metadata.json... dunno why"
316-
end
311+
raise StandardError, "Can't find metadata.json... dunno why" unless File.file? metadata_file
312+
317313
content = File.read metadata_file
318314
@metadata = JSON.parse content
319315
end
@@ -329,7 +325,7 @@ def self.metadata_file
329325
# @param message [String]
330326
# @api private
331327
def self.warning(message)
332-
STDERR.puts message
328+
warn message
333329
end
334330

335331
# Reset the memoization
@@ -379,9 +375,6 @@ def self.facter_version_to_loose_requirement_string(version)
379375
elsif /\A[0-9]+\Z/.match?(version)
380376
# Interpret 3 as < 4
381377
"< #{version.to_i + 1}"
382-
else
383-
# This would be the same as the strict requirement
384-
nil
385378
end
386379
end
387380

@@ -397,13 +390,13 @@ def self.facter_version_for_puppet_version(puppet_version)
397390
fd = File.open(json_path, 'rb:UTF-8')
398391
data = JSON.parse(fd.read)
399392

400-
version_map = data.map { |_, versions|
393+
version_map = data.map do |_, versions|
401394
if versions['puppet'].nil? || versions['facter'].nil?
402395
nil
403396
else
404397
[Gem::Version.new(versions['puppet']), versions['facter']]
405398
end
406-
}.compact
399+
end.compact
407400

408401
puppet_gem_version = Gem::Version.new(puppet_version)
409402
applicable_versions = version_map.select { |p, _| puppet_gem_version >= p }
@@ -422,8 +415,6 @@ def self.facter_version_for_puppet_version(puppet_version)
422415
end
423416

424417
RSpec.configure do |c|
425-
c.add_setting :default_facter_version,
426-
default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
427-
c.add_setting :facterdb_string_keys,
428-
default: false
418+
c.add_setting :default_facter_version, default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
419+
c.add_setting :facterdb_string_keys, default: false
429420
end

0 commit comments

Comments
 (0)