From 06ad1b45652c658b2997bdfc8bca061820d89a9a Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 23 Feb 2022 11:55:23 -0600 Subject: [PATCH] Fixup tests for Ruby 3.0 This is a pass through test failures and deprecation warnings: * Make all ambiguous `.with(..., key: val)` use explicit hashes to prevent test failures for argument mismatch in Ruby 3.0 * Scope down all unbounded `raise_error` to address warnings (remove one test that was revealed to be referencing a nonexistent variable once the raise_error was scoped.) * Update all `any_instance` usage to new syntax to address warnings * Allow the service cache to be cleared and do so between some tests * Fix a small bug in with_plugin's plugin not found code path (revealed by a scoped and_raise) --- plugins/commands/serve/util/cacher.rb | 4 ++++ plugins/commands/serve/util/service_info.rb | 2 +- .../unit/plugins/commands/package/command_test.rb | 4 ++-- .../commands/serve/service/guest_service_test.rb | 15 +++++++-------- .../commands/serve/service/host_service_test.rb | 11 +++++++---- .../providers/hyperv/action/read_guest_ip_test.rb | 2 +- test/unit/vagrant/ui_test.rb | 4 ++-- 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/plugins/commands/serve/util/cacher.rb b/plugins/commands/serve/util/cacher.rb index 8fe4612e8..e77dd96b2 100644 --- a/plugins/commands/serve/util/cacher.rb +++ b/plugins/commands/serve/util/cacher.rb @@ -10,6 +10,10 @@ module VagrantPlugins @registry = {} end + def clear + @registry = {} + end + # Check if the given key is currently registered # # @param key [Object] Generally String or Symbol diff --git a/plugins/commands/serve/util/service_info.rb b/plugins/commands/serve/util/service_info.rb index 57bd2984d..843983be0 100644 --- a/plugins/commands/serve/util/service_info.rb +++ b/plugins/commands/serve/util/service_info.rb @@ -59,7 +59,7 @@ module VagrantPlugins send(plugins)[info.plugin_name] ).first if !plugin - raise NameError, "Failed to locate plugin '#{plugin_name}' within #{plugins} plugins" + raise NameError, "Failed to locate plugin '#{info.plugin_name}' within #{plugins} plugins" end yield plugin, info if block_given? end diff --git a/test/unit/plugins/commands/package/command_test.rb b/test/unit/plugins/commands/package/command_test.rb index 7b289bd1e..793f0ea6d 100644 --- a/test/unit/plugins/commands/package/command_test.rb +++ b/test/unit/plugins/commands/package/command_test.rb @@ -64,7 +64,7 @@ describe VagrantPlugins::CommandPackage::Command do it "packages default machine inside specified folder" do expect(package_command).to receive(:package_vm).with( - a_machine_named('default'), :output => "package-output-folder/default" + a_machine_named('default'), {:output => "package-output-folder/default"} ) package_command.execute end @@ -96,7 +96,7 @@ describe VagrantPlugins::CommandPackage::Command do let(:argv){ ['--base', 'machine-id'] } it "packages vm defined within virtualbox" do - expect(package_command).to receive(:package_base).with(:base => 'machine-id') + expect(package_command).to receive(:package_base).with({:base => 'machine-id'}) package_command.execute end diff --git a/test/unit/plugins/commands/serve/service/guest_service_test.rb b/test/unit/plugins/commands/serve/service/guest_service_test.rb index 1a4aa90f0..fbe789ed7 100644 --- a/test/unit/plugins/commands/serve/service/guest_service_test.rb +++ b/test/unit/plugins/commands/serve/service/guest_service_test.rb @@ -61,7 +61,7 @@ describe VagrantPlugins::CommandServe::Service::GuestService do it "raises an error for unknown plugins" do ctx = DummyContext.new("idontexisthahaha") - expect { subject.parent("", ctx) }.to raise_error + expect { subject.parent("", ctx) }.to raise_error(/Failed to locate guest plugin/) end it "requests parent from plugins" do @@ -84,7 +84,11 @@ describe VagrantPlugins::CommandServe::Service::GuestService do p.guest(:test_false) { test_false_guest } end - VagrantPlugins::CommandServe::Mappers.any_instance.stub(:funcspec_map).and_return(machine) + allow_any_instance_of(VagrantPlugins::CommandServe::Mappers).to receive(:funcspec_map).and_return(machine) + end + + after do + VagrantPlugins::CommandServe::Service.cache.clear end it "generates a spec" do @@ -94,7 +98,7 @@ describe VagrantPlugins::CommandServe::Service::GuestService do it "raises an error for unknown plugins" do ctx = DummyContext.new("idontexisthahaha") - expect { subject.detect("", ctx) }.to raise_error + expect { subject.detect("", ctx) }.to raise_error(/Failed to locate plugin/) end it "detects true plugins" do @@ -153,11 +157,6 @@ describe VagrantPlugins::CommandServe::Service::GuestService do expect(spec).not_to be_nil end - it "raises an error for unknown plugins" do - ctx = DummyContext.new("idontexisthahaha") - expect { subject.has_capability(test_cap_name, ctx) }.to raise_error - end - it "returns true for plugin with capability" do ctx = DummyContext.new("cap_guest") d = subject.has_capability(named_cap_request, ctx) diff --git a/test/unit/plugins/commands/serve/service/host_service_test.rb b/test/unit/plugins/commands/serve/service/host_service_test.rb index 7d238eb35..ce0654662 100644 --- a/test/unit/plugins/commands/serve/service/host_service_test.rb +++ b/test/unit/plugins/commands/serve/service/host_service_test.rb @@ -88,10 +88,13 @@ describe VagrantPlugins::CommandServe::Service::HostService do p.host(:test_false) { test_false_host } end - VagrantPlugins::CommandServe::Client::Target.any_instance.stub(:project).and_return("") - VagrantPlugins::CommandServe::Client::Target.any_instance.stub(:name).and_return("dummy") - VagrantPlugins::CommandServe::Client::Target.any_instance.stub(:provider_name).and_return("virtualbox") - # Vagrant::Environment.any_instance.stub(:machine).and_return(machine) + allow_any_instance_of(VagrantPlugins::CommandServe::Client::Target).to receive(:project).and_return("") + allow_any_instance_of(VagrantPlugins::CommandServe::Client::Target).to receive(:name).and_return("dummy") + allow_any_instance_of(VagrantPlugins::CommandServe::Client::Target).to receive(:provider_name).and_return("virtualbox") + end + + after do + VagrantPlugins::CommandServe::Service.cache.clear end it "generates a spec" do diff --git a/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb b/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb index 5642c6271..0c96c436e 100644 --- a/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb +++ b/test/unit/plugins/providers/hyperv/action/read_guest_ip_test.rb @@ -31,7 +31,7 @@ describe VagrantPlugins::HyperV::Action::ReadGuestIP do end it "should set the host information into the env" do - expect(env).to receive(:[]=).with(:machine_ssh_info, host: "ADDRESS") + expect(env).to receive(:[]=).with(:machine_ssh_info, {host: "ADDRESS"}) expect(driver).to receive(:read_guest_ip).and_return("ip" => "ADDRESS") subject.call(env) end diff --git a/test/unit/vagrant/ui_test.rb b/test/unit/vagrant/ui_test.rb index e484b8154..d9c6afcc0 100644 --- a/test/unit/vagrant/ui_test.rb +++ b/test/unit/vagrant/ui_test.rb @@ -379,12 +379,12 @@ describe Vagrant::UI::Prefixed do describe "#machine" do it "sets the target option" do - expect(ui).to receive(:machine).with(:foo, target: prefix) + expect(ui).to receive(:machine).with(:foo, {target: prefix}) subject.machine(:foo) end it "preserves existing options" do - expect(ui).to receive(:machine).with(:foo, :bar, foo: :bar, target: prefix) + expect(ui).to receive(:machine).with(:foo, :bar, {foo: :bar, target: prefix}) subject.machine(:foo, :bar, foo: :bar) end end