Skip to content

Commit 51ebf2d

Browse files
committed
(maint) Switch to rspec-mocks
1 parent 7c3c42c commit 51ebf2d

File tree

9 files changed

+77
-89
lines changed

9 files changed

+77
-89
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Style/SymbolArray:
7777
EnforcedStyle: brackets
7878
RSpec/NamedSubject:
7979
Enabled: false
80+
RSpec/SubjectStub:
81+
Enabled: false
8082
RSpec/MessageSpies:
8183
EnforcedStyle: receive
8284
Style/Documentation:

spec/integration/provider/cron/crontab_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
include PuppetSpec::Compiler
88

99
before :each do
10-
Puppet::Type.type(:cron).stubs(:defaultprovider).returns described_class
11-
described_class.stubs(:suitable?).returns true
12-
Puppet::FileBucket::Dipper.any_instance.stubs(:backup) # rubocop:disable RSpec/AnyInstance
10+
allow(Puppet::Type.type(:cron)).to receive(:defaultprovider).and_return described_class
11+
allow(described_class).to receive(:suitable?).and_return true
12+
allow_any_instance_of(Puppet::FileBucket::Dipper).to receive(:backup) # rubocop:disable RSpec/AnyInstance
1313

1414
# I don't want to execute anything
15-
described_class.stubs(:filetype).returns Puppet::Util::FileType::FileTypeFlat
16-
described_class.stubs(:default_target).returns crontab_user1
15+
allow(described_class).to receive(:filetype).and_return Puppet::Util::FileType::FileTypeFlat
16+
allow(described_class).to receive(:default_target).and_return crontab_user1
1717

1818
# I don't want to stub Time.now to get a static header because I don't know
1919
# where Time.now is used elsewhere, so just go with a very simple header
20-
described_class.stubs(:header).returns "# HEADER: some simple\n# HEADER: header\n"
20+
allow(described_class).to receive(:header).and_return "# HEADER: some simple\n# HEADER: header\n"
2121
FileUtils.cp(my_fixture('crontab_user1'), crontab_user1)
2222
FileUtils.cp(my_fixture('crontab_user2'), crontab_user2)
2323
end
@@ -113,9 +113,9 @@ def expect_output(fixture_name)
113113
MANIFEST
114114
apply_compiled_manifest(manifest) do |res|
115115
if res.ref == 'Cron[Entirely new resource]'
116-
res.expects(:err).with(regexp_matches(%r{no command}))
116+
expect(res).to receive(:err).with(%r{no command})
117117
else
118-
res.expects(:err).never
118+
expect(res).not_to receive(:err)
119119
end
120120
end
121121
end

