From 2e2c2fad56c1ed5db1942440912d547d6c5d0bad Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 14 Mar 2014 11:37:50 -0300 Subject: [PATCH] Convert specs to RSpec 2.99.0.beta2 syntax with Transpec This conversion is done by Transpec 1.10.2 with the following command: transpec * 46 conversions from: obj.should to: expect(obj).to * 20 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 10 conversions from: == expected to: eq(expected) * 6 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 5 conversions from: obj.should_not to: expect(obj).not_to * 5 conversions from: Klass.any_instance.stub(:message) { |arg| } to: Klass.any_instance.stub(:message) { |instance, arg| } * 5 conversions from: Klass.any_instance.stub(:message) to: allow_any_instance_of(Klass).to receive(:message) * 1 conversion from: lambda { }.should to: expect { }.to --- spec/spec_helper.rb | 4 ++ .../unit/action/clear_forwarded_ports_spec.rb | 6 +- spec/unit/action/compress_rootfs_spec.rb | 4 +- spec/unit/action/forward_ports_spec.rb | 12 ++-- spec/unit/action/handle_box_metadata_spec.rb | 28 +++++----- spec/unit/action/setup_package_files_spec.rb | 12 ++-- spec/unit/driver/cli_spec.rb | 56 +++++++++---------- spec/unit/driver_spec.rb | 36 ++++++------ spec/unit/support/unit_example_group.rb | 12 ++-- spec/unit_helper.rb | 4 ++ 10 files changed, 91 insertions(+), 83 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7fee68f..7a4d034 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,4 +24,8 @@ RSpec.configure do |config| # the seed, which is printed after each run. # --seed 1234 config.order = 'random' + + config.mock_with :rspec do |c| + c.yield_receiver_to_any_instance_implementation_blocks = true + end end diff --git a/spec/unit/action/clear_forwarded_ports_spec.rb b/spec/unit/action/clear_forwarded_ports_spec.rb index 3f9f630..7f98e23 100644 --- a/spec/unit/action/clear_forwarded_ports_spec.rb +++ b/spec/unit/action/clear_forwarded_ports_spec.rb @@ -24,12 +24,12 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do after { FileUtils.rm_rf data_dir.to_s } it 'removes all files under pid directory' do - Dir[pids_dir.to_s + "/redir_*.pid"].should be_empty + expect(Dir[pids_dir.to_s + "/redir_*.pid"]).to be_empty end context 'with a valid redir pid' do it 'kills known processes' do - subject.should have_received(:system).with("pkill -TERM -P #{pid}") + expect(subject).to have_received(:system).with("pkill -TERM -P #{pid}") end end @@ -37,7 +37,7 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do let(:pid_cmd) { 'sudo ls' } it 'does not kill the process' do - subject.should_not have_received(:system).with("pkill -TERM -P #{pid}") + expect(subject).not_to have_received(:system).with("pkill -TERM -P #{pid}") end end end diff --git a/spec/unit/action/compress_rootfs_spec.rb b/spec/unit/action/compress_rootfs_spec.rb index 0a81c72..6807ed4 100644 --- a/spec/unit/action/compress_rootfs_spec.rb +++ b/spec/unit/action/compress_rootfs_spec.rb @@ -20,10 +20,10 @@ describe Vagrant::LXC::Action::CompressRootFS do end it "asks the driver to compress container's rootfs" do - driver.should have_received(:compress_rootfs) + expect(driver).to have_received(:compress_rootfs) end it 'sets export.temp_dir on action env' do - env['package.rootfs'].should == compressed_rootfs_path + expect(env['package.rootfs']).to eq(compressed_rootfs_path) end end diff --git a/spec/unit/action/forward_ports_spec.rb b/spec/unit/action/forward_ports_spec.rb index 077ee7d..bbaece4 100644 --- a/spec/unit/action/forward_ports_spec.rb +++ b/spec/unit/action/forward_ports_spec.rb @@ -33,7 +33,7 @@ describe Vagrant::LXC::Action::ForwardPorts do it 'forwards ports using redir' do subject.stub(system: true) subject.call(env) - subject.should have_received(:spawn).with( + expect(subject).to have_received(:spawn).with( "redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) end @@ -42,7 +42,7 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf.delete(:host_ip) subject.stub(system: true) subject.call(env) - subject.should have_received(:spawn).with( + expect(subject).to have_received(:spawn).with( "redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) end @@ -51,7 +51,7 @@ describe Vagrant::LXC::Action::ForwardPorts do forward_conf[:host_ip] = ' ' subject.stub(system: true) subject.call(env) - subject.should have_received(:spawn).with( + expect(subject).to have_received(:spawn).with( "redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" ) end @@ -60,18 +60,18 @@ describe Vagrant::LXC::Action::ForwardPorts do subject.stub(system: true) subject.call(env) pid_file = data_dir.join('pids', "redir_#{host_port}.pid").read - pid_file.should == pid + expect(pid_file).to eq(pid) end it 'allows disabling a previously forwarded port' do forward_conf[:disabled] = true subject.stub(system: true) subject.call(env) - subject.should_not have_received(:spawn) + expect(subject).not_to have_received(:spawn) end it 'raises RedirNotInstalled error if `redir` is not installed' do subject.stub(system: false) - lambda { subject.call(env) }.should raise_error(Vagrant::LXC::Errors::RedirNotInstalled) + expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled) end end diff --git a/spec/unit/action/handle_box_metadata_spec.rb b/spec/unit/action/handle_box_metadata_spec.rb index 63d81df..2f9c7c5 100644 --- a/spec/unit/action/handle_box_metadata_spec.rb +++ b/spec/unit/action/handle_box_metadata_spec.rb @@ -28,33 +28,33 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do end it 'sets the tarball argument for the template' do - env[:lxc_template_opts].should include( + expect(env[:lxc_template_opts]).to include( '--tarball' => box_directory.join('rootfs.tar.gz').to_s ) end it 'sets the template --config parameter' do - env[:lxc_template_opts].should include( + expect(env[:lxc_template_opts]).to include( '--config' => box_directory.join('lxc-config').to_s ) end it 'does not set the auth key argument for the template' do - env[:lxc_template_opts].should_not include( + expect(env[:lxc_template_opts]).not_to include( '--auth-key' => vagrant_key ) end it 'sets the template options from metadata on env hash' do - env[:lxc_template_opts].should include(metadata['template-opts']) + expect(env[:lxc_template_opts]).to include(metadata['template-opts']) end xit 'sets the template source path on env hash' do - env[:lxc_template_src].should == box_directory.join('lxc-template').to_s + expect(env[:lxc_template_src]).to eq(box_directory.join('lxc-template').to_s) end it 'does not warn about deprecation' do - env[:ui].should_not have_received(:warn) + expect(env[:ui]).not_to have_received(:warn) end end @@ -68,31 +68,31 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do end it 'sets the tarball argument for the template' do - env[:lxc_template_opts].should include( + expect(env[:lxc_template_opts]).to include( '--tarball' => box_directory.join('rootfs.tar.gz').to_s ) end it 'sets the auth key argument for the template' do - env[:lxc_template_opts].should include( + expect(env[:lxc_template_opts]).to include( '--auth-key' => vagrant_key ) end it 'sets the lxc config file parameter' do - env[:lxc_template_config].should == box_directory.join('lxc.conf').to_s + expect(env[:lxc_template_config]).to eq(box_directory.join('lxc.conf').to_s) end it 'sets the template options from metadata on env hash' do - env[:lxc_template_opts].should include(metadata['template-opts']) + expect(env[:lxc_template_opts]).to include(metadata['template-opts']) end xit 'sets the template source path on env hash' do - env[:lxc_template_src].should == box_directory.join('lxc-template').to_s + expect(env[:lxc_template_src]).to eq(box_directory.join('lxc-template').to_s) end it 'warns about deprecation' do - env[:ui].should have_received(:warn) + expect(env[:ui]).to have_received(:warn) end end @@ -110,14 +110,14 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do end it 'raises an error if the rootfs tarball cant be found' do - File.stub(:exists?).with(box_directory.join('rootfs.tar.gz').to_s).and_return(false) + allow(File).to receive(:exists?).with(box_directory.join('rootfs.tar.gz').to_s).and_return(false) expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RootFSTarballMissing) end it 'does not raise an error if the lxc-template script cant be found' do - File.stub(:exists?).with(box_directory.join('lxc-template').to_s).and_return(false) + allow(File).to receive(:exists?).with(box_directory.join('lxc-template').to_s).and_return(false) expect { subject.call(env) }.to_not raise_error diff --git a/spec/unit/action/setup_package_files_spec.rb b/spec/unit/action/setup_package_files_spec.rb index 00f421a..44db639 100644 --- a/spec/unit/action/setup_package_files_spec.rb +++ b/spec/unit/action/setup_package_files_spec.rb @@ -30,24 +30,24 @@ describe Vagrant::LXC::Action::SetupPackageFiles do before { subject.call(env) } it 'copies box lxc-template to package directory' do - env['package.directory'].join('lxc-template').should be_file + expect(env['package.directory'].join('lxc-template')).to be_file end it 'copies metadata.json to package directory' do - env['package.directory'].join('metadata.json').should be_file + expect(env['package.directory'].join('metadata.json')).to be_file end it 'copies box lxc.conf to package directory' do - env['package.directory'].join('lxc-template').should be_file + expect(env['package.directory'].join('lxc-template')).to be_file end it 'copies box lxc-config to package directory' do - env['package.directory'].join('lxc-config').should be_file + expect(env['package.directory'].join('lxc-config')).to be_file end it 'moves the compressed rootfs to package directory' do - env['package.directory'].join(rootfs_path.basename).should be_file - env['package.rootfs'].should_not be_file + expect(env['package.directory'].join(rootfs_path.basename)).to be_file + expect(env['package.rootfs']).not_to be_file end end diff --git a/spec/unit/driver/cli_spec.rb b/spec/unit/driver/cli_spec.rb index e2f6c4a..e97fc33 100644 --- a/spec/unit/driver/cli_spec.rb +++ b/spec/unit/driver/cli_spec.rb @@ -13,18 +13,18 @@ describe Vagrant::LXC::Driver::CLI do let(:result) { @result } before do - subject.stub(:run).with(:ls).and_return(lxc_ls_out) + allow(subject).to receive(:run).with(:ls).and_return(lxc_ls_out) @result = subject.list end it 'grabs previously created containers from lxc-ls output' do - result.should be_an Enumerable - result.should include 'a-container' - result.should include 'dup-container' + expect(result).to be_an Enumerable + expect(result).to include 'a-container' + expect(result).to include 'dup-container' end it 'removes duplicates from lxc-ls output' do - result.uniq.should == result + expect(result.uniq).to eq(result) end end @@ -32,11 +32,11 @@ describe Vagrant::LXC::Driver::CLI do let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" } before do - subject.stub(:run).with(:version).and_return(lxc_version_out) + allow(subject).to receive(:run).with(:version).and_return(lxc_version_out) end it 'parses the version from the output' do - subject.version.should == '0.x.y-rc1' + expect(subject.version).to eq('0.x.y-rc1') end end @@ -49,12 +49,12 @@ describe Vagrant::LXC::Driver::CLI do subject { described_class.new(sudo_wrapper, name) } before do - subject.stub(:run) { |*args| @run_args = args } + allow(subject).to receive(:run) { |*args| @run_args = args } end it 'issues a lxc-create with provided template, container name and hash of arguments' do subject.create(template, config_file, template_args) - subject.should have_received(:run).with( + expect(subject).to have_received(:run).with( :create, '--template', template, '--name', name, @@ -66,7 +66,7 @@ describe Vagrant::LXC::Driver::CLI do end it 'wraps a low level error into something more meaningful in case the container already exists' do - subject.stub(:run) { raise Vagrant::LXC::Errors::ExecuteError, stderr: 'alreAdy Exists' } + allow(subject).to receive(:run) { raise Vagrant::LXC::Errors::ExecuteError, stderr: 'alreAdy Exists' } expect { subject.create(template, config_file, template_args) }.to raise_error(Vagrant::LXC::Errors::ContainerAlreadyExists) @@ -79,12 +79,12 @@ describe Vagrant::LXC::Driver::CLI do subject { described_class.new(sudo_wrapper, name) } before do - subject.stub(:run) + allow(subject).to receive(:run) subject.destroy end it 'issues a lxc-destroy with container name' do - subject.should have_received(:run).with(:destroy, '--name', name) + expect(subject).to have_received(:run).with(:destroy, '--name', name) end end @@ -93,12 +93,12 @@ describe Vagrant::LXC::Driver::CLI do subject { described_class.new(sudo_wrapper, name) } before do - subject.stub(:run) + allow(subject).to receive(:run) end it 'starts container on the background' do subject.start - subject.should have_received(:run).with( + expect(subject).to have_received(:run).with( :start, '-d', '--name', name @@ -112,16 +112,16 @@ describe Vagrant::LXC::Driver::CLI do before do subject.stub(system: true) - subject.stub(:run) + allow(subject).to receive(:run) end it 'issues a lxc-shutdown with provided container name' do subject.shutdown - subject.should have_received(:run).with(:shutdown, '--name', name) + expect(subject).to have_received(:run).with(:shutdown, '--name', name) end it 'raises a ShutdownNotSupported in case it is not supported' do - subject.stub(:system).with('which lxc-shutdown > /dev/null').and_return(false) + allow(subject).to receive(:system).with('which lxc-shutdown > /dev/null').and_return(false) expect { subject.shutdown }.to raise_error(described_class::ShutdownNotSupported) end end @@ -131,21 +131,21 @@ describe Vagrant::LXC::Driver::CLI do subject { described_class.new(sudo_wrapper, name) } before do - subject.stub(:run).and_return("state: STOPPED\npid: 2") + allow(subject).to receive(:run).and_return("state: STOPPED\npid: 2") end it 'calls lxc-info with the right arguments' do subject.state - subject.should have_received(:run).with(:info, '--name', name, retryable: true) + expect(subject).to have_received(:run).with(:info, '--name', name, retryable: true) end it 'maps the output of lxc-info status out to a symbol' do - subject.state.should == :stopped + expect(subject.state).to eq(:stopped) end it 'is not case sensitive' do - subject.stub(:run).and_return("StatE: STarTED\npid: 2") - subject.state.should == :started + allow(subject).to receive(:run).and_return("StatE: STarTED\npid: 2") + expect(subject.state).to eq(:started) end end @@ -161,17 +161,17 @@ describe Vagrant::LXC::Driver::CLI do it 'calls lxc-attach with specified command' do subject.attach(*command) - subject.should have_received(:run).with(:attach, '--name', name, '--', *command) + expect(subject).to have_received(:run).with(:attach, '--name', name, '--', *command) end it 'supports a "namespaces" parameter' do - subject.stub(:run).with(:attach, '-h', :show_stderr => true).and_return({:stdout => '', :stderr => '--namespaces'}) + allow(subject).to receive(:run).with(:attach, '-h', :show_stderr => true).and_return({:stdout => '', :stderr => '--namespaces'}) subject.attach *(command + [{namespaces: ['network', 'mount']}]) - subject.should have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command) + expect(subject).to have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command) end it 'raises a NamespacesNotSupported error if not supported' do - subject.stub(:run).with(:attach, '-h', :show_stderr => true).and_return({:stdout => '', :stderr => 'not supported'}) + allow(subject).to receive(:run).with(:attach, '-h', :show_stderr => true).and_return({:stdout => '', :stderr => 'not supported'}) expect { subject.attach *(command + [{namespaces: ['network', 'mount']}]) }.to raise_error(Vagrant::LXC::Errors::NamespacesNotSupported) @@ -187,9 +187,9 @@ describe Vagrant::LXC::Driver::CLI do end it 'yields a cli object' do - subject.stub(:shutdown) + allow(subject).to receive(:shutdown) subject.transition_to(:stopped) { |c| c.shutdown } - subject.should have_received(:shutdown) + expect(subject).to have_received(:shutdown) end it 'throws an exception if block is not provided' do diff --git a/spec/unit/driver_spec.rb b/spec/unit/driver_spec.rb index 8d61013..c56fc63 100644 --- a/spec/unit/driver_spec.rb +++ b/spec/unit/driver_spec.rb @@ -42,16 +42,16 @@ describe Vagrant::LXC::Driver do subject { described_class.new(nil, nil, cli) } before do - subject.stub(:import_template).and_yield(template_name) + allow(subject).to receive(:import_template).and_yield(template_name) subject.create name, template_path, config_file, template_opts end it 'sets the cli object container name' do - cli.should have_received(:name=).with(name) + expect(cli).to have_received(:name=).with(name) end it 'creates container with the right arguments' do - cli.should have_received(:create).with( + expect(cli).to have_received(:create).with( template_name, config_file, template_opts @@ -67,7 +67,7 @@ describe Vagrant::LXC::Driver do before { subject.destroy } it 'delegates to cli object' do - cli.should have_received(:destroy) + expect(cli).to have_received(:destroy) end end @@ -89,7 +89,7 @@ describe Vagrant::LXC::Driver do it 'writes configurations to config file' it 'starts container with configured customizations' do - cli.should have_received(:start) + expect(cli).to have_received(:start) end end @@ -99,30 +99,30 @@ describe Vagrant::LXC::Driver do subject { described_class.new('name', nil, cli) } before do - cli.stub(:transition_to).and_yield(cli) + allow(cli).to receive(:transition_to).and_yield(cli) end it 'delegates to cli shutdown' do - cli.should_receive(:shutdown) + expect(cli).to receive(:shutdown) subject.forced_halt end it 'expects a transition to running state to take place' do - cli.should_receive(:transition_to).with(:stopped) + expect(cli).to receive(:transition_to).with(:stopped) subject.forced_halt end it 'attempts to force the container to stop in case a shutdown doesnt work' do - cli.stub(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::TargetStateNotReached.new :target, :source) - cli.should_receive(:transition_to).with(:stopped).twice - cli.should_receive(:stop) + allow(cli).to receive(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::TargetStateNotReached.new :target, :source) + expect(cli).to receive(:transition_to).with(:stopped).twice + expect(cli).to receive(:stop) subject.forced_halt end it 'attempts to force the container to stop in case lxc-shutdown is not supported' do - cli.stub(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::ShutdownNotSupported) - cli.should_receive(:transition_to).with(:stopped).twice - cli.should_receive(:stop) + allow(cli).to receive(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::ShutdownNotSupported) + expect(cli).to receive(:transition_to).with(:stopped).twice + expect(cli).to receive(:stop) subject.forced_halt end end @@ -134,7 +134,7 @@ describe Vagrant::LXC::Driver do subject { described_class.new('name', nil, cli) } it 'delegates to cli' do - subject.state.should == cli_state + expect(subject.state).to eq(cli_state) end end @@ -154,18 +154,18 @@ describe Vagrant::LXC::Driver do end it "creates guest folder under container's rootfs" do - sudo_wrapper.should have_received(:run).with("mkdir", "-p", expected_guest_path) + expect(sudo_wrapper).to have_received(:run).with("mkdir", "-p", expected_guest_path) end it 'adds a mount.entry to its local customizations' do - subject.customizations.should include [ + expect(subject.customizations).to include [ 'mount.entry', "#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0" ] end it 'supports additional mount options' do - subject.customizations.should include [ + expect(subject.customizations).to include [ 'mount.entry', "#{ro_rw_folder[:hostpath]} #{rootfs_path}/vagrant/ro_rw none ro,rw 0 0" ] diff --git a/spec/unit/support/unit_example_group.rb b/spec/unit/support/unit_example_group.rb index bc390bc..9727637 100644 --- a/spec/unit/support/unit_example_group.rb +++ b/spec/unit/support/unit_example_group.rb @@ -2,23 +2,23 @@ module UnitExampleGroup def self.included(base) base.metadata[:type] = :unit base.before do - Object.any_instance.stub(:system) { |*args, &block| + allow_any_instance_of(Object).to receive(:system) { |instance, *args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } - Object.any_instance.stub(:`) { |*args, &block| + allow_any_instance_of(Object).to receive(:`) { |instance, *args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } - Object.any_instance.stub(:exec) { |*args, &block| + allow_any_instance_of(Object).to receive(:exec) { |instance, *args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } - Object.any_instance.stub(:fork) { |*args, &block| + allow_any_instance_of(Object).to receive(:fork) { |instance, *args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } - Object.any_instance.stub(:spawn) { |*args, &block| + allow_any_instance_of(Object).to receive(:spawn) { |instance, *args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } require 'vagrant/util/subprocess' - Vagrant::Util::Subprocess.stub(:execute) { |*args, &block| + allow(Vagrant::Util::Subprocess).to receive(:execute) { |*args, &block| UnitExampleGroup.prevent_system_calls(*args, &block) } end diff --git a/spec/unit_helper.rb b/spec/unit_helper.rb index 844be91..a1b7ad8 100644 --- a/spec/unit_helper.rb +++ b/spec/unit_helper.rb @@ -10,4 +10,8 @@ RSpec.configure do |config| config.include UnitExampleGroup, :type => :unit, :example_group => { :file_path => /\bspec\/unit\// } + + config.mock_with :rspec do |c| + c.yield_receiver_to_any_instance_implementation_blocks = true + end end