Skip to content

Commit

Permalink
rubocop: autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Nov 23, 2021
1 parent 5e498fb commit cf4881a
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 63 deletions.
34 changes: 13 additions & 21 deletions lib/puppet/provider/alternative_entry/chkconfig.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.type(:alternative_entry).provide(:chkconfig) do
confine osfamily: :redhat
defaultfor osfamily: :redhat
Expand All @@ -19,14 +21,8 @@ def exists?
end

def destroy
# rubocop:disable Style/RedundantBegin
begin
# rubocop::enable Style/RedundantBegin
update('--remove', @resource.value(:altname), @resource.value(:name))
# rubocop:disable Lint/HandleExceptions
rescue
# rubocop:enable Lint/HandleExceptions
end
update('--remove', @resource.value(:altname), @resource.value(:name))
rescue StandardError # rubocop:disable Lint/SuppressedException
end

def self.instances
Expand All @@ -46,26 +42,22 @@ def self.prefetch(resources)
catalog = resources.values.first.catalog
instances.each do |prov|
catalog.resources.each do |item|
if item.class.to_s == 'Puppet::Type::Alternative_entry' && item.name == prov.name && item.parameter('altlink').value == prov.altlink
item.provider = prov
end
item.provider = prov if item.class.to_s == 'Puppet::Type::Alternative_entry' && item.name == prov.name && item.parameter('altlink').value == prov.altlink
end
end
end

ALT_RPM_QUERY_REGEX = %r{^(.*\/[^\/]*) -.*priority (\w+)$}
ALT_RPM_QUERY_REGEX = %r{^(.*/[^/]*) -.*priority (\w+)$}.freeze

def self.query_alternative(altname)
begin
output = update('--display', altname)
altlink = File.readlines('/var/lib/alternatives/' + altname)[1].chomp
output.scan(ALT_RPM_QUERY_REGEX).map do |(path, priority)|
{ altname: altname, altlink: altlink, name: path, priority: priority, altlink_ro: altlink, ensure: :present }
end
rescue
Puppet.warning format(_('Failed to parse alternatives entry %{name}'), name: name)
{}
output = update('--display', altname)
altlink = File.readlines("/var/lib/alternatives/#{altname}")[1].chomp
output.scan(ALT_RPM_QUERY_REGEX).map do |(path, priority)|
{ altname: altname, altlink: altlink, name: path, priority: priority, altlink_ro: altlink, ensure: :present }
end
rescue StandardError
Puppet.warning format(_('Failed to parse alternatives entry %{name}'), name: name)
{}
end

def name=(new_name)
Expand Down
14 changes: 7 additions & 7 deletions lib/puppet/provider/alternative_entry/dpkg.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

Puppet::Type.type(:alternative_entry).provide(:dpkg) do
confine osfamily: [:debian, :suse]
defaultfor [operatingsystem: [:debian, :ubuntu], osfamily: :suse]
confine osfamily: %i[debian suse]
defaultfor [operatingsystem: %i[debian ubuntu], osfamily: :suse]

commands update: 'update-alternatives'

Expand All @@ -18,7 +20,7 @@ def exists?
# we cannot fetch @resource.value(:altname) if running 'puppet resource alternative_entry'
begin
output = update('--list', @resource.value(:altname) || altname)
rescue
rescue StandardError
return false
end

Expand Down Expand Up @@ -50,14 +52,12 @@ def self.prefetch(resources)
catalog = resources.values.first.catalog
instances.each do |prov|
catalog.resources.each do |item|
if item.class.to_s == 'Puppet::Type::Alternative_entry' && item.name == prov.name && item.parameter('altlink').value == prov.altlink
item.provider = prov
end
item.provider = prov if item.class.to_s == 'Puppet::Type::Alternative_entry' && item.name == prov.name && item.parameter('altlink').value == prov.altlink
end
end
end

ALT_QUERY_REGEX = %r{Alternative: (.*?)$.Priority: (.*?)$}m
ALT_QUERY_REGEX = %r{Alternative: (.*?)$.Priority: (.*?)$}m.freeze

