Skip to content

Commit 43fb939

Browse files
committed
Fix CI
File#exist? is called in many locations. If we want to mock its behavior for some use cases, we must ensure that only the specific test case is mocked and the other calls still rely on the original implementation.
1 parent caea941 commit 43fb939

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

spec/functions/loadjson_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
end
2727

2828
before(:each) do
29+
allow(File).to receive(:exist?).and_call_original
2930
allow(File).to receive(:exist?).with(filename).and_return(false).once
3031
if Puppet::PUPPETVERSION[0].to_i < 8
3132
allow(PSON).to receive(:load).never # rubocop:disable RSpec/ReceiveNever Switching to not_to receive breaks testing in this case
@@ -51,6 +52,7 @@
5152
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
5253

5354
before(:each) do
55+
allow(File).to receive(:exist?).and_call_original
5456
allow(File).to receive(:exist?).with(filename).and_return(true).once
5557
allow(File).to receive(:read).with(filename).and_return(json).once
5658
allow(File).to receive(:read).with(filename).and_return(json).once
@@ -75,6 +77,7 @@
7577
let(:json) { '{"key":"value"}' }
7678

7779
before(:each) do
80+
allow(File).to receive(:exist?).and_call_original
7881
allow(File).to receive(:exist?).with(filename).and_return(true).once
7982
allow(File).to receive(:read).with(filename).and_return(json).once
8083
if Puppet::PUPPETVERSION[0].to_i < 8

spec/functions/loadyaml_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
let(:filename) { '/tmp/doesnotexist' }
1111

1212
it "'default' => 'value'" do
13+
allow(File).to receive(:exist?).and_call_original
1314
expect(File).to receive(:exist?).with(filename).and_return(false).once
1415
expect(YAML).not_to receive(:load_file)
1516
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
@@ -21,6 +22,7 @@
2122
let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
2223

2324
it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do
25+
allow(File).to receive(:exist?).and_call_original
2426
expect(File).to receive(:exist?).with(filename).and_return(true).once
2527
expect(YAML).to receive(:load_file).with(filename).and_return(data).once
2628
expect(subject).to run.with_params(filename).and_return(data)
@@ -31,6 +33,7 @@
3133
let(:filename) { '/tmp/doesexist' }
3234

3335
it 'filename /tmp/doesexist' do
36+
allow(File).to receive(:exist?).and_call_original
3437
expect(File).to receive(:exist?).with(filename).and_return(true).once
3538
allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!')
3639
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')

0 commit comments

Comments
 (0)