Skip to content

Commit

Permalink
Merge pull request #182 from bastelfreak/rubocop4
Browse files Browse the repository at this point in the history
rubocop: Fix Layout cops
  • Loading branch information
bastelfreak authored Jun 9, 2024
2 parents a0eb4d5 + 453848c commit 50cff16
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 169 deletions.
5 changes: 1 addition & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ inherit_from: .rubocop_todo.yml
inherit_gem:
voxpupuli-rubocop: rubocop.yml

Style:
Enabled: false

Layout:
Layout/LineLength:
Enabled: false

# To match the gem name
Expand Down
61 changes: 59 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-06-09 13:09:14 UTC using RuboCop version 1.63.5.
# on 2024-06-09 17:48:43 UTC using RuboCop version 1.63.5.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -52,7 +52,7 @@ RSpec/MessageSpies:
RSpec/MultipleExpectations:
Max: 2

# Offense count: 49
# Offense count: 40
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Expand All @@ -74,3 +74,60 @@ RSpec/StubbedMock:
Rake/Desc:
Exclude:
- 'Rakefile'

# Offense count: 6
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Exclude:
- 'Gemfile'
- 'Rakefile'
- 'lib/rspec-puppet-facts.rb'
- 'lib/rspec-puppet-facts/version.rb'
- 'spec/rspec_puppet_facts_spec.rb'
- 'spec/spec_helper.rb'

# Offense count: 1
Style/MixinUsage:
Exclude:
- 'spec/spec_helper.rb'

# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: literals, strict
Style/MutableConstant:
Exclude:
- 'lib/rspec-puppet-facts.rb'
- 'lib/rspec-puppet-facts/version.rb'
- 'spec/rspec_puppet_facts_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Methods.
Style/RedundantArgument:
Exclude:
- 'lib/rspec-puppet-facts.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/RedundantSort:
Exclude:
- 'lib/rspec-puppet-facts.rb'

# Offense count: 2
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
# AllowedMethods: present?, blank?, presence, try, try!
Style/SafeNavigation:
Exclude:
- 'lib/rspec-puppet-facts.rb'

# Offense count: 2
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: RequireEnglish, EnforcedStyle.
# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names
Style/SpecialGlobalVars:
Exclude:
- 'lib/rspec-puppet-facts.rb'
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ source ENV['GEM_SOURCE'] || 'https://rubygems.org'

gemspec

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

group :release do
gem 'faraday-retry', '~> 2.1', require: false
gem 'github_changelog_generator', '~> 1.16.4', require: false
end

group :coverage, optional: ENV['COVERAGE']!='yes' do
gem 'codecov', :require => false
gem 'simplecov-console', :require => false
group :coverage, optional: ENV['COVERAGE'] != 'yes' do
gem 'codecov', require: false
gem 'simplecov-console', require: false
end
14 changes: 7 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PUPPET_VERSIONS_PATH = File.join(__dir__, 'ext', 'puppet_agent_components.json')

begin
require 'rspec/core/rake_task'
require 'yard'
RSpec::Core::RakeTask.new(:spec)
YARD::Rake::YardocTask.new
require 'rspec/core/rake_task'
require 'yard'
RSpec::Core::RakeTask.new(:spec)
YARD::Rake::YardocTask.new
rescue LoadError
# yard is optional
end
Expand Down Expand Up @@ -40,8 +40,8 @@ namespace :puppet_versions do

output = `git status --porcelain #{PUPPET_VERSIONS_PATH}`
unless output.strip.empty?
$stderr.puts "#{PUPPET_VERSIONS_PATH} is out of date."
$stderr.puts 'Run the puppet_versions:update task to update it and commit the changes.'
warn "#{PUPPET_VERSIONS_PATH} is out of date."
warn 'Run the puppet_versions:update task to update it and commit the changes.'
raise
end
end
Expand All @@ -54,7 +54,7 @@ rescue LoadError
else
require 'rubygems'
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
config.exclude_labels = %w{duplicate question invalid wontfix wont-fix skip-changelog github_actions}
config.exclude_labels = %w[duplicate question invalid wontfix wont-fix skip-changelog github_actions]
config.user = 'voxpupuli'
config.project = 'rspec-puppet-facts'
gem_version = Gem::Specification.load("#{config.project}.gemspec").version
Expand Down
61 changes: 26 additions & 35 deletions lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ def on_supported_os(opts = {})
# @api private
def on_supported_os_implementation(opts = {})
unless /\A\d+\.\d+(?:\.\d+)*\z/.match?((facterversion = opts[:facterversion]))
raise ArgumentError, ":facterversion must be in the format 'n.n' or " \
"'n.n.n' (n is numeric), not '#{facterversion}'"
raise ArgumentError, ":facterversion must be in the format 'n.n' or 'n.n.n' (n is numeric), not '#{facterversion}'"
end

filter = []
opts[:supported_os].map do |os_sup|
if os_sup['operatingsystemrelease']
Array(os_sup['operatingsystemrelease']).map do |operatingsystemmajrelease|
opts[:hardwaremodels].each do |hardwaremodel|

