From d0297689a46c3b07b1cc5b23d7c720816f0c7fdd Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 6 Oct 2023 15:31:51 -0700 Subject: [PATCH 01/11] (FACT-3428) RSpec/StubbedMock Switching from expect to allow can result in a false passing test in cases where the method is no longer called. Just disable it. See https://github.com/rubocop/rubocop-rspec/issues/1271 --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 7 ------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 241a8c9d18..29b1f01ecb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -53,6 +53,9 @@ Naming/VariableNumber: - 'lib/facter/resolvers/windows/ffi/networking_ffi.rb' - 'lib/facter/util/facts/windows_release_finder.rb' +RSpec/StubbedMock: + Enabled: false + Style/IfUnlessModifier: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3239cc990d..5fc64ffeb2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -75,13 +75,6 @@ RSpec/MultipleMemoizedHelpers: RSpec/NestedGroups: Max: 6 -# Offense count: 3 -RSpec/StubbedMock: - Exclude: - - 'spec/custom_facts/core/execution/fact_manager_spec.rb' - - 'spec/custom_facts/util/confine_spec.rb' - - 'spec/custom_facts/util/parser_spec.rb' - # Offense count: 6 RSpec/SubjectStub: Exclude: From ae1ffc8c511f7faaf497d8796a925837d8967084 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Tue, 19 Dec 2023 23:59:07 -0800 Subject: [PATCH 02/11] (FACT-3428) RSpec/MultipleMemoizedHelpers This doesn't seem useful. --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 29b1f01ecb..746c49219b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -53,6 +53,9 @@ Naming/VariableNumber: - 'lib/facter/resolvers/windows/ffi/networking_ffi.rb' - 'lib/facter/util/facts/windows_release_finder.rb' +RSpec/MultipleMemoizedHelpers: + Enabled: false + RSpec/StubbedMock: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5fc64ffeb2..60f2049083 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -65,11 +65,6 @@ RSpec/MessageSpies: RSpec/MultipleExpectations: Max: 3 -# Offense count: 166 -# Configuration parameters: AllowSubject. -RSpec/MultipleMemoizedHelpers: - Max: 16 - # Offense count: 241 # Configuration parameters: AllowedGroups. RSpec/NestedGroups: From aa9542a7933b44a0123a5051397ad74cf4f6fa59 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Wed, 20 Dec 2023 01:20:48 -0800 Subject: [PATCH 03/11] (FACT-3428) RSpec/{NestedGroups,DescribedClass,ExampleLength} We don't generally use `described_class.new` in tests so set the `EnforcedStyle` to explicit. Ignore ExampleLength Ensure no more than 6 nested example groups. --- .rubocop.yml | 9 +++++++++ .rubocop_todo.yml | 17 ----------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 746c49219b..2a784f07c5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -73,3 +73,12 @@ Style/GlobalStdStream: - 'lib/facter/framework/logging/logger.rb' - 'spec/framework/core/fact/internal/internal_fact_manager_spec.rb' - 'spec/framework/logging/logger_spec.rb' + +RSpec/NestedGroups: + Max: 6 + +RSpec/DescribedClass: + EnforcedStyle: explicit + +RSpec/ExampleLength: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 60f2049083..6ecde91adc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,18 +12,6 @@ Performance/RegexpMatch: Exclude: - 'install.rb' -# Offense count: 1490 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: SkipBlocks, EnforcedStyle. -# SupportedStyles: described_class, explicit -RSpec/DescribedClass: - Enabled: false - -# Offense count: 192 -# Configuration parameters: CountAsOne. -RSpec/ExampleLength: - Max: 34 - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: CustomTransform, IgnoredWords, DisallowedExamples. @@ -65,11 +53,6 @@ RSpec/MessageSpies: RSpec/MultipleExpectations: Max: 3 -# Offense count: 241 -# Configuration parameters: AllowedGroups. -RSpec/NestedGroups: - Max: 6 - # Offense count: 6 RSpec/SubjectStub: Exclude: From 1527f362d2de0c2344835fb8c74201e638bf859c Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Wed, 20 Dec 2023 00:11:23 -0800 Subject: [PATCH 04/11] (FACT-3428) RSpec/InstanceVariable Use let instead of instance variables --- .rubocop_todo.yml | 8 -------- .../core/execution/fact_manager_spec.rb | 12 ++++++------ spec/custom_facts/util/collection_spec.rb | 13 +++++++------ spec/custom_facts/util/confine_spec.rb | 15 ++++++++------- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6ecde91adc..a686a856a2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -26,14 +26,6 @@ RSpec/ExampleWording: RSpec/FilePath: Enabled: false -# Offense count: 15 -# Configuration parameters: AssignmentOnly. -RSpec/InstanceVariable: - Exclude: - - 'spec/custom_facts/core/execution/fact_manager_spec.rb' - - 'spec/custom_facts/util/collection_spec.rb' - - 'spec/custom_facts/util/confine_spec.rb' - # Offense count: 8 RSpec/LeakyConstantDeclaration: Exclude: diff --git a/spec/custom_facts/core/execution/fact_manager_spec.rb b/spec/custom_facts/core/execution/fact_manager_spec.rb index 2db211dc5c..1a6ce8f7f6 100644 --- a/spec/custom_facts/core/execution/fact_manager_spec.rb +++ b/spec/custom_facts/core/execution/fact_manager_spec.rb @@ -4,6 +4,8 @@ subject(:executor) { Facter::Core::Execution::Posix.new } describe '#with_env' do + let(:sentinel_var) { :resolution_test_foo.to_s } + it "executes the caller's block with the specified env vars" do test_env = { 'LANG' => 'C', 'LC_ALL' => 'C', 'FOO' => 'BAR' } executor.with_env test_env do @@ -56,23 +58,21 @@ end it "is not affected by a 'return' statement in the yield block" do - @sentinel_var = :resolution_test_foo.to_s - # the intent of this test case is to test a yield block that contains a return statement. However, it's illegal # to use a return statement outside of a method, so we need to create one here to give scope to the 'return' def handy_method - ENV[@sentinel_var] = 'foo' - new_env = { @sentinel_var => 'bar' } + ENV[sentinel_var] = 'foo' + new_env = { sentinel_var => 'bar' } executor.with_env new_env do - ENV[@sentinel_var] = 'bar' + ENV[sentinel_var] = 'bar' return end end handy_method - expect(ENV[@sentinel_var]).to eq 'foo' + expect(ENV[sentinel_var]).to eq 'foo' end end diff --git a/spec/custom_facts/util/collection_spec.rb b/spec/custom_facts/util/collection_spec.rb index 7638a132cc..6ee3e0dab2 100755 --- a/spec/custom_facts/util/collection_spec.rb +++ b/spec/custom_facts/util/collection_spec.rb @@ -125,20 +125,21 @@ def load(collection) end describe 'when retrieving facts' do - before do - @fact = collection.add('YayNess') - end + let(:fact) { collection.add('YayNess') } + + # initialize + before { fact } it 'returns the fact instance specified by the name' do - expect(collection.fact('YayNess')).to equal(@fact) + expect(collection.fact('YayNess')).to equal(fact) end it 'is case-insensitive' do - expect(collection.fact('yayness')).to equal(@fact) + expect(collection.fact('yayness')).to equal(fact) end it 'treats strings and symbols equivalently' do - expect(collection.fact(:yayness)).to equal(@fact) + expect(collection.fact(:yayness)).to equal(fact) end it 'uses its loader to try to load the fact if no fact can be found' do diff --git a/spec/custom_facts/util/confine_spec.rb b/spec/custom_facts/util/confine_spec.rb index a7e80e2c31..0b1bdf628e 100755 --- a/spec/custom_facts/util/confine_spec.rb +++ b/spec/custom_facts/util/confine_spec.rb @@ -27,14 +27,15 @@ end describe 'when evaluating' do + let(:fact) { double 'fact' } + def confined(fact_value, *confines) - allow(@fact).to receive(:value).and_return fact_value + allow(fact).to receive(:value).and_return fact_value LegacyFacter::Util::Confine.new('yay', *confines).true? end before do - @fact = double 'fact' - allow(Facter).to receive(:[]).and_return @fact + allow(Facter).to receive(:[]).and_return fact end it 'returns false if the fact does not exist' do @@ -44,9 +45,9 @@ def confined(fact_value, *confines) end it 'uses the returned fact to get the value' do - allow(Facter).to receive(:[]).with('yay').and_return @fact + allow(Facter).to receive(:[]).with('yay').and_return fact - expect(@fact).to receive(:value).and_return nil + expect(fact).to receive(:value).and_return nil LegacyFacter::Util::Confine.new('yay', 'test').true? end @@ -120,13 +121,13 @@ def confined(fact_value, *confines) end it 'accepts and evaluate a block argument against the fact' do - allow(@fact).to receive(:value).and_return 'foo' + allow(fact).to receive(:value).and_return 'foo' confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'foo' } expect(confine.true?).to be true end it 'returns false if the block raises a StandardError when checking a fact' do - allow(@fact).to receive(:value).and_return 'foo' + allow(fact).to receive(:value).and_return 'foo' confine = LegacyFacter::Util::Confine.new(:yay) { |_f| raise StandardError } expect(confine.true?).to be false end From ece4eb7fbfbe397228a66573c2e0c11778258bde Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 8 Dec 2023 23:11:57 -0800 Subject: [PATCH 05/11] (FACT-3428) RSpec/MessageSpies The cop recommends following: allow(..) expect(a).to have_received(:b) But sometimes it's shorter to combine: expect(a).to receive(b).and_return(c) Especially when testing the multiple messages are received. --- .rubocop.yml | 5 +++++ .rubocop_todo.yml | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 2a784f07c5..0f5d9778c0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -82,3 +82,8 @@ RSpec/DescribedClass: RSpec/ExampleLength: Enabled: false + +# It is sometimes better to expect().to receive().and_return +# or to receive different messages. +RSpec/MessageSpies: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a686a856a2..60a24d6758 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -35,12 +35,6 @@ RSpec/LeakyConstantDeclaration: - 'spec/facter/resolvers/macosx/mountpoints_spec.rb' - 'spec/facter/util/windows/network_utils_spec.rb' -# Offense count: 51 -# Configuration parameters: EnforcedStyle. -# SupportedStyles: have_received, receive -RSpec/MessageSpies: - Enabled: false - # Offense count: 13 RSpec/MultipleExpectations: Max: 3 From 31b22050a0b75c60a2848d0ea4d8a280c8f7284f Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 8 Dec 2023 23:19:34 -0800 Subject: [PATCH 06/11] (FACT-3428) RSpec/Multiple* Sometimes it's easier to test a couple of things in one test. --- .rubocop.yml | 12 +++++++++--- .rubocop_todo.yml | 4 ---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0f5d9778c0..389f7aaf5a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -74,9 +74,6 @@ Style/GlobalStdStream: - 'spec/framework/core/fact/internal/internal_fact_manager_spec.rb' - 'spec/framework/logging/logger_spec.rb' -RSpec/NestedGroups: - Max: 6 - RSpec/DescribedClass: EnforcedStyle: explicit @@ -87,3 +84,12 @@ RSpec/ExampleLength: # or to receive different messages. RSpec/MessageSpies: Enabled: false + +RSpec/MultipleExpectations: + Max: 3 + +RSpec/MultipleMemoizedHelpers: + Enabled: false + +RSpec/NestedGroups: + Enabled: 6 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 60a24d6758..76535fbca4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -35,10 +35,6 @@ RSpec/LeakyConstantDeclaration: - 'spec/facter/resolvers/macosx/mountpoints_spec.rb' - 'spec/facter/util/windows/network_utils_spec.rb' -# Offense count: 13 -RSpec/MultipleExpectations: - Max: 3 - # Offense count: 6 RSpec/SubjectStub: Exclude: From 03baf040f2c2cc43033a2ec85dd0836c77caab04 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 8 Dec 2023 23:31:31 -0800 Subject: [PATCH 07/11] (FACT-3428) RSpec/SubjectStub It's a code smell to stub out a method that you're trying to test. The custom_fact code is legacy, so just exclude those tests. --- .rubocop.yml | 7 +++++++ .rubocop_todo.yml | 8 -------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 389f7aaf5a..efec6ecad7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -93,3 +93,10 @@ RSpec/MultipleMemoizedHelpers: RSpec/NestedGroups: Enabled: 6 + +RSpec/SubjectStub: + Exclude: + - 'spec/custom_facts/core/aggregate_spec.rb' + - 'spec/custom_facts/core/resolvable_spec.rb' + - 'spec/custom_facts/util/fact_spec.rb' + - 'spec/custom_facts/util/resolution_spec.rb' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 76535fbca4..c4c1135f53 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -35,14 +35,6 @@ RSpec/LeakyConstantDeclaration: - 'spec/facter/resolvers/macosx/mountpoints_spec.rb' - 'spec/facter/util/windows/network_utils_spec.rb' -# Offense count: 6 -RSpec/SubjectStub: - Exclude: - - 'spec/custom_facts/core/aggregate_spec.rb' - - 'spec/custom_facts/core/resolvable_spec.rb' - - 'spec/custom_facts/util/fact_spec.rb' - - 'spec/custom_facts/util/resolution_spec.rb' - # Offense count: 114 # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: From 6e7970e20a97a0d4658ca3c5b1cef83dff9457c6 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Sat, 9 Dec 2023 00:29:20 -0800 Subject: [PATCH 08/11] (FACT-3428) RSpec/VerifyingDoubles Prefer instance_double/instance_spy over double/spy because the former will verify that the underlying method implements the method and will error if it doesn't. Given the way we currently stub ffi and windows classes in tests we have to use doubles for now. The following bugs in tests were uncovered: spec/facter/resolvers/solaris/mountpoints_spec.rb was incorrectly passing a class to object_double, so the test caused non-existent class methods to be called. spec/mocks/sys_filesystem.rb was incorrectly stubbing class methods. The real object has instance methods. spec/framework/core/session_cache_spec.rb was incorrectly passing an instance of the uname resolver to the SessionCache.subscribe method. spec/mocks/util.rb was attempting to call a non-existent setter on Facter::ResolvedFact. --- .rubocop.yml | 13 +++ .rubocop_todo.yml | 5 -- .../facts/macosx/memory/swap/capacity_spec.rb | 2 +- .../macosx/memory/swap/used_bytes_spec.rb | 2 +- .../macosx/memory/system/capacity_spec.rb | 2 +- .../macosx/memory/system/used_bytes_spec.rb | 2 +- .../windows/dmi/product/serial_number_spec.rb | 4 +- .../facts/windows/hypervisors/hyperv_spec.rb | 6 +- .../facts/windows/hypervisors/kvm_spec.rb | 8 +- .../windows/hypervisors/virtualbox_spec.rb | 6 +- .../facts/windows/hypervisors/vmware_spec.rb | 6 +- .../facts/windows/hypervisors/xen_spec.rb | 6 +- .../facts/windows/identity/privileged_spec.rb | 2 +- .../windows/memory/system/capacity_spec.rb | 2 +- .../windows/memory/system/used_bytes_spec.rb | 2 +- .../windows/networking/interfaces_spec.rb | 2 +- .../facts/windows/networking/mtu_spec.rb | 2 +- .../facts/windows/processors/models_spec.rb | 2 +- spec/facter/query_parser_spec.rb | 28 +++--- .../resolvers/freebsd/ffi_helper_spec.rb | 2 +- .../resolvers/macosx/mountpoints_spec.rb | 14 +-- spec/facter/resolvers/mountpoints_spec.rb | 20 ++--- .../resolvers/solaris/mountpoints_spec.rb | 54 ++++++------ .../fact_loaders/external_fact_loader_spec.rb | 2 +- spec/framework/core/session_cache_spec.rb | 8 +- .../formatters/hocon_fact_formatter_spec.rb | 28 +++--- .../formatters/json_fact_formatter_spec.rb | 26 +++--- spec/mocks/sys_filesystem.rb | 85 +++++++++++++++---- spec/mocks/util.rb | 20 ++--- 29 files changed, 208 insertions(+), 153 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index efec6ecad7..20dac8c1d3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -100,3 +100,16 @@ RSpec/SubjectStub: - 'spec/custom_facts/core/resolvable_spec.rb' - 'spec/custom_facts/util/fact_spec.rb' - 'spec/custom_facts/util/resolution_spec.rb' + +# Prefer instance_double/instance_spy over double/spy because only methods +# defined on the underlying object can be stubbed. FFI and some Windows +# code can't be verified because of the way we fake out those classes. +RSpec/VerifiedDoubles: + Enabled: true + Exclude: + - 'spec/custom_facts/**/*' + - 'spec/facter/resolvers/aix/hardware_spec.rb' + - 'spec/facter/resolvers/*/ffi_helper_spec.rb' + - 'spec/facter/resolvers/windows/*' + - 'spec/facter/util/windows/network_utils_spec.rb' + - 'spec/facter/util/windows/win32ole_spec.rb' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c4c1135f53..43a1ecb71b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -35,11 +35,6 @@ RSpec/LeakyConstantDeclaration: - 'spec/facter/resolvers/macosx/mountpoints_spec.rb' - 'spec/facter/util/windows/network_utils_spec.rb' -# Offense count: 114 -# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. -RSpec/VerifiedDoubles: - Enabled: false - # Need to remove logger class variables Style/ClassVars: Exclude: diff --git a/spec/facter/facts/macosx/memory/swap/capacity_spec.rb b/spec/facter/facts/macosx/memory/swap/capacity_spec.rb index 2db9dd22a7..724d18ee02 100644 --- a/spec/facter/facts/macosx/memory/swap/capacity_spec.rb +++ b/spec/facter/facts/macosx/memory/swap/capacity_spec.rb @@ -3,7 +3,7 @@ describe Facts::Macosx::Memory::Swap::Capacity do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.swap.capacity', value: 1024) + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.swap.capacity', value: 1024) allow(Facter::Resolvers::Macosx::SwapMemory).to receive(:resolve).with(:capacity).and_return(1024) allow(Facter::ResolvedFact).to receive(:new).with('memory.swap.capacity', 1024).and_return(expected_fact) diff --git a/spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb b/spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb index 26578d2a56..5ec94c5cd6 100644 --- a/spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb +++ b/spec/facter/facts/macosx/memory/swap/used_bytes_spec.rb @@ -3,7 +3,7 @@ describe Facts::Macosx::Memory::Swap::UsedBytes do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.swap.used_bytes', value: 1024) + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.swap.used_bytes', value: 1024) allow(Facter::Resolvers::Macosx::SwapMemory).to receive(:resolve).with(:used_bytes).and_return(1024) allow(Facter::ResolvedFact).to receive(:new).with('memory.swap.used_bytes', 1024).and_return(expected_fact) diff --git a/spec/facter/facts/macosx/memory/system/capacity_spec.rb b/spec/facter/facts/macosx/memory/system/capacity_spec.rb index 4aeefb370d..ad614e2d08 100644 --- a/spec/facter/facts/macosx/memory/system/capacity_spec.rb +++ b/spec/facter/facts/macosx/memory/system/capacity_spec.rb @@ -3,7 +3,7 @@ describe Facts::Macosx::Memory::System::Capacity do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.system.capacity', value: '15.53%') + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.system.capacity', value: '15.53%') allow(Facter::Resolvers::Macosx::SystemMemory).to receive(:resolve).with(:capacity).and_return('15.53%') allow(Facter::ResolvedFact).to receive(:new).with('memory.system.capacity', '15.53%').and_return(expected_fact) diff --git a/spec/facter/facts/macosx/memory/system/used_bytes_spec.rb b/spec/facter/facts/macosx/memory/system/used_bytes_spec.rb index ca6d05682f..a41dbe6928 100644 --- a/spec/facter/facts/macosx/memory/system/used_bytes_spec.rb +++ b/spec/facter/facts/macosx/memory/system/used_bytes_spec.rb @@ -3,7 +3,7 @@ describe Facts::Macosx::Memory::System::UsedBytes do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.system.used_bytes', value: 1024) + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.system.used_bytes', value: 1024) allow(Facter::Resolvers::Macosx::SystemMemory).to receive(:resolve).with(:used_bytes).and_return(1024) allow(Facter::ResolvedFact).to receive(:new).with('memory.system.used_bytes', 1024).and_return(expected_fact) diff --git a/spec/facter/facts/windows/dmi/product/serial_number_spec.rb b/spec/facter/facts/windows/dmi/product/serial_number_spec.rb index f713ada40f..00f06ec246 100644 --- a/spec/facter/facts/windows/dmi/product/serial_number_spec.rb +++ b/spec/facter/facts/windows/dmi/product/serial_number_spec.rb @@ -5,8 +5,8 @@ subject(:fact) { Facts::Windows::Dmi::Product::SerialNumber.new } let(:value) { 'VMware-42 1a 0d 03 0a b7 98 28-78 98 5e 85 a0 ad 18 47' } - let(:expected_resolved_fact) { double(Facter::ResolvedFact, name: 'dmi.product.serial_number', value: value) } - let(:resolved_legacy_fact) { double(Facter::ResolvedFact, name: 'serialnumber', value: value, type: :legacy) } + let(:expected_resolved_fact) { instance_double(Facter::ResolvedFact, name: 'dmi.product.serial_number', value: value) } + let(:resolved_legacy_fact) { instance_double(Facter::ResolvedFact, name: 'serialnumber', value: value, type: :legacy) } before do allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:serial_number).and_return(value) diff --git a/spec/facter/facts/windows/hypervisors/hyperv_spec.rb b/spec/facter/facts/windows/hypervisors/hyperv_spec.rb index ac53f7a813..87a44520fb 100644 --- a/spec/facter/facts/windows/hypervisors/hyperv_spec.rb +++ b/spec/facter/facts/windows/hypervisors/hyperv_spec.rb @@ -4,7 +4,7 @@ describe '#call_the_resolver' do context 'when is not HyperV hypervisor' do it 'returns nil' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:manufacturer).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.hyperv', nil).and_return(expected_fact) @@ -16,7 +16,7 @@ context 'when is HyperV hypervisor and CpuidSource resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: {}) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: {}) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('hyperv') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.hyperv', {}).and_return(expected_fact) @@ -27,7 +27,7 @@ context 'when is HyperV hypervisor and DmiBios resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: {}) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.hyperv', value: {}) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:manufacturer).and_return('Microsoft Enterprise') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.hyperv', {}).and_return(expected_fact) diff --git a/spec/facter/facts/windows/hypervisors/kvm_spec.rb b/spec/facter/facts/windows/hypervisors/kvm_spec.rb index 2cb0ba980e..bf00e23958 100644 --- a/spec/facter/facts/windows/hypervisors/kvm_spec.rb +++ b/spec/facter/facts/windows/hypervisors/kvm_spec.rb @@ -4,7 +4,7 @@ describe '#call_the_resolver' do context 'when is not kvm hypervisor' do it 'returns nil' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::NetKVM).to receive(:resolve).with(:kvm).and_return(false) allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('value') @@ -17,7 +17,7 @@ context 'when is kvm hypervisor but product name is parallels' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('kvm') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('Parallels') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.kvm', nil).and_return(expected_fact) @@ -29,7 +29,7 @@ context 'when is kvm hypervisor and openstack' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: { openstack: true }) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: { openstack: true }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('kvm') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('OpenStack') allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:manufacturer).and_return('value') @@ -44,7 +44,7 @@ context 'when is kvm hypervisor and gce' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: { google: true }) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.kvm', value: { google: true }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('gce') allow(Facter::Resolvers::NetKVM).to receive(:resolve).with(:kvm).and_return(true) allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('value') diff --git a/spec/facter/facts/windows/hypervisors/virtualbox_spec.rb b/spec/facter/facts/windows/hypervisors/virtualbox_spec.rb index 23e3968f29..1c3b18cac4 100644 --- a/spec/facter/facts/windows/hypervisors/virtualbox_spec.rb +++ b/spec/facter/facts/windows/hypervisors/virtualbox_spec.rb @@ -4,7 +4,7 @@ describe '#call_the_resolver' do context 'when is not Virtualbox hypervisor' do it 'returns nil' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.virtualbox', nil).and_return(expected_fact) @@ -16,7 +16,7 @@ context 'when is VirtualBox hypervisor and CpuidSource resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: { revision: ' 13.4', version: ' 13.4' }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('virtualbox') allow(Facter::Resolvers::Windows::Virtualization) @@ -33,7 +33,7 @@ context 'when is VirtualBox hypervisor and DMIComputerSystem resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.virtualbox', value: { revision: '', version: '' }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('VirtualBox') diff --git a/spec/facter/facts/windows/hypervisors/vmware_spec.rb b/spec/facter/facts/windows/hypervisors/vmware_spec.rb index 22cfd83915..8409de3777 100644 --- a/spec/facter/facts/windows/hypervisors/vmware_spec.rb +++ b/spec/facter/facts/windows/hypervisors/vmware_spec.rb @@ -4,7 +4,7 @@ describe '#call_the_resolver' do context 'when is not VMware hypervisor' do it 'returns nil' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:manufacturer).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.vmware', nil).and_return(expected_fact) @@ -16,7 +16,7 @@ context 'when is VMware hypervisor and virtualization resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: {}) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: {}) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('vmware') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.vmware', {}).and_return(expected_fact) @@ -27,7 +27,7 @@ context 'when is VMware hypervisor and DmiBios resolver returns the required output' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: {}) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.vmware', value: {}) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::Resolvers::DMIBios).to receive(:resolve).with(:manufacturer).and_return('VMware, Inc.') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.vmware', {}).and_return(expected_fact) diff --git a/spec/facter/facts/windows/hypervisors/xen_spec.rb b/spec/facter/facts/windows/hypervisors/xen_spec.rb index 17bdca285c..d28ab25c36 100644 --- a/spec/facter/facts/windows/hypervisors/xen_spec.rb +++ b/spec/facter/facts/windows/hypervisors/xen_spec.rb @@ -4,7 +4,7 @@ describe '#call_the_resolver' do context 'when is not xen hypervisor' do it 'returns nil' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.xen', value: nil) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.xen', value: nil) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('hypervisors.xen', nil).and_return(expected_fact) @@ -15,7 +15,7 @@ context 'when is xen hypervisor and context not hvm' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.xen', value: { context: 'pv' }) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.xen', value: { context: 'pv' }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('xen') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('PV domU') allow(Facter::ResolvedFact).to receive(:new) @@ -28,7 +28,7 @@ context 'when is xen hypervisor and context hvm' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'hypervisors.xen', value: { context: 'hvm' }) + expected_fact = instance_double(Facter::ResolvedFact, name: 'hypervisors.xen', value: { context: 'hvm' }) allow(Facter::Resolvers::Windows::Virtualization).to receive(:resolve).with(:virtual).and_return('xen') allow(Facter::Resolvers::DMIComputerSystem).to receive(:resolve).with(:name).and_return('HVM domU') allow(Facter::ResolvedFact).to receive(:new) diff --git a/spec/facter/facts/windows/identity/privileged_spec.rb b/spec/facter/facts/windows/identity/privileged_spec.rb index 932d5d60a9..49288c9704 100644 --- a/spec/facter/facts/windows/identity/privileged_spec.rb +++ b/spec/facter/facts/windows/identity/privileged_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Identity::Privileged do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'identity.privileged', value: 'value') + expected_fact = instance_double(Facter::ResolvedFact, name: 'identity.privileged', value: 'value') allow(Facter::Resolvers::Identity).to receive(:resolve).with(:privileged).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('identity.privileged', 'value').and_return(expected_fact) diff --git a/spec/facter/facts/windows/memory/system/capacity_spec.rb b/spec/facter/facts/windows/memory/system/capacity_spec.rb index 6656ce2dfd..1904d0095c 100644 --- a/spec/facter/facts/windows/memory/system/capacity_spec.rb +++ b/spec/facter/facts/windows/memory/system/capacity_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Memory::System::Capacity do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.system.capacity', value: 'value') + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.system.capacity', value: 'value') allow(Facter::Resolvers::Memory).to receive(:resolve).with(:capacity).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('memory.system.capacity', 'value').and_return(expected_fact) diff --git a/spec/facter/facts/windows/memory/system/used_bytes_spec.rb b/spec/facter/facts/windows/memory/system/used_bytes_spec.rb index 21194baced..7ac2053bbd 100644 --- a/spec/facter/facts/windows/memory/system/used_bytes_spec.rb +++ b/spec/facter/facts/windows/memory/system/used_bytes_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Memory::System::UsedBytes do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'memory.system.used_bytes', value: 1024) + expected_fact = instance_double(Facter::ResolvedFact, name: 'memory.system.used_bytes', value: 1024) allow(Facter::Resolvers::Memory).to receive(:resolve).with(:used_bytes).and_return(1024) allow(Facter::ResolvedFact).to receive(:new).with('memory.system.used_bytes', 1024).and_return(expected_fact) diff --git a/spec/facter/facts/windows/networking/interfaces_spec.rb b/spec/facter/facts/windows/networking/interfaces_spec.rb index c0df6ca8e0..e20a39a7d1 100644 --- a/spec/facter/facts/windows/networking/interfaces_spec.rb +++ b/spec/facter/facts/windows/networking/interfaces_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Networking::Interfaces do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'networking.interfaces', value: 'value') + expected_fact = instance_double(Facter::ResolvedFact, name: 'networking.interfaces', value: 'value') allow(Facter::Resolvers::Windows::Networking).to receive(:resolve).with(:interfaces).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('networking.interfaces', 'value').and_return(expected_fact) diff --git a/spec/facter/facts/windows/networking/mtu_spec.rb b/spec/facter/facts/windows/networking/mtu_spec.rb index a4616eb3dd..10b9dba2d5 100644 --- a/spec/facter/facts/windows/networking/mtu_spec.rb +++ b/spec/facter/facts/windows/networking/mtu_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Networking::Mtu do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'networking.mtu', value: 'value') + expected_fact = instance_double(Facter::ResolvedFact, name: 'networking.mtu', value: 'value') allow(Facter::Resolvers::Windows::Networking).to receive(:resolve).with(:mtu).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('networking.mtu', 'value').and_return(expected_fact) diff --git a/spec/facter/facts/windows/processors/models_spec.rb b/spec/facter/facts/windows/processors/models_spec.rb index 4e69d7df36..1a444e7a97 100644 --- a/spec/facter/facts/windows/processors/models_spec.rb +++ b/spec/facter/facts/windows/processors/models_spec.rb @@ -3,7 +3,7 @@ describe Facts::Windows::Processors::Models do describe '#call_the_resolver' do it 'returns a fact' do - expected_fact = double(Facter::ResolvedFact, name: 'processors.models', value: 'value') + expected_fact = instance_double(Facter::ResolvedFact, name: 'processors.models', value: 'value') allow(Facter::Resolvers::Processors).to receive(:resolve).with(:models).and_return('value') allow(Facter::ResolvedFact).to receive(:new).with('processors.models', 'value').and_return(expected_fact) diff --git a/spec/facter/query_parser_spec.rb b/spec/facter/query_parser_spec.rb index 360a5311b5..d1e3f11f73 100644 --- a/spec/facter/query_parser_spec.rb +++ b/spec/facter/query_parser_spec.rb @@ -8,9 +8,9 @@ os_name_class = 'Facter::Ubuntu::OsName' os_family_class = 'Facter::Ubuntu::OsFamily' - loaded_fact_os_name = double(Facter::LoadedFact, name: 'os.name', klass: os_name_class, type: :core, file: nil) - loaded_fact_os_family = double(Facter::LoadedFact, name: 'os.family', klass: os_family_class, type: :core, - file: nil) + loaded_fact_os_name = instance_double(Facter::LoadedFact, name: 'os.name', klass: os_name_class, type: :core, file: nil) + loaded_fact_os_family = instance_double(Facter::LoadedFact, name: 'os.family', klass: os_family_class, type: :core, + file: nil) loaded_facts = [loaded_fact_os_name, loaded_fact_os_family] matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) @@ -25,10 +25,10 @@ networking_class = 'Facter::Ubuntu::NetworkInterface' os_family_class = 'Facter::Ubuntu::OsFamily' - loaded_fact_networking = double(Facter::LoadedFact, name: 'ipaddress_.*', klass: networking_class, type: :legacy, - file: nil) - loaded_fact_os_family = double(Facter::LoadedFact, name: 'os.family', klass: os_family_class, type: :core, - file: nil) + loaded_fact_networking = instance_double(Facter::LoadedFact, name: 'ipaddress_.*', klass: networking_class, type: :legacy, + file: nil) + loaded_fact_os_family = instance_double(Facter::LoadedFact, name: 'os.family', klass: os_family_class, type: :core, + file: nil) loaded_facts = [loaded_fact_networking, loaded_fact_os_family] matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) @@ -57,8 +57,8 @@ query_list = ['custom_fact'] os_name_class = 'Facter::Ubuntu::OsName' - loaded_fact_os_name = double(Facter::LoadedFact, name: 'os.name', klass: os_name_class, type: :core, file: nil) - loaded_fact_custom_fact = double(Facter::LoadedFact, name: 'custom_fact', klass: nil, type: :custom, file: nil) + loaded_fact_os_name = instance_double(Facter::LoadedFact, name: 'os.name', klass: os_name_class, type: :core, file: nil) + loaded_fact_custom_fact = instance_double(Facter::LoadedFact, name: 'custom_fact', klass: nil, type: :custom, file: nil) loaded_facts = [loaded_fact_os_name, loaded_fact_custom_fact] matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) @@ -70,7 +70,7 @@ it 'queries if param is symbol' do query_list = [:path] path_class = 'Facter::Ubuntu::Path' - loaded_fact_path = double(Facter::LoadedFact, name: 'path', klass: path_class, type: :core, file: nil) + loaded_fact_path = instance_double(Facter::LoadedFact, name: 'path', klass: path_class, type: :core, file: nil) loaded_facts = [loaded_fact_path] matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) @@ -84,9 +84,9 @@ query_list = ['ldom.domainrole.impl'] ldom_class = 'Facter::Solaris::Ldom' - loaded_fact_ldom = double(Facter::LoadedFact, name: 'ldom', klass: ldom_class, type: :core, file: nil) - ldom_fact_ldom_alias = double(Facter::LoadedFact, name: 'ldom_domainrole_impl', klass: ldom_class, - type: :legacy, file: nil) + loaded_fact_ldom = instance_double(Facter::LoadedFact, name: 'ldom', klass: ldom_class, type: :core, file: nil) + ldom_fact_ldom_alias = instance_double(Facter::LoadedFact, name: 'ldom_domainrole_impl', klass: ldom_class, + type: :legacy, file: nil) loaded_facts = [loaded_fact_ldom, ldom_fact_ldom_alias] matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) @@ -112,7 +112,7 @@ context 'when a fact name contains a regex special character' do let(:query_list) { ['regex(string'] } - let(:loaded_facts) { [double(Facter::LoadedFact, name: 'a_loaded_fact', klass: nil, type: :custom, file: nil)] } + let(:loaded_facts) { [instance_double(Facter::LoadedFact, name: 'a_loaded_fact', klass: nil, type: :custom, file: nil)] } it 'is escaped correctly and does not result in an unexpected regex parse error' do matched_facts = Facter::QueryParser.parse(query_list, loaded_facts) diff --git a/spec/facter/resolvers/freebsd/ffi_helper_spec.rb b/spec/facter/resolvers/freebsd/ffi_helper_spec.rb index 17302ae79b..9abc41520e 100644 --- a/spec/facter/resolvers/freebsd/ffi_helper_spec.rb +++ b/spec/facter/resolvers/freebsd/ffi_helper_spec.rb @@ -36,7 +36,7 @@ context 'when expecting an uint32_t' do let(:result) { 42 } - let(:oldlen) { double('Integer') } + let(:oldlen) { instance_double(Integer) } before do allow(FFI::MemoryPointer).to receive(:new) diff --git a/spec/facter/resolvers/macosx/mountpoints_spec.rb b/spec/facter/resolvers/macosx/mountpoints_spec.rb index f729ae92ae..a8def81cdb 100644 --- a/spec/facter/resolvers/macosx/mountpoints_spec.rb +++ b/spec/facter/resolvers/macosx/mountpoints_spec.rb @@ -8,14 +8,14 @@ class Error < StandardError; end describe Facter::Resolvers::Macosx::Mountpoints do let(:mount) do - double(Sys::Filesystem::Mount, mount_type: 'ext4', mount_point: '/proc/a', options: mount_options, - name: '/dev/nvme0n1p2', dump_frequency: 0, pass_number: 0) + instance_double(Sys::Filesystem::Mount, mount_type: 'ext4', mount_point: '/proc/a', + options: mount_options, name: '/dev/nvme0n1p2', dump_frequency: 0, pass_number: 0) end let(:stat) do - double(Sys::Filesystem::Stat, - path: '/proc/a', base_type: nil, fragment_size: 4096, block_size: 4096, blocks: 113_879_332, - blocks_available: 16_596_603, blocks_free: 22_398_776) + instance_double(Sys::Filesystem::Stat, path: '/proc/a', base_type: nil, fragment_size: 4096, + block_size: 4096, blocks: 113_879_332, + blocks_available: 16_596_603, blocks_free: 22_398_776) end let(:fact) do @@ -57,8 +57,8 @@ class Error < StandardError; end let(:fact_options) { %w[rw noatime] } let(:mount) do - double(Sys::Filesystem::Mount, mount_point: '/', mount_type: 'ext4', options: mount_options, - name: '/dev/root') + instance_double(Sys::Filesystem::Mount, mount_point: '/', mount_type: 'ext4', options: mount_options, + name: '/dev/root') end it 'looks up the actual device if /dev/root' do diff --git a/spec/facter/resolvers/mountpoints_spec.rb b/spec/facter/resolvers/mountpoints_spec.rb index 9618a08633..177487b170 100644 --- a/spec/facter/resolvers/mountpoints_spec.rb +++ b/spec/facter/resolvers/mountpoints_spec.rb @@ -2,16 +2,16 @@ describe Facter::Resolvers::Mountpoints do let(:mount) do - double(Sys::Filesystem::Mount, - mount_point: '/', mount_time: nil, - mount_type: 'ext4', options: 'rw,noatime', name: - '/dev/nvme0n1p2', dump_frequency: 0, pass_number: 0) + instance_double(Sys::Filesystem::Mount, + mount_point: '/', mount_time: nil, + mount_type: 'ext4', options: 'rw,noatime', + name: '/dev/nvme0n1p2', dump_frequency: 0, pass_number: 0) end let(:stat) do - double(Sys::Filesystem::Stat, - path: '/', base_type: nil, fragment_size: 4096, block_size: 4096, blocks: 113_879_332, - blocks_available: -16_596_603, blocks_free: 22_398_776) + instance_double(Sys::Filesystem::Stat, + path: '/', base_type: nil, fragment_size: 4096, block_size: 4096, blocks: 113_879_332, + blocks_available: -16_596_603, blocks_free: 22_398_776) end let(:fact) do @@ -29,8 +29,8 @@ end let(:ignored_mounts) do - [double(Sys::Filesystem::Mount, mount_type: 'ext4', mount_point: '/proc/a', name: '/dev/ignore', options: 'rw'), - double(Sys::Filesystem::Mount, mount_type: 'autofs', mount_point: '/mnt/auto', name: '/dev/ignore', options: 'rw')] + [instance_double(Sys::Filesystem::Mount, mount_type: 'ext4', mount_point: '/proc/a', name: '/dev/ignore', options: 'rw'), + instance_double(Sys::Filesystem::Mount, mount_type: 'autofs', mount_point: '/mnt/auto', name: '/dev/ignore', options: 'rw')] end before do @@ -109,7 +109,7 @@ describe '.root_device' do let(:mount) do - double(Sys::Filesystem::Mount, mount_point: '/', mount_type: 'ext4', options: 'rw,noatime', name: '/dev/root') + instance_double(Sys::Filesystem::Mount, mount_point: '/', mount_type: 'ext4', options: 'rw,noatime', name: '/dev/root') end it 'looks up the actual device if /dev/root' do diff --git a/spec/facter/resolvers/solaris/mountpoints_spec.rb b/spec/facter/resolvers/solaris/mountpoints_spec.rb index c5934df05d..4bc4a0c4f5 100644 --- a/spec/facter/resolvers/solaris/mountpoints_spec.rb +++ b/spec/facter/resolvers/solaris/mountpoints_spec.rb @@ -4,39 +4,39 @@ let(:resolver) { Facter::Resolvers::Solaris::Mountpoints } let(:mounts) do [ - object_double(Sys::Filesystem::Mount, - mount_point: '/', mount_time: nil, - mount_type: 'zfs', options: 'dev=4490002', name: - 'rpool/ROOT/solaris', dump_frequency: nil, pass_number: nil), - object_double(Sys::Filesystem::Mount, - mount_point: '/devices', mount_time: nil, - mount_type: 'devfs', options: 'dev=8580000', name: - '/devices', dump_frequency: nil, pass_number: nil), - object_double(Sys::Filesystem::Mount, - mount_point: '/proc', mount_time: nil, - mount_type: 'proc', options: 'dev=8600000', name: - 'proc', dump_frequency: nil, pass_number: nil), - object_double(Sys::Filesystem::Mount, - mount_point: '/net', mount_time: nil, - mount_type: 'autofs', options: 'nosuid,indirect,ignore,nobrowse,dev=8900007', name: - '-hosts', dump_frequency: nil, pass_number: nil), - object_double(Sys::Filesystem::Mount, - mount_point: '/home', mount_time: nil, - mount_type: 'autofs', options: 'indirect,ignore,nobrowse,dev=8900008', name: - 'auto_home', dump_frequency: nil, pass_number: nil), - object_double(Sys::Filesystem::Mount, - mount_point: '/home/user', mount_time: nil, - mount_type: 'zfs', options: 'dev=8900009', name: - 'rpool/user', dump_frequency: nil, pass_number: nil) + instance_double(Sys::Filesystem::Mount, + mount_point: '/', mount_time: nil, + mount_type: 'zfs', options: 'dev=4490002', name: + 'rpool/ROOT/solaris', dump_frequency: nil, pass_number: nil), + instance_double(Sys::Filesystem::Mount, + mount_point: '/devices', mount_time: nil, + mount_type: 'devfs', options: 'dev=8580000', name: + '/devices', dump_frequency: nil, pass_number: nil), + instance_double(Sys::Filesystem::Mount, + mount_point: '/proc', mount_time: nil, + mount_type: 'proc', options: 'dev=8600000', name: + 'proc', dump_frequency: nil, pass_number: nil), + instance_double(Sys::Filesystem::Mount, + mount_point: '/net', mount_time: nil, + mount_type: 'autofs', options: 'nosuid,indirect,ignore,nobrowse,dev=8900007', name: + '-hosts', dump_frequency: nil, pass_number: nil), + instance_double(Sys::Filesystem::Mount, + mount_point: '/home', mount_time: nil, + mount_type: 'autofs', options: 'indirect,ignore,nobrowse,dev=8900008', name: + 'auto_home', dump_frequency: nil, pass_number: nil), + instance_double(Sys::Filesystem::Mount, + mount_point: '/home/user', mount_time: nil, + mount_type: 'zfs', options: 'dev=8900009', name: + 'rpool/user', dump_frequency: nil, pass_number: nil) ] end let(:mount) { mounts.first } let(:stat) do - object_double('Sys::Filesystem::Stat', - path: '/', base_type: 'zfs', fragment_size: 512, block_size: 131_072, blocks: 20_143_706, - blocks_available: 11_731_043, blocks_free: 11_731_043) + instance_double('Sys::Filesystem::Stat', + path: '/', base_type: 'zfs', fragment_size: 512, block_size: 131_072, blocks: 20_143_706, + blocks_available: 11_731_043, blocks_free: 11_731_043) end let(:fact) do diff --git a/spec/framework/core/fact_loaders/external_fact_loader_spec.rb b/spec/framework/core/fact_loaders/external_fact_loader_spec.rb index 84e541b685..5c3e8a47cd 100644 --- a/spec/framework/core/fact_loaders/external_fact_loader_spec.rb +++ b/spec/framework/core/fact_loaders/external_fact_loader_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe Facter::ExternalFactLoader do - let(:collection) { double(LegacyFacter::Util::Collection) } + let(:collection) { instance_double(LegacyFacter::Util::Collection) } let(:external_fact) { Facter::Util::Fact.new('external_fact', options: { fact_type: :external }) } let(:custom_fact) { Facter::Util::Fact.new('custom_fact', options: { fact_type: :custom }) } diff --git a/spec/framework/core/session_cache_spec.rb b/spec/framework/core/session_cache_spec.rb index 4b0c9da822..e6a857bbcf 100644 --- a/spec/framework/core/session_cache_spec.rb +++ b/spec/framework/core/session_cache_spec.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true describe Facter::SessionCache do - let(:uname_resolver) { spy(Facter::Resolvers::Uname) } + let(:uname_resolver) { Facter::Resolvers::Uname.new } it 'registers resolver subscription' do - Facter::SessionCache.subscribe(uname_resolver) - Facter::SessionCache.invalidate_all_caches + expect(uname_resolver.class).to receive(:invalidate_cache) - expect(uname_resolver).to have_received(:invalidate_cache) + Facter::SessionCache.subscribe(uname_resolver.class) + Facter::SessionCache.invalidate_all_caches end end diff --git a/spec/framework/formatters/hocon_fact_formatter_spec.rb b/spec/framework/formatters/hocon_fact_formatter_spec.rb index 59d05bc0e3..8d1838a9c2 100644 --- a/spec/framework/formatters/hocon_fact_formatter_spec.rb +++ b/spec/framework/formatters/hocon_fact_formatter_spec.rb @@ -3,14 +3,14 @@ describe Facter::HoconFactFormatter do it 'formats to hocon when no user query' do resolved_fact1 = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: '', type: :core) resolved_fact2 = - double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', + user_query: '', type: :core) resolved_fact3 = - double(Facter::ResolvedFact, name: 'os.architecture', value: 'x86_64', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.architecture', value: 'x86_64', + user_query: '', type: :core) resolved_fact_list = [resolved_fact1, resolved_fact2, resolved_fact3] formatted_output = Facter::HoconFactFormatter.new.format(resolved_fact_list) @@ -22,8 +22,8 @@ it 'formats to hocon for a single user query' do resolved_fact = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: 'os.name', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: 'os.name', type: :core) resolved_fact_list = [resolved_fact] formatted_output = Facter::HoconFactFormatter.new.format(resolved_fact_list) @@ -34,11 +34,11 @@ it 'formats to hocon for multiple user queries' do resolved_fact1 = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: 'os.name', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: 'os.name', type: :core) resolved_fact2 = - double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', - user_query: 'os.family', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', + user_query: 'os.family', type: :core) resolved_fact_list = [resolved_fact1, resolved_fact2] formatted_output = Facter::HoconFactFormatter.new.format(resolved_fact_list) @@ -55,8 +55,8 @@ it 'returns empty string when the fact value is nil' do resolved_fact1 = - double(Facter::ResolvedFact, name: 'my_external_fact', value: nil, - user_query: 'my_external_fact', type: :core) + instance_double(Facter::ResolvedFact, name: 'my_external_fact', value: nil, + user_query: 'my_external_fact', type: :core) resolved_fact_list = [resolved_fact1] formatted_output = Facter::HoconFactFormatter.new.format(resolved_fact_list) diff --git a/spec/framework/formatters/json_fact_formatter_spec.rb b/spec/framework/formatters/json_fact_formatter_spec.rb index 13aca0c02d..e9928c03e9 100644 --- a/spec/framework/formatters/json_fact_formatter_spec.rb +++ b/spec/framework/formatters/json_fact_formatter_spec.rb @@ -3,18 +3,16 @@ describe Facter::JsonFactFormatter do it 'formats to json when no user query' do resolved_fact1 = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: '', type: :core) resolved_fact2 = - double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', + user_query: '', type: :core) resolved_fact3 = - double(Facter::ResolvedFact, name: 'os.architecture', value: 'x86_64', - user_query: '', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.architecture', value: 'x86_64', + user_query: '', type: :core) resolved_fact_list = [resolved_fact1, resolved_fact2, resolved_fact3] - double - formatted_output = Facter::JsonFactFormatter.new.format(resolved_fact_list) expected_output = @@ -25,8 +23,8 @@ it 'formats to json for a single user query' do resolved_fact = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: 'os.name', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: 'os.name', type: :core) resolved_fact_list = [resolved_fact] formatted_output = Facter::JsonFactFormatter.new.format(resolved_fact_list) @@ -37,11 +35,11 @@ it 'formats to json for multiple user queries' do resolved_fact1 = - double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', - user_query: 'os.name', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.name', value: 'Darwin', + user_query: 'os.name', type: :core) resolved_fact2 = - double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', - user_query: 'os.family', type: :core) + instance_double(Facter::ResolvedFact, name: 'os.family', value: 'Darwin', + user_query: 'os.family', type: :core) resolved_fact_list = [resolved_fact1, resolved_fact2] formatted_output = Facter::JsonFactFormatter.new.format(resolved_fact_list) diff --git a/spec/mocks/sys_filesystem.rb b/spec/mocks/sys_filesystem.rb index 10fadb3bb0..1aca75d121 100644 --- a/spec/mocks/sys_filesystem.rb +++ b/spec/mocks/sys_filesystem.rb @@ -5,43 +5,92 @@ class Filesystem def self.mounts; end class Mount - def self.mount_point; end + # The name of the mounted resource. + attr_accessor :name - def self.mount_type; end + # The mount point/directory. + attr_accessor :mount_point - def self.options; end + # The type of filesystem mount, e.g. ufs, nfs, etc. + attr_accessor :mount_type - def self.mount_time; end + # A list of comma separated options for the mount, e.g. nosuid, etc. + attr_accessor :options - def self.name; end + # The time the filesystem was mounted. May be nil. + attr_accessor :mount_time - def self.dump_frequency; end + # The dump frequency in days. May be nil. + attr_accessor :dump_frequency - def self.pass_number; end + # The pass number of the filessytem check. May be nil. + attr_accessor :pass_number end class Stat - def self.path; end + # The path of the filesystem. + attr_accessor :path - def self.base_type; end + # The preferred system block size. + attr_accessor :block_size - def self.fragment_size; end + # The fragment size, i.e. fundamental filesystem block size. + attr_accessor :fragment_size - def self.block_size; end + # The total number of +fragment_size+ blocks in the filesystem. + attr_accessor :blocks - def self.blocks; end + # The total number of free blocks in the filesystem. + attr_accessor :blocks_free - def self.blocks_available; end + # The number of free blocks available to unprivileged processes. + attr_accessor :blocks_available - def self.blocks_free; end + # The total number of files/inodes that can be created. + attr_accessor :files - def self.bytes_total; end + # The total number of files/inodes on the filesystem. + attr_accessor :files_free - def self.bytes_available; end + # The number of free files/inodes available to unprivileged processes. + attr_accessor :files_available - def self.bytes_free; end + # The filesystem identifier. + attr_accessor :filesystem_id - def self.bytes_used; end + # A bit mask of flags. + attr_accessor :flags + + # The maximum length of a file name permitted on the filesystem. + attr_accessor :name_max + + # The filesystem type, e.g. UFS. + attr_accessor :base_type + + # Returns the total space on the partition. + def bytes_total + blocks * fragment_size + end + + # Returns the total amount of free space on the partition. + def bytes_free + blocks_free * fragment_size + end + + # Returns the amount of free space available to unprivileged processes. + def bytes_available + blocks_available * fragment_size + end + + # Returns the total amount of used space on the partition. + def bytes_used + bytes_total - bytes_free + end + + # Returns the percentage of the partition that has been used. + def percent_used + 100 - (100.0 * bytes_free.to_f / bytes_total.to_f) + end end end end diff --git a/spec/mocks/util.rb b/spec/mocks/util.rb index bbd5344bff..dd10dd7453 100644 --- a/spec/mocks/util.rb +++ b/spec/mocks/util.rb @@ -27,26 +27,26 @@ def mock_query_parser(user_query, loaded_fact_hash) .with(user_query, loaded_fact_hash) end -private_methods def allow_attr_change(resolved_fact_mock, fact_name, fact_value) +private_methods def allow_attr_change(resolved_fact_mock, fact_value) allow(resolved_fact_mock) .to receive(:value=) .with(fact_value) - allow(resolved_fact_mock) - .to receive(:name=) - .with(fact_name) - allow(resolved_fact_mock) .to receive(:user_query=) end def mock_resolved_fact(fact_name, fact_value, user_query = nil, type = :core) - resolved_fact_mock = double(Facter::ResolvedFact, name: fact_name, value: fact_value, - user_query: user_query, type: type, - legacy?: type == :legacy, core?: type == :core, file: nil, - resolves?: fact_name == user_query) + resolved_fact_mock = instance_double( + Facter::ResolvedFact, name: fact_name, value: fact_value, + user_query: user_query, type: type, + legacy?: type == :legacy, + core?: type == :core, + file: nil, + resolves?: fact_name == user_query + ) - allow_attr_change(resolved_fact_mock, fact_name, fact_value) + allow_attr_change(resolved_fact_mock, fact_value) resolved_fact_mock end From 658c096875d7bfd05977f6442b69a19dada991d2 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Wed, 20 Dec 2023 12:24:06 -0800 Subject: [PATCH 09/11] (FACT-3428) RSpec/LeakyConstantDeclaration This was resolved in the Lint/ConstantDefinitionInBlock cop --- .rubocop_todo.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 43a1ecb71b..3ad60c94bf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -26,15 +26,6 @@ RSpec/ExampleWording: RSpec/FilePath: Enabled: false -# Offense count: 8 -RSpec/LeakyConstantDeclaration: - Exclude: - - 'spec/custom_facts/core/resolvable_spec.rb' - - 'spec/custom_facts/core/suitable_spec.rb' - - 'spec/custom_facts/util/collection_spec.rb' - - 'spec/facter/resolvers/macosx/mountpoints_spec.rb' - - 'spec/facter/util/windows/network_utils_spec.rb' - # Need to remove logger class variables Style/ClassVars: Exclude: From a7e3f9cd8c522aae63f6f58af21775cc54d82076 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 8 Jan 2024 12:36:24 -0800 Subject: [PATCH 10/11] (FACT-3428) RSpec/ExampleWording --- .rubocop_todo.yml | 8 -------- spec/custom_facts/core/execution/windows_spec.rb | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3ad60c94bf..7bdfc55fc4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,14 +12,6 @@ Performance/RegexpMatch: Exclude: - 'install.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: CustomTransform, IgnoredWords, DisallowedExamples. -# DisallowedExamples: works -RSpec/ExampleWording: - Exclude: - - 'spec/custom_facts/core/execution/windows_spec.rb' - # Offense count: 70 # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. # Include: **/*_spec*rb*, **/spec/**/* diff --git a/spec/custom_facts/core/execution/windows_spec.rb b/spec/custom_facts/core/execution/windows_spec.rb index 275702b243..13f7d6f04c 100644 --- a/spec/custom_facts/core/execution/windows_spec.rb +++ b/spec/custom_facts/core/execution/windows_spec.rb @@ -82,7 +82,7 @@ expect(executor.which('foo.exe')).to eq 'C:\Windows\foo.exe' end - it "won't check all paths if one is executable" do + it 'does not check all paths if one is executable' do allow(File).to receive(:executable?).with('C:\Windows\system32\foo.exe').and_return false allow(File).to receive(:executable?).with('C:\Windows\foo.exe').and_return true From c8c09efdc8aaa43aaa9bc501872c21ac8ab0678b Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 6 Oct 2023 16:20:35 -0700 Subject: [PATCH 11/11] (FACT-3428) Performance/RegexpMatch If you don't need MatchData, then it's better to use String#match? --- .rubocop_todo.yml | 6 ------ install.rb | 10 +++++----- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7bdfc55fc4..4cc40fa416 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,12 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Performance/RegexpMatch: - Exclude: - - 'install.rb' - # Offense count: 70 # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. # Include: **/*_spec*rb*, **/spec/**/* diff --git a/install.rb b/install.rb index 730b17e1b0..12a8878632 100755 --- a/install.rb +++ b/install.rb @@ -40,7 +40,7 @@ class Installer # Returns true if OS is windows (copied from facter/util/config.rb) def windows? - (defined?(RbConfig) ? RbConfig : Config)::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i + (defined?(RbConfig) ? RbConfig : Config)::CONFIG['host_os'].match?(/mswin|win32|dos|mingw|cygwin/i) end def glob(list) @@ -153,7 +153,7 @@ def prepare_installation # /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin # which is not generally where people expect executables to be installed # These settings are appropriate defaults for all OS X versions. - RbConfig::CONFIG['bindir'] = '/usr/bin' if RUBY_PLATFORM =~ /^universal-darwin[\d.]+$/ + RbConfig::CONFIG['bindir'] = '/usr/bin' if RUBY_PLATFORM.match?(/^universal-darwin[\d.]+$/) # if InstallOptions.configdir # configdir = InstallOptions.configdir @@ -173,10 +173,10 @@ def prepare_installation else sitelibdir = RbConfig::CONFIG['sitelibdir'] if sitelibdir.nil? - sitelibdir = $LOAD_PATH.find { |x| x =~ /site_ruby/ } + sitelibdir = $LOAD_PATH.find { |x| x.match?(/site_ruby/) } if sitelibdir.nil? sitelibdir = File.join(libdir, 'site_ruby') - elsif sitelibdir !~ Regexp.quote(version) + elsif !sitelibdir.match?(Regexp.quote(version)) sitelibdir = File.join(sitelibdir, version) end end @@ -232,7 +232,7 @@ def install_binfile(from, op_file, target) File.open(tmp_file.path, 'w') do |op| op.puts "#!#{ruby}" contents = ip.readlines - contents.shift if contents[0] =~ /^#!/ + contents.shift if contents[0].match?(/^#!/) op.write contents.join end end