def self.query_alternative(altname)
output = update('--query', altname)
Expand Down
33 changes: 17 additions & 16 deletions lib/puppet/provider/alternatives/chkconfig.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.type(:alternatives).provide(:chkconfig) do
confine osfamily: :redhat
defaultfor osfamily: :redhat
Expand All @@ -17,7 +19,7 @@ def self.list_alternatives
Dir.glob('/var/lib/alternatives/*')
end

ALT_RPM_QUERY_CURRENT_REGEX = %r{status is (\w+)\.\n\slink currently points to (.*\/[^\/]*)\n}
ALT_RPM_QUERY_CURRENT_REGEX = %r{status is (\w+)\.\n\slink currently points to (.*/[^/]*)\n}.freeze

# Generate a hash of hashes containing a link name and associated properties
#
Expand All @@ -27,15 +29,12 @@ def self.list_alternatives
def self.all
hash = {}
list_alternatives.map { |x| File.basename(x) }.each do |name|
begin
# rubocop:enable Style/EachWithObject
output = update('--display', name)
mode = output.match(ALT_RPM_QUERY_CURRENT_REGEX)[1]
path = output.match(ALT_RPM_QUERY_CURRENT_REGEX)[2]
hash[name] = { path: path, mode: mode }
rescue
Puppet.warning format(_('Failed to parse alternatives entry %{name}'), name: name)
end
output = update('--display', name)
mode = output.match(ALT_RPM_QUERY_CURRENT_REGEX)[1]
path = output.match(ALT_RPM_QUERY_CURRENT_REGEX)[2]
hash[name] = { path: path, mode: mode }
rescue StandardError
Puppet.warning format(_('Failed to parse alternatives entry %{name}'), name: name)
end
hash
end
Expand All @@ -57,13 +56,14 @@ def mode
output = update('--display', @resource.value(:name))
first = output.split("\n").first

if first =~ %r{auto mode}
case first
when %r{auto mode}
'auto'
elsif first =~ %r{manual mode}
when %r{manual mode}
'manual'
elsif first =~ %r{status is auto}
when %r{status is auto}
'auto'
elsif first =~ %r{status is manual}
when %r{status is manual}
'manual'
else
raise Puppet::Error, "Could not determine if #{self} is in auto or manual mode"
Expand All @@ -73,9 +73,10 @@ def mode
# Set the mode to manual or auto.
# @param [Symbol] newmode Either :auto or :manual for the alternatives mode
def mode=(newmode)
if newmode == :auto
case newmode
when :auto
update('--auto', @resource.value(:name))
elsif newmode == :manual
when :manual
# No change in value, but sets it to manual
update('--set', name, path)
end
Expand Down
16 changes: 10 additions & 6 deletions lib/puppet/provider/alternatives/dpkg.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

Puppet::Type.type(:alternatives).provide(:dpkg) do
confine osfamily: [:debian, :suse]
defaultfor [operatingsystem: [:debian, :ubuntu], osfamily: :suse]
confine osfamily: %i[debian suse]
defaultfor [operatingsystem: %i[debian ubuntu], osfamily: :suse]
commands update: 'update-alternatives'

has_feature :mode
Expand Down Expand Up @@ -50,9 +52,10 @@ def mode
output = update('--display', @resource.value(:name))
first = output.split("\n").first

if first =~ %r{auto mode}
case first
when %r{auto mode}
'auto'
elsif first =~ %r{manual mode}
when %r{manual mode}
'manual'
else
raise Puppet::Error, "Could not determine if #{self} is in auto or manual mode"
Expand All @@ -62,9 +65,10 @@ def mode
# Set the mode to manual or auto.
# @param [Symbol] newmode Either :auto or :manual for the alternatives mode
def mode=(newmode)
if newmode == :auto
case newmode
when :auto
update('--auto', @resource.value(:name))
elsif newmode == :manual
when :manual
# No change in value, but sets it to manual
update('--set', name, path)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/puppet/type/alternative_entry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:alternative_entry) do
ensurable