spec/lib/puppet_spec/compiler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def apply_compiled_manifest(manifest, prioritizer = Puppet::Graph::SequentialPri
3333
if Puppet.version.to_f < 5.0
3434
args << 'apply'
3535
# rubocop:disable RSpec/AnyInstance
36-
Puppet::Transaction::Persistence.any_instance.stubs(:save)
36+
allow_any_instance_of(Puppet::Transaction::Persistence).to receive(:save)
3737
# rubocop:enable RSpec/AnyInstance
3838
end
3939
catalog = compile_to_ral(manifest)
@@ -51,7 +51,7 @@ def apply_compiled_manifest(manifest, prioritizer = Puppet::Graph::SequentialPri
5151

5252
def apply_with_error_check(manifest)
5353
apply_compiled_manifest(manifest) do |res|
54-
res.expects(:err).never
54+
expect(res).not_to receive(:err)
5555
end
5656
end
5757

spec/lib/puppet_spec/files.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def self.cleanup
1010
$global_tempfiles ||= []
1111
$global_tempfiles.each do |path|
1212
begin
13-
Dir.unstub(:entries)
13+
allow(Dir).to receive(:entries).and_call_original
1414
FileUtils.rm_rf path, secure: true
1515
rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions
1616
# nothing to do

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
end
3232

3333
RSpec.configure do |c|
34+
c.mock_with :rspec
3435
c.default_facts = default_facts
3536
c.before :each do
3637
# set to strictest setting for testing

spec/unit/provider/cron/crontab_spec.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def compare_crontab_record(have, want)
125125
let(:resources) { { 'test' => resource } }
126126

127127
before :each do
128-
subject.stubs(:prefetch_all_targets).returns([record])
128+
allow(subject).to receive(:prefetch_all_targets).and_return([record])
129129
end
130130

131131
# this would be a more fitting test, but I haven't yet
@@ -141,34 +141,34 @@ def compare_crontab_record(have, want)
141141
# end
142142

143143
it "does not base the new resource's provider on the existing record" do
144-
subject.expects(:new).with(record).never
145-
subject.stubs(:new)
144+
expect(subject).not_to receive(:new).with(record)
145+
allow(subject).to receive(:new)
146146
subject.prefetch(resources)
147147
end
148148
end
149149

150150
context 'when prefetching an entry now managed for another user' do
151151
let(:resource) do
152-
s = stub(:resource)
153-
s.stubs(:[]).with(:user).returns 'root'
154-
s.stubs(:[]).with(:target).returns 'root'
152+
s = instance_double('Puppet::Type::Crontab')
153+
allow(s).to receive(:[]).with(:user).and_return 'root'
154+
allow(s).to receive(:[]).with(:target).and_return 'root'
155155
s
156156
end
157157

158158
let(:record) { { name: 'test', user: 'nobody', command: '/bin/true', record_type: :crontab } }
159159
let(:resources) { { 'test' => resource } }
160160

161161
before :each do
162-
subject.stubs(:prefetch_all_targets).returns([record])
162+
allow(subject).to receive(:prefetch_all_targets).and_return([record])
163163
end
164164

165165
it 'tries and use the match method to find a more fitting record' do
166-
subject.expects(:match).with(record, resources)
166+
expect(subject).to receive(:match).with(record, resources)
167167
subject.prefetch(resources)
168168
end
169169

170170
it 'does not match a provider to the resource' do
171-
resource.expects(:provider=).never
171+
expect(resource).not_to receive(:provider=)
172172
subject.prefetch(resources)
173173
end
174174

@@ -205,19 +205,17 @@ def compare_crontab_record(have, want)
205205

206206
context '#enumerate_crontabs' do
207207
before(:each) do
208-
File.expects(:readable?).with(subject.crontab_dir).returns(true)
209-
Dir.expects(:foreach).with(subject.crontab_dir).multiple_yields(*files)
208+
allow(File).to receive(:readable?).with(subject.crontab_dir).and_return(true)
210209
end
211210

212211
context 'only a hidden file' do
213-
let(:files) { ['.keep_cronbase-0'] }
212+
let(:file) { '.keep_cronbase-0' }
214213

215214
before(:each) do
216-
files.each do |filename|
217-
path = File.join(subject.crontab_dir, filename)
218-
File.expects(:file?).with(path).returns(true)
219-
File.expects(:writable?).with(path).returns(true)
220-
end
215+
path = File.join(subject.crontab_dir, file)
216+
allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(file)
217+
allow(File).to receive(:file?).with(path).and_return(true)
218+
allow(File).to receive(:writable?).with(path).and_return(true)
221219
end
222220

223221
it 'ignores .keep_* files' do
@@ -229,10 +227,12 @@ def compare_crontab_record(have, want)
229227
let(:files) { ['myuser', '.keep_cronbase-0'] }
230228

231229
before(:each) do
230+
allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(files[0]).and_yield(files[1])
231+
232232
files.each do |filename|
233233
path = File.join(subject.crontab_dir, filename)
234-
File.expects(:file?).with(path).returns(true)
235-
File.expects(:writable?).with(path).returns(true)
234+
allow(File).to receive(:file?).with(path).and_return(true)
235+
allow(File).to receive(:writable?).with(path).and_return(true)
236236
end
237237
end
238238

spec/unit/provider/cron/filetype_spec.rb

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,53 @@
1717
# make Puppet::Util::SUIDManager return something deterministic, not the
1818
# uid of the user running the tests, except where overridden below.
1919
before :each do
20-
Puppet::Util::SUIDManager.stubs(:uid).returns 1234
20+
allow(Puppet::Util::SUIDManager).to receive(:uid).and_return 1234
2121
end
2222

2323
describe '#read' do
2424
before(:each) do
25-
Puppet::Util.stubs(:uid).with(uid).returns 9000
25+
allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000
2626
end
2727

2828
it 'runs crontab -l as the target user' do
29-
Puppet::Util::Execution
30-
.expects(:execute)
31-
.with(['crontab', '-l'], user_options)
32-
.returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0))
33-
29+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0))
3430
expect(cron.read).to eq(crontab)
3531
end
3632

