Skip to content

Commit

Permalink
rubocop: Fix Layout cops
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Jun 9, 2024
1 parent 6913f88 commit 453848c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ inherit_from: .rubocop_todo.yml
inherit_gem:
voxpupuli-rubocop: rubocop.yml

Layout:
Layout/LineLength:
Enabled: false

# To match the gem name
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group :release do
gem 'github_changelog_generator', '~> 1.16.4', require: false
end

group :coverage, optional: ENV['COVERAGE']!='yes' do
group :coverage, optional: ENV['COVERAGE'] != 'yes' do
gem 'codecov', require: false
gem 'simplecov-console', require: false
end
8 changes: 4 additions & 4 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
22 changes: 12 additions & 10 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 Down Expand Up @@ -119,7 +117,9 @@ def on_supported_os_implementation(opts = {})
# 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 }

Expand Down Expand Up @@ -150,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 @@ -191,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 @@ -210,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 Down Expand Up @@ -249,6 +250,7 @@ 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'],
Expand Down Expand Up @@ -295,6 +297,7 @@ def self.mcollective?
# @api private
def self.meta_supported_os
raise StandardError, 'Unknown operatingsystem support in the metadata file!' unless metadata['operatingsystem_support'].is_a? Array

metadata['operatingsystem_support']
end

Expand All @@ -306,6 +309,7 @@ def self.meta_supported_os
def self.metadata
return @metadata if @metadata
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 Down Expand Up @@ -411,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
66 changes: 34 additions & 32 deletions spec/rspec_puppet_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
end

describe '.stringify_keys' do
it { expect(described_class.stringify_keys({ os: { family: 'RedHat' } })).to eq({ 'os' => { 'family' => 'RedHat' } }) }
it {
expect(described_class.stringify_keys({ os: { family: 'RedHat' } })).to eq({ 'os' => { 'family' => 'RedHat' } })
}
end

describe '.facter_version_for_puppet_version' do
Expand Down Expand Up @@ -139,7 +141,7 @@
{
supported_os: [
{
'operatingsystem' => 'Debian',
'operatingsystem' => 'Debian',
'operatingsystemrelease' => ['12'],
},
],
Expand All @@ -148,7 +150,7 @@
end

let(:get_keys) do
proc { |r| r.keys + r.select { |_,v| v.is_a?(Hash) }.map { |_,v| get_keys.call(v) }.flatten }
proc { |r| r.keys + r.select { |_, v| v.is_a?(Hash) }.map { |_, v| get_keys.call(v) }.flatten }
end

context 'set to true' do
Expand Down Expand Up @@ -245,14 +247,13 @@
context 'With a wrong operatingsystem_support section' do
let(:metadata) do
{
'operatingsystem_support' => 'Ubuntu',
'operatingsystem_support' => 'Ubuntu',
}
end

it { expect { subject }.to raise_error(StandardError, /Unknown operatingsystem support/) }
end
end

end
end

Expand Down Expand Up @@ -441,17 +442,17 @@
context 'When testing AIX 7.1' do
subject do
on_supported_os(
{
supported_os: [
{
'operatingsystem' => 'AIX',
'operatingsystemrelease' => [
'7.1', '7100',
],
},
{
supported_os: [
{
'operatingsystem' => 'AIX',
'operatingsystemrelease' => [
'7.1', '7100',
],
facterversion: '3.9',
},
},
],
facterversion: '3.9',
},
)
end

Expand All @@ -475,7 +476,7 @@
{
supported_os: [
{
'operatingsystem' => 'Windows',
'operatingsystem' => 'Windows',
'operatingsystemrelease' => release,
},
],
Expand Down Expand Up @@ -598,7 +599,7 @@
{
supported_os: [
{
'operatingsystem' => 'IOS',
'operatingsystem' => 'IOS',
'operatingsystemrelease' => ['12.2(25)EWA9'],
},
],
Expand All @@ -612,9 +613,9 @@

it 'escapes the parens in the filter' do
filter = {
'os.name' => 'IOS',
'os.name' => 'IOS',
'os.release.full' => '/^12\\.2\\(25\\)EWA9/',
'os.hardware' => 'x86_64',
'os.hardware' => 'x86_64',
}

expect(FacterDB).to receive(:get_facts).with(filter, symbolize_keys: true).once
Expand Down Expand Up @@ -643,7 +644,6 @@
RSpec.configuration.default_facter_version = Facter.version
end


it 'returns facts from the specified default Facter version' do
is_expected.to match(
'centos-9-x86_64' => include(
Expand All @@ -667,7 +667,6 @@
allow(Facter).to receive(:version).and_return('4.6')
end


it 'returns facts from a facter version matching version and below' do
is_expected.to match(
'centos-9-x86_64' => include(
Expand Down Expand Up @@ -747,7 +746,7 @@
before do
allow(FacterDB).to receive(:get_facts).and_call_original
allow(FacterDB).to receive(:get_facts).with(
{'os.name'=>'CentOS', 'os.release.full'=>'/^9/', 'os.hardware'=>'x86_64'}, symbolize_keys: true
{ 'os.name' => 'CentOS', 'os.release.full' => '/^9/', 'os.hardware' => 'x86_64' }, symbolize_keys: true
).and_wrap_original do |m, *args|
m.call(*args).reject { |facts| facts[:facterversion].start_with?('4.6.') }
end
Expand Down Expand Up @@ -792,21 +791,23 @@
it 'merges a fact value into fact when merge_facts passed' do
add_custom_fact :identity, { 'user' => 'test_user' }, merge_facts: true
expect(subject['redhat-9-x86_64'][:identity]).to eq(
{
'gid'=>0,
'group'=>'root',
'privileged'=>true,
'uid'=>0,
'user'=>'test_user',
})
{
'gid' => 0,
'group' => 'root',
'privileged' => true,
'uid' => 0,
'user' => 'test_user',
},
)
end

it 'overwrites fact' do
add_custom_fact :identity, { 'user' => 'other_user' }
expect(subject['redhat-9-x86_64'][:identity]).to eq(
{
'user'=>'other_user',
})
{
'user' => 'other_user',
},
)
end

it 'confines a fact to a particular operating system' do
Expand Down Expand Up @@ -851,6 +852,7 @@ module AugeasStub # rubocop:todo Lint/ConstantDefinitionInBlock
def self.open(*_args)
self
end

def self.get(*_args)
'my_version'
end
Expand Down

0 comments on commit 453848c

Please sign in to comment.