Skip to content

Commit

Permalink
wip: satisfy rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning- committed May 8, 2024
1 parent 0ab971b commit 0c80681
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 130 deletions.
42 changes: 24 additions & 18 deletions lib/facter/power_devices.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
Facter.add(:power_devices, :type => :aggregate) do
# frozen_string_literal: true

# rubocop:disable Style/RegexpLiteral

Facter.add(:power_devices, type: :aggregate) do
confine kernel: 'windows'

chunk(:devices) do
all_devices = Facter::Core::Execution.execute('powercfg /devicequery all_devices').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_programmable = Facter::Core::Execution.execute('powercfg /devicequery wake_programmable').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_armed = Facter::Core::Execution.execute('powercfg /devicequery wake_armed').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_from_any = Facter::Core::Execution.execute('powercfg /devicequery wake_from_any').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_from_s1_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_from_s2_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
wake_from_s3_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
s1_supported = Facter::Core::Execution.execute('powercfg /devicequery s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
s2_supported = Facter::Core::Execution.execute('powercfg /devicequery s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
s3_supported = Facter::Core::Execution.execute('powercfg /devicequery s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
s4_supported = Facter::Core::Execution.execute('powercfg /devicequery s4_supported').split(/[\n\r]+/).reject{|x| x.eql?('')}
all_devices = Facter::Core::Execution.execute('powercfg /devicequery all_devices').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_programmable = Facter::Core::Execution.execute('powercfg /devicequery wake_programmable').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_armed = Facter::Core::Execution.execute('powercfg /devicequery wake_armed').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_from_any = Facter::Core::Execution.execute('powercfg /devicequery wake_from_any').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_from_s1_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s1_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_from_s2_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s2_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
wake_from_s3_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s3_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
s1_supported = Facter::Core::Execution.execute('powercfg /devicequery s1_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
s2_supported = Facter::Core::Execution.execute('powercfg /devicequery s2_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
s3_supported = Facter::Core::Execution.execute('powercfg /devicequery s3_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }
s4_supported = Facter::Core::Execution.execute('powercfg /devicequery s4_supported').split(/[\n\r]+/).reject { |x| x.eql?('') }

devices = {}

all_devices.each do |device|
devices[device.to_sym] = {} unless devices.has_key?(device.to_sym)
devices[device.to_sym] = {} unless devices.key?(device.to_sym)
devices[device.to_sym].store(:wake_programmable, true) if wake_programmable.include?(device)
devices[device.to_sym].store(:wake_armed, true) if wake_armed.include?(device)
devices[device.to_sym].store(:wake_from_any, true) if wake_from_any.include?(device)
Expand All @@ -30,7 +34,7 @@
devices[device.to_sym].store(:s4_supported, true) if s4_supported.include?(device)
end

devices.delete_if{|key, value| value.empty?}
devices.delete_if { |_key, value| value.empty? }
devices
end

Expand All @@ -46,12 +50,12 @@
caller_type = match[1].downcase.to_sym
next
end
if caller_type != :unknown
if caller_type != :unknown # rubocop:disable Style/Next
line.match(/^(.*?)((?:\s(DISPLAY|SYSTEM|AWAYMODE)(?!.*\b\3\b))+)$/) do |match|
request = match[2].strip.split
devices[match[1].to_sym] = {} unless devices.has_key?(match[1].to_sym)
devices[match[1].to_sym][:power_request_overrides] = {} unless devices[match[1].to_sym].has_key?(:power_request_overrides)
devices[match[1].to_sym][:power_request_overrides][caller_type] = {} unless devices[match[1].to_sym][:power_request_overrides].has_key?(caller_type)
devices[match[1].to_sym] = {} unless devices.key?(match[1].to_sym)
devices[match[1].to_sym][:power_request_overrides] = {} unless devices[match[1].to_sym].key?(:power_request_overrides)
devices[match[1].to_sym][:power_request_overrides][caller_type] = {} unless devices[match[1].to_sym][:power_request_overrides].key?(caller_type)
devices[match[1].to_sym][:power_request_overrides][caller_type].store(:display, true) if request.include?('DISPLAY')
devices[match[1].to_sym][:power_request_overrides][caller_type].store(:system, true) if request.include?('SYSTEM')
devices[match[1].to_sym][:power_request_overrides][caller_type].store(:awaymode, true) if request.include?('AWAYMODE')
Expand All @@ -63,3 +67,5 @@
devices
end
end

# rubocop:enable Style/RegexpLiteral
6 changes: 6 additions & 0 deletions lib/facter/power_schemes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

# rubocop:disable Style/RegexpLiteral

Facter.add(:power_schemes) do
confine kernel: 'windows'

Expand All @@ -18,3 +22,5 @@
power_schemes
end
end

# rubocop:enable Style/RegexpLiteral
132 changes: 78 additions & 54 deletions spec/classes/scheme_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'windows_power::scheme' do
Expand All @@ -14,13 +16,15 @@
context 'activate existing power scheme' do
context 'which is not active yet' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: false
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: false
}
}
}
})
)
end

let(:params) do
Expand All @@ -40,13 +44,15 @@

context 'which is active already' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
}
}
}
})
)
end

let(:params) do
Expand All @@ -67,11 +73,13 @@

context 'activate non-existing power scheme' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
}
}
})
)
end

let(:params) do
Expand All @@ -92,14 +100,16 @@
context 'rename existing power scheme' do
context 'to new name' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true,
name: 'High performance'
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true,
name: 'High performance'
}
}
}
})
)
end

let(:params) do
Expand All @@ -120,14 +130,16 @@

context 'to matching name' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true,
name: 'super power'
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true,
name: 'super power'
}
}
}
})
)
end

let(:params) do
Expand All @@ -150,11 +162,13 @@
context 'duplicate existing power scheme' do
context 'as new scheme' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
}
}
})
)
end

let(:params) do
Expand All @@ -175,14 +189,16 @@

context 'as existing scheme' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {},
'3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {},
'3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
}
}
}
})
)
end

let(:params) do
Expand All @@ -204,11 +220,13 @@

context 'duplicate non-existing power scheme' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
}
}
})
)
end

let(:params) do
Expand All @@ -230,13 +248,15 @@
context 'configure existing power scheme' do
context 'which is active already' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: true
}
}
}
})
)
end

let(:params) do
Expand All @@ -262,13 +282,15 @@

context 'which is not active yet' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: false
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {
active: false
}
}
}
})
)
end

let(:params) do
Expand All @@ -295,11 +317,13 @@

context 'configure non-existing power scheme' do
let(:facts) do
super().merge({
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
super().merge(
{
power_schemes: {
'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}
}
}
})
)
end

let(:params) do
Expand Down
Loading

0 comments on commit 0c80681

Please sign in to comment.