3733
it 'does not switch user if current user is the target user' do
38-
Puppet::Util.expects(:uid).with(uid).twice.returns 9000
39-
Puppet::Util::SUIDManager.expects(:uid).returns 9000
40-
Puppet::Util::Execution
41-
.expects(:execute).with(['crontab', '-l'], options)
42-
.returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0))
34+
expect(Puppet::Util).to receive(:uid).with(uid).twice.and_return 9000
35+
expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000
36+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0))
4337
expect(cron.read).to eq(crontab)
4438
end
4539

4640
it 'treats an absent crontab as empty' do
47-
Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, absent_crontab)
41+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, absent_crontab)
4842
expect(cron.read).to eq('')
4943
end
5044

5145
it "treats a nonexistent user's crontab as empty" do
52-
Puppet::Util.expects(:uid).with(uid).returns nil
46+
expect(Puppet::Util).to receive(:uid).with(uid).and_return nil
5347

5448
expect(cron.read).to eq('')
5549
end
5650

5751
it 'returns empty if the user is not authorized to use cron' do
58-
Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, unauthorized_crontab)
52+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, unauthorized_crontab)
5953
expect(cron.read).to eq('')
6054
end
6155
end
6256

6357
describe '#remove' do
6458
it 'runs crontab -r as the target user' do
65-
Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], user_options)
59+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], user_options)
6660
cron.remove
6761
end
6862

6963
it 'does not switch user if current user is the target user' do
70-
Puppet::Util.expects(:uid).with(uid).returns 9000
71-
Puppet::Util::SUIDManager.expects(:uid).returns 9000
72-
Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], options)
64+
expect(Puppet::Util).to receive(:uid).with(uid).and_return 9000
65+
expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000
66+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], options)
7367
cron.remove
7468
end
7569
end
@@ -79,30 +73,30 @@
7973
let!(:tmp_cron_path) { tmp_cron.path }
8074

8175
before :each do
82-
Puppet::Util.stubs(:uid).with(uid).returns 9000
83-
Tempfile.expects(:new).with("puppet_#{name}", encoding: Encoding.default_external).returns tmp_cron
76+
allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000
77+
allow(Tempfile).to receive(:new).with("puppet_#{name}", encoding: Encoding.default_external).and_return tmp_cron
8478
end
8579

8680
after :each do
87-
File.unstub(:chown)
81+
allow(File).to receive(:chown).and_call_original
8882
end
8983

9084
it 'runs crontab as the target user on a temporary file' do
91-
File.expects(:chown).with(9000, nil, tmp_cron_path)
92-
Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], user_options)
85+
expect(File).to receive(:chown).with(9000, nil, tmp_cron_path)
86+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], user_options)
9387

94-
tmp_cron.expects(:print).with("foo\n")
88+
expect(tmp_cron).to receive(:print).with("foo\n")
9589
cron.write "foo\n"
9690

9791
expect(Puppet::FileSystem).not_to exist(tmp_cron_path)
9892
end
9993

10094
it 'does not switch user if current user is the target user' do
101-
Puppet::Util::SUIDManager.expects(:uid).returns 9000
102-
File.expects(:chown).with(9000, nil, tmp_cron_path)
103-
Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], options)
95+
expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000
96+
expect(File).to receive(:chown).with(9000, nil, tmp_cron_path)
97+
expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], options)
10498

105-
tmp_cron.expects(:print).with("foo\n")
99+
expect(tmp_cron).to receive(:print).with("foo\n")
106100
cron.write "foo\n"
107101

108102
expect(Puppet::FileSystem).not_to exist(tmp_cron_path)

spec/unit/provider/cron/parsed_spec.rb

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@
6363

6464
describe 'when determining the correct filetype' do
6565
it 'uses the suntab filetype on Solaris' do
66-
Facter.stubs(:value).with(:osfamily).returns 'Solaris'
66+
allow(Facter).to receive(:value).with(:osfamily).and_return 'Solaris'
6767
expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeSuntab)
6868
end
6969

