From 51ebf2d8d5c02c7cd314edd9e6507163a8073785 Mon Sep 17 00:00:00 2001 From: Gabriel Nagy Date: Thu, 11 Mar 2021 14:17:09 +0200 Subject: (maint) Switch to rspec-mocks --- spec/unit/provider/cron/crontab_spec.rb | 38 +++++++++++------------ spec/unit/provider/cron/filetype_spec.rb | 52 ++++++++++++++------------------ spec/unit/provider/cron/parsed_spec.rb | 45 +++++++++++---------------- 3 files changed, 60 insertions(+), 75 deletions(-) (limited to 'spec/unit/provider') diff --git a/spec/unit/provider/cron/crontab_spec.rb b/spec/unit/provider/cron/crontab_spec.rb index 796a4c2..6e28393 100644 --- a/spec/unit/provider/cron/crontab_spec.rb +++ b/spec/unit/provider/cron/crontab_spec.rb @@ -125,7 +125,7 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:resources) { { 'test' => resource } } before :each do - subject.stubs(:prefetch_all_targets).returns([record]) + allow(subject).to receive(:prefetch_all_targets).and_return([record]) end # this would be a more fitting test, but I haven't yet @@ -141,17 +141,17 @@ describe Puppet::Type.type(:cron).provider(:crontab) do # end it "does not base the new resource's provider on the existing record" do - subject.expects(:new).with(record).never - subject.stubs(:new) + expect(subject).not_to receive(:new).with(record) + allow(subject).to receive(:new) subject.prefetch(resources) end end context 'when prefetching an entry now managed for another user' do let(:resource) do - s = stub(:resource) - s.stubs(:[]).with(:user).returns 'root' - s.stubs(:[]).with(:target).returns 'root' + s = instance_double('Puppet::Type::Crontab') + allow(s).to receive(:[]).with(:user).and_return 'root' + allow(s).to receive(:[]).with(:target).and_return 'root' s end @@ -159,16 +159,16 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:resources) { { 'test' => resource } } before :each do - subject.stubs(:prefetch_all_targets).returns([record]) + allow(subject).to receive(:prefetch_all_targets).and_return([record]) end it 'tries and use the match method to find a more fitting record' do - subject.expects(:match).with(record, resources) + expect(subject).to receive(:match).with(record, resources) subject.prefetch(resources) end it 'does not match a provider to the resource' do - resource.expects(:provider=).never + expect(resource).not_to receive(:provider=) subject.prefetch(resources) end @@ -205,19 +205,17 @@ describe Puppet::Type.type(:cron).provider(:crontab) do context '#enumerate_crontabs' do before(:each) do - File.expects(:readable?).with(subject.crontab_dir).returns(true) - Dir.expects(:foreach).with(subject.crontab_dir).multiple_yields(*files) + allow(File).to receive(:readable?).with(subject.crontab_dir).and_return(true) end context 'only a hidden file' do - let(:files) { ['.keep_cronbase-0'] } + let(:file) { '.keep_cronbase-0' } before(:each) do - files.each do |filename| - path = File.join(subject.crontab_dir, filename) - File.expects(:file?).with(path).returns(true) - File.expects(:writable?).with(path).returns(true) - end + path = File.join(subject.crontab_dir, file) + allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(file) + allow(File).to receive(:file?).with(path).and_return(true) + allow(File).to receive(:writable?).with(path).and_return(true) end it 'ignores .keep_* files' do @@ -229,10 +227,12 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:files) { ['myuser', '.keep_cronbase-0'] } before(:each) do + allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(files[0]).and_yield(files[1]) + files.each do |filename| path = File.join(subject.crontab_dir, filename) - File.expects(:file?).with(path).returns(true) - File.expects(:writable?).with(path).returns(true) + allow(File).to receive(:file?).with(path).and_return(true) + allow(File).to receive(:writable?).with(path).and_return(true) end end diff --git a/spec/unit/provider/cron/filetype_spec.rb b/spec/unit/provider/cron/filetype_spec.rb index bf96f91..bd579c1 100644 --- a/spec/unit/provider/cron/filetype_spec.rb +++ b/spec/unit/provider/cron/filetype_spec.rb @@ -17,59 +17,53 @@ describe Puppet::Provider::Cron::FileType do # make Puppet::Util::SUIDManager return something deterministic, not the # uid of the user running the tests, except where overridden below. before :each do - Puppet::Util::SUIDManager.stubs(:uid).returns 1234 + allow(Puppet::Util::SUIDManager).to receive(:uid).and_return 1234 end describe '#read' do before(:each) do - Puppet::Util.stubs(:uid).with(uid).returns 9000 + allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000 end it 'runs crontab -l as the target user' do - Puppet::Util::Execution - .expects(:execute) - .with(['crontab', '-l'], user_options) - .returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) - + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) expect(cron.read).to eq(crontab) end it 'does not switch user if current user is the target user' do - Puppet::Util.expects(:uid).with(uid).twice.returns 9000 - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - Puppet::Util::Execution - .expects(:execute).with(['crontab', '-l'], options) - .returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) + expect(Puppet::Util).to receive(:uid).with(uid).twice.and_return 9000 + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) expect(cron.read).to eq(crontab) end it 'treats an absent crontab as empty' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, absent_crontab) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, absent_crontab) expect(cron.read).to eq('') end it "treats a nonexistent user's crontab as empty" do - Puppet::Util.expects(:uid).with(uid).returns nil + expect(Puppet::Util).to receive(:uid).with(uid).and_return nil expect(cron.read).to eq('') end it 'returns empty if the user is not authorized to use cron' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, unauthorized_crontab) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, unauthorized_crontab) expect(cron.read).to eq('') end end describe '#remove' do it 'runs crontab -r as the target user' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], user_options) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], user_options) cron.remove end it 'does not switch user if current user is the target user' do - Puppet::Util.expects(:uid).with(uid).returns 9000 - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], options) + expect(Puppet::Util).to receive(:uid).with(uid).and_return 9000 + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], options) cron.remove end end @@ -79,30 +73,30 @@ describe Puppet::Provider::Cron::FileType do let!(:tmp_cron_path) { tmp_cron.path } before :each do - Puppet::Util.stubs(:uid).with(uid).returns 9000 - Tempfile.expects(:new).with("puppet_#{name}", encoding: Encoding.default_external).returns tmp_cron + allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000 + allow(Tempfile).to receive(:new).with("puppet_#{name}", encoding: Encoding.default_external).and_return tmp_cron end after :each do - File.unstub(:chown) + allow(File).to receive(:chown).and_call_original end it 'runs crontab as the target user on a temporary file' do - File.expects(:chown).with(9000, nil, tmp_cron_path) - Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], user_options) + expect(File).to receive(:chown).with(9000, nil, tmp_cron_path) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], user_options) - tmp_cron.expects(:print).with("foo\n") + expect(tmp_cron).to receive(:print).with("foo\n") cron.write "foo\n" expect(Puppet::FileSystem).not_to exist(tmp_cron_path) end it 'does not switch user if current user is the target user' do - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - File.expects(:chown).with(9000, nil, tmp_cron_path) - Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], options) + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(File).to receive(:chown).with(9000, nil, tmp_cron_path) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], options) - tmp_cron.expects(:print).with("foo\n") + expect(tmp_cron).to receive(:print).with("foo\n") cron.write "foo\n" expect(Puppet::FileSystem).not_to exist(tmp_cron_path) diff --git a/spec/unit/provider/cron/parsed_spec.rb b/spec/unit/provider/cron/parsed_spec.rb index b115f9d..e4fa4cd 100644 --- a/spec/unit/provider/cron/parsed_spec.rb +++ b/spec/unit/provider/cron/parsed_spec.rb @@ -63,22 +63,22 @@ describe Puppet::Type.type(:cron).provider(:crontab) do describe 'when determining the correct filetype' do it 'uses the suntab filetype on Solaris' do - Facter.stubs(:value).with(:osfamily).returns 'Solaris' + allow(Facter).to receive(:value).with(:osfamily).and_return 'Solaris' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeSuntab) end it 'uses the aixtab filetype on AIX' do - Facter.stubs(:value).with(:osfamily).returns 'AIX' + allow(Facter).to receive(:value).with(:osfamily).and_return 'AIX' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeAixtab) end it 'uses the crontab filetype on other platforms' do - Facter.stubs(:value).with(:osfamily).returns 'Not a real operating system family' + allow(Facter).to receive(:value).with(:osfamily).and_return 'Not a real operating system family' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeCrontab) end end - # I'd use ENV.expects(:[]).with('USER') but this does not work because + # I'd use expect(ENV).to receive(:[]).with('USER') but this does not work because # ENV["USER"] is evaluated at load time. describe 'when determining the default target' do it "should use the current user #{ENV['USER']}", if: ENV['USER'] do @@ -94,16 +94,13 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:tabs) { [described_class.default_target] + ['foo', 'bar'] } before(:each) do - File.expects(:readable?).returns true - File.stubs(:file?).returns true - File.stubs(:writable?).returns true - end - after(:each) do - File.unstub :readable?, :file?, :writable? - Dir.unstub :foreach + allow(File).to receive(:readable?).and_return true + allow(File).to receive(:file?).and_return true + allow(File).to receive(:writable?).and_return true end + it 'adds all crontabs as targets' do - Dir.expects(:foreach).multiple_yields(*tabs) + expect(Dir).to receive(:foreach).and_yield(tabs[0]).and_yield(tabs[1]).and_yield(tabs[2]) expect(described_class.targets).to eq(tabs) end end @@ -185,36 +182,30 @@ describe Puppet::Type.type(:cron).provider(:crontab) do describe '.instances' do before :each do - described_class.stubs(:default_target).returns 'foobar' + allow(described_class).to receive(:targets).and_return ['foobar'] end describe 'on linux' do before(:each) do - Facter.stubs(:value).with(:osfamily).returns 'Linux' - Facter.stubs(:value).with(:operatingsystem) + allow(Facter).to receive(:value).with(:osfamily).and_return 'Linux' + allow(Facter).to receive(:value).with(:operatingsystem) end it 'contains no resources for a user who has no crontab' do - Puppet::Util.stubs(:uid).returns(10) + allow(Puppet::Util).to receive(:uid).and_return(10) - Puppet::Util::Execution - .expects(:execute) - .with('crontab -u foobar -l', failonfail: true, combine: true) - .returns('') - - expect(described_class.instances.select do |resource| - resource.get('target') == 'foobar' - end).to be_empty + expect(Puppet::Util::Execution).to receive(:execute).with('crontab -u foobar -l', failonfail: true, combine: true).and_return('') + expect(described_class.instances).to be_empty end it 'contains no resources for a user who is absent' do - Puppet::Util.stubs(:uid).returns(nil) + allow(Puppet::Util).to receive(:uid).and_return(nil) expect(described_class.instances).to be_empty end it 'is able to create records from not-managed records' do - described_class.stubs(:target_object).returns File.new(my_fixture('simple')) + allow(described_class).to receive(:target_object).and_return File.new(my_fixture('simple')) parameters = described_class.instances.map do |p| h = { name: p.get(:name) } Puppet::Type.type(:cron).validproperties.each do |property| @@ -250,7 +241,7 @@ describe Puppet::Type.type(:cron).provider(:crontab) do end it 'is able to parse puppet managed cronjobs' do - described_class.stubs(:target_object).returns File.new(my_fixture('managed')) + allow(described_class).to receive(:target_object).and_return File.new(my_fixture('managed')) expect(described_class.instances.map do |p| h = { name: p.get(:name) } Puppet::Type.type(:cron).validproperties.each do |property| -- cgit v1.2.3