diff --git a/.rubocop.yml b/.rubocop.yml index b7bd785..586f159 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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 @@ -38,6 +40,8 @@ Style/RedundantBegin: Enabled: false Style/SafeNavigation: Enabled: false +Style/SlicingWithRange: + Enabled: false Layout/EmptyLineAfterGuardClause: Enabled: false Layout/CaseIndentation: @@ -47,7 +51,7 @@ Layout/SpaceBeforeBlockBraces: Metrics/AbcSize: Enabled: false Metrics/BlockLength: - IgnoredMethods: ['describe', 'context'] + AllowedMethods: ['describe', 'context'] Metrics/ClassLength: Enabled: false Metrics/MethodLength: @@ -56,6 +60,8 @@ Metrics/CyclomaticComplexity: Enabled: false Metrics/PerceivedComplexity: Enabled: false +Naming/VariableNumber: + Enabled: false Naming/RescuedExceptionsVariableName: PreferredName: 'err' Naming/FileName: diff --git a/lib/sys/darwin/sys/cpu.rb b/lib/sys/darwin/sys/cpu.rb index e68b664..ca34dc6 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 @@ -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 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 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. diff --git a/spec/sys_cpu_bsd_spec.rb b/spec/sys_cpu_bsd_spec.rb index 432fc09..de0738f 100644 --- a/spec/sys_cpu_bsd_spec.rb +++ b/spec/sys_cpu_bsd_spec.rb @@ -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 @@ -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 @@ -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) diff --git a/spec/sys_cpu_hpux_spec.rb b/spec/sys_cpu_hpux_spec.rb index 917c51b..3ef05a9 100644 --- a/spec/sys_cpu_hpux_spec.rb +++ b/spec/sys_cpu_hpux_spec.rb @@ -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 @@ -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..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| @@ -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..109704d 100644 --- a/spec/sys_cpu_windows_spec.rb +++ b/spec/sys_cpu_windows_spec.rb @@ -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 @@ -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