Expand Down Expand Up @@ -33,11 +35,9 @@
desc 'The value of the priority of this alternative'

validate do |prio|
begin
Integer(prio)
rescue
raise ArgumentError, 'priority must be an integer'
end
Integer(prio)
rescue StandardError
raise ArgumentError, 'priority must be an integer'
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/type/alternatives.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:alternatives) do
feature :mode, 'The alternative can provide auto and manual modes'

Expand Down
6 changes: 4 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
$LOAD_PATH.unshift(dir, "#{dir}lib", "#{dir}../lib")

require 'mocha/api'
require 'puppet'

if Dir.exist?(File.expand_path('../../lib', __FILE__)) && RUBY_VERSION !~ %r{^1.9}
if Dir.exist?(File.expand_path('../lib', __dir__)) && RUBY_VERSION !~ %r{^1.9}
require 'coveralls'
require 'simplecov'
require 'simplecov-console'
Expand Down
8 changes: 5 additions & 3 deletions spec/unit/provider/alternatives/chkconfig_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Puppet::Type.type(:alternatives).provider(:chkconfig) do
Expand All @@ -15,7 +17,7 @@ def my_fixture_read(type, path)

let(:stub_selections) do
{
'sample' => { path: '/opt/sample1' },
'sample' => { path: '/opt/sample1' },
'testcmd' => { path: '/opt/testcmd1' }
}
end
Expand All @@ -29,14 +31,14 @@ def my_fixture_read(type, path)
end

describe 'returning data' do
subject { described_class.all }

before do
described_class.stubs(:list_alternatives).returns my_fixture_alternatives
described_class.stubs(:update).with('--display', 'sample').returns my_fixture_read('display', 'sample')
described_class.stubs(:update).with('--display', 'testcmd').returns my_fixture_read('display', 'testcmd')
end

subject { described_class.all }

it { is_expected.to be_a Hash }
it { expect(subject['sample']).to eq(mode: 'manual', path: '/opt/sample2') }
it { expect(subject['testcmd']).to eq(mode: 'manual', path: '/opt/testcmd1') }
Expand Down
8 changes: 5 additions & 3 deletions spec/unit/provider/alternatives/dpkg_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Puppet::Type.type(:alternatives).provider(:dpkg) do
Expand All @@ -11,7 +13,7 @@ def my_fixture_read(path)

let(:stub_selections) do
{
'editor' => { mode: 'manual', path: '/usr/bin/vim.tiny' },
'editor' => { mode: 'manual', path: '/usr/bin/vim.tiny' },
'aptitude' => { mode: 'auto', path: '/usr/bin/aptitude-curses' }
}
end
Expand All @@ -23,12 +25,12 @@ def my_fixture_read(path)
end

describe 'returning data' do
subject { described_class.all }

before do
described_class.stubs(:update).with('--get-selections').returns my_fixture_read('get-selections')
end

subject { described_class.all }

it { is_expected.to be_a Hash }
it { expect(subject['editor']).to eq(mode: 'manual', path: '/usr/bin/vim.tiny') }
it { expect(subject['aptitude']).to eq(mode: 'auto', path: '/usr/bin/aptitude-curses') }
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/type/alternatives_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe Puppet::Type.type(:alternatives) do
describe 'property `path`' do
it 'passes validation with an absolute path' do
expect { described_class.new(name: 'ruby', path: '/usr/bin/ruby1.9') }.not_to raise_error
end

it 'fails validation without an absolute path' do
expect { described_class.new(name: 'ruby', path: "The bees they're in my eyes") }.to raise_error Puppet::Error, %r{must be a fully qualified path}
end
Expand Down Expand Up @@ -45,9 +48,11 @@
it 'is equal' do
expect(req.size).to eq(1)
end

it 'has matching source' do
expect(req[0].source).to eq alternative_entry
end

it 'has matching target' do
expect(req[0].target).to eq alternatives
end
Expand Down

0 comments on commit cf4881a

Please sign in to comment.