7070
it 'uses the aixtab filetype on AIX' do
71-
Facter.stubs(:value).with(:osfamily).returns 'AIX'
71+
allow(Facter).to receive(:value).with(:osfamily).and_return 'AIX'
7272
expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeAixtab)
7373
end
7474

7575
it 'uses the crontab filetype on other platforms' do
76-
Facter.stubs(:value).with(:osfamily).returns 'Not a real operating system family'
76+
allow(Facter).to receive(:value).with(:osfamily).and_return 'Not a real operating system family'
7777
expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeCrontab)
7878
end
7979
end
8080

81-
# I'd use ENV.expects(:[]).with('USER') but this does not work because
81+
# I'd use expect(ENV).to receive(:[]).with('USER') but this does not work because
8282
# ENV["USER"] is evaluated at load time.
8383
describe 'when determining the default target' do
8484
it "should use the current user #{ENV['USER']}", if: ENV['USER'] do
@@ -94,16 +94,13 @@
9494
let(:tabs) { [described_class.default_target] + ['foo', 'bar'] }
9595

9696
before(:each) do
97-
File.expects(:readable?).returns true
98-
File.stubs(:file?).returns true
99-
File.stubs(:writable?).returns true
100-
end
101-
after(:each) do
102-
File.unstub :readable?, :file?, :writable?
103-
Dir.unstub :foreach
97+
allow(File).to receive(:readable?).and_return true
98+
allow(File).to receive(:file?).and_return true
99+
allow(File).to receive(:writable?).and_return true
104100
end
101+
105102
it 'adds all crontabs as targets' do
106-
Dir.expects(:foreach).multiple_yields(*tabs)
103+
expect(Dir).to receive(:foreach).and_yield(tabs[0]).and_yield(tabs[1]).and_yield(tabs[2])
107104
expect(described_class.targets).to eq(tabs)
108105
end
109106
end
@@ -185,36 +182,30 @@
185182

186183
describe '.instances' do
187184
before :each do
188-
described_class.stubs(:default_target).returns 'foobar'
185+
allow(described_class).to receive(:targets).and_return ['foobar']
189186
end
190187

191188
describe 'on linux' do
192189
before(:each) do
193-
Facter.stubs(:value).with(:osfamily).returns 'Linux'
194-
Facter.stubs(:value).with(:operatingsystem)
190+
allow(Facter).to receive(:value).with(:osfamily).and_return 'Linux'
191+
allow(Facter).to receive(:value).with(:operatingsystem)
195192
end
196193

197194
it 'contains no resources for a user who has no crontab' do
198-
Puppet::Util.stubs(:uid).returns(10)
195+
allow(Puppet::Util).to receive(:uid).and_return(10)
199196

200-
Puppet::Util::Execution
201-
.expects(:execute)
202-
.with('crontab -u foobar -l', failonfail: true, combine: true)
203-
.returns('')
204-
205-
expect(described_class.instances.select do |resource|
206-
resource.get('target') == 'foobar'
207-
end).to be_empty
197+
expect(Puppet::Util::Execution).to receive(:execute).with('crontab -u foobar -l', failonfail: true, combine: true).and_return('')
198+
expect(described_class.instances).to be_empty
208199
end
209200

210201
it 'contains no resources for a user who is absent' do
211-
Puppet::Util.stubs(:uid).returns(nil)
202+
allow(Puppet::Util).to receive(:uid).and_return(nil)
212203

213204
expect(described_class.instances).to be_empty
214205
end
215206

216207
it 'is able to create records from not-managed records' do
217-
described_class.stubs(:target_object).returns File.new(my_fixture('simple'))
208+
allow(described_class).to receive(:target_object).and_return File.new(my_fixture('simple'))
218209
parameters = described_class.instances.map do |p|
219210
h = { name: p.get(:name) }
220211
Puppet::Type.type(:cron).validproperties.each do |property|
@@ -250,7 +241,7 @@
250241
end
251242

252243
it 'is able to parse puppet managed cronjobs' do
253-
described_class.stubs(:target_object).returns File.new(my_fixture('managed'))
244+
allow(described_class).to receive(:target_object).and_return File.new(my_fixture('managed'))
254245
expect(described_class.instances.map do |p|
255246
h = { name: p.get(:name) }
256247
Puppet::Type.type(:cron).validproperties.each do |property|

0 commit comments

Comments
 (0)