From f045c04aed8c27cbacb2eed8fa900f2681e40911 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:27:01 -0400 Subject: [PATCH 01/11] Remove outdated cop, apply RSpec/ClassCheck cop. --- .rubocop.yml | 2 -- spec/sys_cpu_bsd_spec.rb | 14 +++++++------- spec/sys_cpu_hpux_spec.rb | 12 ++++++------ spec/sys_cpu_linux_spec.rb | 10 +++++----- spec/sys_cpu_windows_spec.rb | 12 ++++++------ 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b7bd785..d4c1343 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,8 +4,6 @@ RSpec/MultipleExpectations: Max: 4 RSpec/ContextWording: Enabled: false -RSpec/FilePath: - SpecSuffixOnly: true RSpec/InstanceVariable: Enabled: false diff --git a/spec/sys_cpu_bsd_spec.rb b/spec/sys_cpu_bsd_spec.rb index 432fc09..c7717ea 100644 --- a/spec/sys_cpu_bsd_spec.rb +++ b/spec/sys_cpu_bsd_spec.rb @@ -15,7 +15,7 @@ end example 'architecture method returns a sane value' do - expect(described_class.architecture).to be_kind_of(String) + expect(described_class.architecture).to be_a(String) expect(described_class.architecture.size).to be > 0 end @@ -29,7 +29,7 @@ end example 'freq method returns expected value' do - expect(described_class.freq).to be_kind_of(Integer) + expect(described_class.freq).to be_a(Integer) expect(described_class.freq).to be > 0 end @@ -43,9 +43,9 @@ end example 'load_avg returns the expected results' do - expect(described_class.load_avg).to be_kind_of(Array) + expect(described_class.load_avg).to be_a(Array) expect(described_class.load_avg.length).to eq(3) - expect(described_class.load_avg[0]).to be_kind_of(Float) + expect(described_class.load_avg[0]).to be_a(Float) end example 'load_avg does not accept any arguments' do @@ -58,7 +58,7 @@ end example 'machine method returns sane value' do - expect(described_class.machine).to be_kind_of(String) + expect(described_class.machine).to be_a(String) expect(described_class.machine.size).to be > 0 end @@ -72,7 +72,7 @@ end example 'model method returns sane value' do - expect(described_class.model).to be_kind_of(String) + expect(described_class.model).to be_a(String) expect(described_class.model.length).to be > 0 end @@ -86,7 +86,7 @@ end example 'num_cpu method returns expected value' do - expect(described_class.num_cpu).to be_kind_of(Integer) + expect(described_class.num_cpu).to be_a(Integer) expect(described_class.num_cpu).to be > 0 end diff --git a/spec/sys_cpu_hpux_spec.rb b/spec/sys_cpu_hpux_spec.rb index 917c51b..0cd03c4 100644 --- a/spec/sys_cpu_hpux_spec.rb +++ b/spec/sys_cpu_hpux_spec.rb @@ -14,25 +14,25 @@ expect(described_class).to respond_to(:freq) expect{ described_class.freq }.not_to raise_error expect{ described_class.freq(0) }.not_to raise_error - expect(described_class.freq).to be_kind_of(Integer) + expect(described_class.freq).to be_a(Integer) end example 'num_cpu' do expect(described_class).to respond_to(:num_cpu) expect{ described_class.num_cpu }.not_to raise_error - expect(described_class.num_cpu).to be_kind_of(Integer) + expect(described_class.num_cpu).to be_a(Integer) end example 'num_active_cpu' do expect(described_class).to respond_to(:num_active_cpu) expect{ described_class.num_active_cpu }.not_to raise_error - expect(described_class.num_active_cpu).to be_kind_of(Integer) + expect(described_class.num_active_cpu).to be_a(Integer) end example 'cpu_architecture' do expect(described_class).to respond_to(:architecture) expect{ described_class.architecture }.not_to raise_error - expect(described_class.architecture).to be_kind_of(String) + expect(described_class.architecture).to be_a(String) end example 'load_avg basic sanity check' do @@ -47,8 +47,8 @@ end example 'load_avg expected results' do - expect(described_class.load_avg).to be_kind_of(Array) - expect(described_class.load_avg(0)).to be_kind_of(Array) + expect(described_class.load_avg).to be_a(Array) + expect(described_class.load_avg(0)).to be_a(Array) expect(described_class.load_avg.length).to eq(3) expect(described_class.load_avg(0).length).to eq(3) end diff --git a/spec/sys_cpu_linux_spec.rb b/spec/sys_cpu_linux_spec.rb index f228cab..21aec12 100644 --- a/spec/sys_cpu_linux_spec.rb +++ b/spec/sys_cpu_linux_spec.rb @@ -25,28 +25,28 @@ example 'cpu_stats works as expected' do expect{ described_class.cpu_stats }.not_to raise_error - expect(described_class.cpu_stats).to be_kind_of(Hash) + expect(described_class.cpu_stats).to be_a(Hash) expect(described_class.cpu_stats['cpu0'].length).to be >= 4 end example 'architecture works as expected' do expect{ described_class.architecture }.not_to raise_error - expect(described_class.architecture).to be_kind_of(String) + expect(described_class.architecture).to be_a(String) end example 'model works as expected' do expect{ described_class.model }.not_to raise_error - expect(described_class.model).to be_kind_of(String) + expect(described_class.model).to be_a(String) end example 'freq works as expected' do expect{ described_class.freq }.not_to raise_error - expect(described_class.freq).to be_kind_of(Numeric) + expect(described_class.freq).to be_a(Numeric) end example 'num_cpu works as expected' do expect{ described_class.num_cpu }.not_to raise_error - expect(described_class.num_cpu).to be_kind_of(Numeric) + expect(described_class.num_cpu).to be_a(Numeric) end example 'bogus methods are not picked up by method_missing' do diff --git a/spec/sys_cpu_windows_spec.rb b/spec/sys_cpu_windows_spec.rb index 15a9160..1b41689 100644 --- a/spec/sys_cpu_windows_spec.rb +++ b/spec/sys_cpu_windows_spec.rb @@ -17,13 +17,13 @@ expect(described_class).to respond_to(:architecture) expect{ described_class.architecture }.not_to raise_error expect{ described_class.architecture(host) }.not_to raise_error - expect(described_class.architecture).to be_kind_of(String) + expect(described_class.architecture).to be_a(String) end example 'freq basic functionality' do expect(described_class).to respond_to(:freq) expect{ described_class.freq }.not_to raise_error - expect(described_class.freq).to be_kind_of(Integer) + expect(described_class.freq).to be_a(Integer) end example 'freq with arguments' do @@ -35,28 +35,28 @@ expect(described_class).to respond_to(:model) expect{ described_class.model }.not_to raise_error expect{ described_class.model(host) }.not_to raise_error - expect(described_class.model).to be_kind_of(String) + expect(described_class.model).to be_a(String) end example 'num_cpu' do expect(described_class).to respond_to(:num_cpu) expect{ described_class.num_cpu }.not_to raise_error expect{ described_class.num_cpu(host) }.not_to raise_error - expect(described_class.num_cpu).to be_kind_of(Integer) + expect(described_class.num_cpu).to be_a(Integer) end example 'cpu_type' do expect(described_class).to respond_to(:cpu_type) expect{ described_class.cpu_type }.not_to raise_error expect{ described_class.cpu_type(host) }.not_to raise_error - expect(described_class.cpu_type).to be_kind_of(String) + expect(described_class.cpu_type).to be_a(String) end example 'load_avg' do expect(described_class).to respond_to(:load_avg) expect{ described_class.load_avg }.not_to raise_error expect{ described_class.load_avg(0, host) }.not_to raise_error - expect(described_class.load_avg).to be_kind_of(Integer).or be_kind_of(NilClass) + expect(described_class.load_avg).to be_a(Integer).or be_a(NilClass) end example 'processors' do From f5329e3055710e465631c9c5333259b795b535c5 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:27:39 -0400 Subject: [PATCH 02/11] Apply RSpec/MetadataStyle cop. --- spec/sys_cpu_bsd_spec.rb | 2 +- spec/sys_cpu_hpux_spec.rb | 2 +- spec/sys_cpu_linux_spec.rb | 2 +- spec/sys_cpu_windows_spec.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/sys_cpu_bsd_spec.rb b/spec/sys_cpu_bsd_spec.rb index c7717ea..11cf7bb 100644 --- a/spec/sys_cpu_bsd_spec.rb +++ b/spec/sys_cpu_bsd_spec.rb @@ -8,7 +8,7 @@ require 'sys/cpu' require 'spec_helper' -RSpec.describe Sys::CPU, :bsd => true do +RSpec.describe Sys::CPU, :bsd do example 'architecture method basic functionality' do expect(described_class).to respond_to(:architecture) expect{ described_class.architecture }.not_to raise_error diff --git a/spec/sys_cpu_hpux_spec.rb b/spec/sys_cpu_hpux_spec.rb index 0cd03c4..3ef05a9 100644 --- a/spec/sys_cpu_hpux_spec.rb +++ b/spec/sys_cpu_hpux_spec.rb @@ -9,7 +9,7 @@ require 'sys/cpu' require 'spec_helper' -RSpec.describe Sys::CPU, :hpux => true do +RSpec.describe Sys::CPU, :hpux do example 'cpu_freq' do expect(described_class).to respond_to(:freq) expect{ described_class.freq }.not_to raise_error diff --git a/spec/sys_cpu_linux_spec.rb b/spec/sys_cpu_linux_spec.rb index 21aec12..c30f986 100644 --- a/spec/sys_cpu_linux_spec.rb +++ b/spec/sys_cpu_linux_spec.rb @@ -9,7 +9,7 @@ require 'sys/cpu' require 'spec_helper' -RSpec.describe Sys::CPU, :linux => true do +RSpec.describe Sys::CPU, :linux do example 'dynamic methods are defined as expected' do expect do described_class.processors do |cs| diff --git a/spec/sys_cpu_windows_spec.rb b/spec/sys_cpu_windows_spec.rb index 1b41689..109704d 100644 --- a/spec/sys_cpu_windows_spec.rb +++ b/spec/sys_cpu_windows_spec.rb @@ -10,7 +10,7 @@ require 'sys/cpu' require 'socket' -RSpec.describe Sys::CPU, :windows => true do +RSpec.describe Sys::CPU, :windows do let(:host) { Socket.gethostname } example 'architecture' do From 2bc1626b8354874dfc240e1c6a94e7162e2d0d8c Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:28:48 -0400 Subject: [PATCH 03/11] Disable FilePathFormat cop. --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index d4c1343..91f1610 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,8 @@ RSpec/ContextWording: Enabled: false RSpec/InstanceVariable: Enabled: false +RSpec/SpecFilePathFormat: + Enabled: false AllCops: NewCops: enable From 766f9f4e21aedd69afa0ed0072bd5e747454f3f6 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:29:27 -0400 Subject: [PATCH 04/11] Apply Style/StringLiterals cop. --- spec/sys_cpu_bsd_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/sys_cpu_bsd_spec.rb b/spec/sys_cpu_bsd_spec.rb index 11cf7bb..de0738f 100644 --- a/spec/sys_cpu_bsd_spec.rb +++ b/spec/sys_cpu_bsd_spec.rb @@ -94,8 +94,8 @@ expect{ described_class.num_cpu(0) }.to raise_error(ArgumentError) end - context "ffi methods and constants are private" do - example "ffi constants are private" do + context 'ffi methods and constants are private' do + example 'ffi constants are private' do constants = described_class.constants expect(constants).not_to include(:CTL_HW) expect(constants).not_to include(:CPU_TYPE_X86) @@ -104,13 +104,13 @@ expect(constants).not_to include(:ClockInfo) end - example "ffi core methods are private" do + example 'ffi core methods are private' do methods = described_class.methods(false) expect(methods).not_to include(:attach_function) expect(methods).not_to include(:bitmask) end - example "ffi attached methods are private" do + example 'ffi attached methods are private' do methods = described_class.methods(false) expect(methods).not_to include(:sysctl) expect(methods).not_to include(:sysctlbyname) From b243cf5c76a4af6fa13467052f7e037050bd4f4e Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:30:48 -0400 Subject: [PATCH 05/11] Minor spacing fix. --- lib/sys/darwin/sys/cpu.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sys/darwin/sys/cpu.rb b/lib/sys/darwin/sys/cpu.rb index e68b664..37ea938 100644 --- a/lib/sys/darwin/sys/cpu.rb +++ b/lib/sys/darwin/sys/cpu.rb @@ -43,7 +43,7 @@ class Error < StandardError; end CPU_ARCH_ABI64 = 0x01000000 CPU_TYPE_X86 = 7 CPU_TYPE_X86_64 = (CPU_TYPE_X86 | CPU_ARCH_ABI64) - CPU_TYPE_ARM = 12 + CPU_TYPE_ARM = 12 CPU_TYPE_SPARC = 14 CPU_TYPE_POWERPC = 18 CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64 From 70a12e8f5d52f5ab4bc68152c8f3e872a06c1995 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:33:19 -0400 Subject: [PATCH 06/11] Disable Naming/VariableNumber cop, these constants are named based on header file entries. --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 91f1610..1a179a4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,6 +56,8 @@ Metrics/CyclomaticComplexity: Enabled: false Metrics/PerceivedComplexity: Enabled: false +Naming/VariableNumber: + Enabled: false Naming/RescuedExceptionsVariableName: PreferredName: 'err' Naming/FileName: From cf61bbca507fd5881ed4dfaea5c0a588dc77cbe5 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:34:10 -0400 Subject: [PATCH 07/11] Disable Style/SlicingWithRange, Ruby shouldn't even parse that abomination. --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 1a179a4..82c8135 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -38,6 +38,8 @@ Style/RedundantBegin: Enabled: false Style/SafeNavigation: Enabled: false +Style/SlicingWithRange: + Enabled: false Layout/EmptyLineAfterGuardClause: Enabled: false Layout/CaseIndentation: From da8fe417726adecee341d508ebd00cfabe5d09d9 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:36:46 -0400 Subject: [PATCH 08/11] Add class comment, remove a bogus if check on Darwin. --- lib/sys/darwin/sys/cpu.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sys/darwin/sys/cpu.rb b/lib/sys/darwin/sys/cpu.rb index 37ea938..ca34dc6 100644 --- a/lib/sys/darwin/sys/cpu.rb +++ b/lib/sys/darwin/sys/cpu.rb @@ -74,6 +74,7 @@ class Error < StandardError; end private_class_method :getloadavg private_class_method :sysconf + # Private wrapper class for struct clockinfo class ClockInfo < FFI::Struct layout( :hz, :int, @@ -189,7 +190,7 @@ def self.freq (optr.read_long * clock[:hz]) / 1_000_000 else if sysctlbyname('hw.cpufrequency', optr, size, nil, 0) < 0 - raise Error, 'sysctlbyname failed on hw.cpufrequency' if result < 0 + raise Error, 'sysctlbyname failed on hw.cpufrequency' end optr.read_long / 1_000_000 end From 95bee6e03081754f8f2ecf824cc15d2111e6b852 Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:41:13 -0400 Subject: [PATCH 09/11] Add class comment, make ProcInfo private in generic Unix code. --- lib/sys/unix/sys/cpu.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/sys/unix/sys/cpu.rb b/lib/sys/unix/sys/cpu.rb index 3e9f845..62d1ba9 100644 --- a/lib/sys/unix/sys/cpu.rb +++ b/lib/sys/unix/sys/cpu.rb @@ -92,6 +92,7 @@ class Error < StandardError; end # Do nothing, not supported on this platform. end + # Private wrapper class for the procinfo struct class ProcInfo < FFI::Struct layout( :pi_state, :int, @@ -101,6 +102,8 @@ class ProcInfo < FFI::Struct ) end + private_constant :ProcInfo + # Returns the cpu's architecture. On most systems this will be identical # to the CPU.machine method. On OpenBSD it will be identical to the CPU.model # method. From b8a3e5b790dac6da1b68520b240f13c082abf5bc Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:44:23 -0400 Subject: [PATCH 10/11] Tweak some cops. --- .rubocop.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 82c8135..586f159 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,9 +1,11 @@ require: rubocop-rspec RSpec/MultipleExpectations: - Max: 4 + Max: 5 RSpec/ContextWording: Enabled: false +RSpec/ExampleLength: + Max: 6 RSpec/InstanceVariable: Enabled: false RSpec/SpecFilePathFormat: @@ -49,7 +51,7 @@ Layout/SpaceBeforeBlockBraces: Metrics/AbcSize: Enabled: false Metrics/BlockLength: - IgnoredMethods: ['describe', 'context'] + AllowedMethods: ['describe', 'context'] Metrics/ClassLength: Enabled: false Metrics/MethodLength: From e2515e8cc6809811e26d7d0b3a4e38730e3fb5bd Mon Sep 17 00:00:00 2001 From: Daniel Berger <78529+djberg96@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:44:38 -0400 Subject: [PATCH 11/11] Temporarily disable Style/MutableConstant on Linux. --- lib/sys/linux/sys/cpu.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/sys/linux/sys/cpu.rb b/lib/sys/linux/sys/cpu.rb index 0052f9e..8472610 100644 --- a/lib/sys/linux/sys/cpu.rb +++ b/lib/sys/linux/sys/cpu.rb @@ -10,7 +10,10 @@ module Sys cpu_file = '/proc/cpuinfo' cpu_hash = {} + + # rubocop:disable Style/MutableConstant CPU_ARRAY = [] + # rubocop:enable Style/MutableConstant private_constant :CPU_ARRAY