os_release_filter = "/^#{Regexp.escape(operatingsystemmajrelease.split(' ')[0])}/"
case os_sup['operatingsystem']
when /BSD/i
Expand All @@ -90,7 +88,7 @@ def on_supported_os_implementation(opts = {})
os_release_filter = "\"#{operatingsystemmajrelease}\""
when /Amazon/i
# Tighten the regex for Amazon Linux 2 so that we don't pick up Amazon Linux 2016 or 2017 facts
os_release_filter = "/^2$/" if operatingsystemmajrelease == '2'
os_release_filter = '/^2$/' if operatingsystemmajrelease == '2'
end

filter << {
Expand All @@ -110,26 +108,26 @@ def on_supported_os_implementation(opts = {})
end
end

strict_requirement = RspecPuppetFacts::facter_version_to_strict_requirement(facterversion)
strict_requirement = RspecPuppetFacts.facter_version_to_strict_requirement(facterversion)

loose_requirement = RspecPuppetFacts::facter_version_to_loose_requirement(facterversion)
loose_requirement = RspecPuppetFacts.facter_version_to_loose_requirement(facterversion)
received_facts = []

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

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

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

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

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

os = "#{os_fact['name'].downcase}-#{os_fact['release']['major']}-#{os_fact['hardware']}"
next if RspecPuppetFacts.spec_facts_os_filter && !os.start_with?(RspecPuppetFacts.spec_facts_os_filter)

facts.merge! RspecPuppetFacts.common_facts
os_facts_hash[os] = RspecPuppetFacts.with_custom_facts(os, facts)
end
Expand Down Expand Up @@ -193,7 +192,7 @@ def add_custom_fact(name, value, options = {})
def self.register_custom_fact(name, value, options)
@custom_facts ||= {}
name = RSpec.configuration.facterdb_string_keys ? name.to_s : name.to_sym
@custom_facts[name] = {:options => options, :value => value}
@custom_facts[name] = { options: options, value: value }
end

# Adds any custom facts according to the rules defined for the operating
Expand All @@ -212,7 +211,7 @@ def self.with_custom_facts(os, facts)
value = fact[:value].respond_to?(:call) ? fact[:value].call(os, facts) : fact[:value]
# if merge_facts passed, merge supplied facts into facts hash
if fact[:options][:merge_facts]
facts.deep_merge!({name => value})
facts.deep_merge!({ name => value })
else
facts[name] = value
end
Expand All @@ -235,7 +234,7 @@ def self.custom_facts
# @return [nil,String]
# @api private
def self.spec_facts_os_filter
ENV['SPEC_FACTS_OS']
ENV.fetch('SPEC_FACTS_OS', nil)
end

# 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.
Expand All @@ -251,17 +250,16 @@ def self.spec_facts_strict?
# @return [Hash <Symbol => String>]
def self.common_facts
return @common_facts if @common_facts

@common_facts = {
:puppetversion => Puppet.version,
:rubysitedir => RbConfig::CONFIG['sitelibdir'],
:rubyversion => RUBY_VERSION,
puppetversion: Puppet.version,
rubysitedir: RbConfig::CONFIG['sitelibdir'],
rubyversion: RUBY_VERSION,
}

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

if augeas?
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version')
end
@common_facts[:augeasversion] = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD).get('/augeas/version') if augeas?
@common_facts = stringify_keys(@common_facts) if RSpec.configuration.facterdb_string_keys

@common_facts
Expand Down Expand Up @@ -298,9 +296,8 @@ def self.mcollective?
# @return [Array<Hash>]
# @api private
def self.meta_supported_os
unless metadata['operatingsystem_support'].is_a? Array
fail StandardError, 'Unknown operatingsystem support in the metadata file!'
end
raise StandardError, 'Unknown operatingsystem support in the metadata file!' unless metadata['operatingsystem_support'].is_a? Array

metadata['operatingsystem_support']
end

Expand All @@ -311,9 +308,8 @@ def self.meta_supported_os
# @api private
def self.metadata
return @metadata if @metadata
unless File.file? metadata_file
fail StandardError, "Can't find metadata.json... dunno why"
end
raise StandardError, "Can't find metadata.json... dunno why" unless File.file? metadata_file

content = File.read metadata_file
@metadata = JSON.parse content
end
Expand All @@ -329,7 +325,7 @@ def self.metadata_file
# @param message [String]
# @api private
def self.warning(message)
STDERR.puts message
warn message
end

# Reset the memoization
Expand Down Expand Up @@ -379,9 +375,6 @@ def self.facter_version_to_loose_requirement_string(version)
elsif /\A[0-9]+\Z/.match?(version)
# Interpret 3 as < 4
"< #{version.to_i + 1}"
else
# This would be the same as the strict requirement
nil
end
end

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

version_map = data.map { |_, versions|
version_map = data.map do |_, versions|
if versions['puppet'].nil? || versions['facter'].nil?
nil
else
[Gem::Version.new(versions['puppet']), versions['facter']]
end
}.compact
end.compact

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

RSpec.configure do |c|
c.add_setting :default_facter_version,
default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
c.add_setting :facterdb_string_keys,
default: false
c.add_setting :default_facter_version, default: RspecPuppetFacts.facter_version_for_puppet_version(Puppet.version)
c.add_setting :facterdb_string_keys, default: false
end
Loading

0 comments on commit 50cff16

Please sign in to comment.