Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop updates, and a couple minor code tweaks #40

Merged
merged 11 commits into from
Aug 22, 2024
14 changes: 10 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require: rubocop-rspec

RSpec/MultipleExpectations:
Max: 4
Max: 5
RSpec/ContextWording:
Enabled: false
RSpec/FilePath:
SpecSuffixOnly: true
RSpec/ExampleLength:
Max: 6
RSpec/InstanceVariable:
Enabled: false
RSpec/SpecFilePathFormat:
Enabled: false

AllCops:
NewCops: enable
Expand Down Expand Up @@ -38,6 +40,8 @@ Style/RedundantBegin:
Enabled: false
Style/SafeNavigation:
Enabled: false
Style/SlicingWithRange:
Enabled: false
Layout/EmptyLineAfterGuardClause:
Enabled: false
Layout/CaseIndentation:
Expand All @@ -47,7 +51,7 @@ Layout/SpaceBeforeBlockBraces:
Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
IgnoredMethods: ['describe', 'context']
AllowedMethods: ['describe', 'context']
Metrics/ClassLength:
Enabled: false
Metrics/MethodLength:
Expand All @@ -56,6 +60,8 @@ Metrics/CyclomaticComplexity:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Naming/VariableNumber:
Enabled: false
Naming/RescuedExceptionsVariableName:
PreferredName: 'err'
Naming/FileName:
Expand Down
5 changes: 3 additions & 2 deletions lib/sys/darwin/sys/cpu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/sys/linux/sys/cpu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/sys/unix/sys/cpu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions spec/sys_cpu_bsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
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
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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -86,16 +86,16 @@
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

example 'num_cpu method does not accept any arguments' do
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)
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions spec/sys_cpu_hpux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
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
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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/sys_cpu_linux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions spec/sys_cpu_windows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
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
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
Expand All @@ -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
Expand Down
Loading