Skip to content

Commit f7b0b1f

Browse files
committed
Fix load_module_metadata and loadjson tests to pass with fully deployed module
When replacing the lib/ and manifests/ symlinks in the fixtures with a proper top-level symlink, puppet 4 starts loading the metadata.json before loading functions, which confuses these tests. Added more specific expectations, and provide data for that call.
1 parent 6bab96e commit f7b0b1f

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

spec/functions/load_module_metadata_spec.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
66
it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
77

8-
it "should json parse the file" do
9-
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
10-
allow(File).to receive(:exists?).with(/metadata.json/).and_return(true)
11-
allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}')
8+
describe "when calling with valid arguments" do
9+
before :each do
10+
allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
11+
end
12+
it "should json parse the file" do
13+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
14+
allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(true)
15+
allow(File).to receive(:read).with('/path/to/module/metadata.json').and_return('{"name": "spencer-science"}')
1216

13-
result = subject.call(['science'])
14-
expect(result['name']).to eq('spencer-science')
15-
end
17+
result = subject.call(['science'])
18+
expect(result['name']).to eq('spencer-science')
19+
end
1620

17-
it "should fail by default if there is no metadata.json" do
18-
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
19-
allow(File).to receive(:exists?).with(/metadata.json/).and_return(false)
20-
expect {subject.call(['science'])}.to raise_error(Puppet::ParseError)
21-
end
21+
it "should fail by default if there is no metadata.json" do
22+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
23+
allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
24+
expect {subject.call(['science'])}.to raise_error(Puppet::ParseError)
25+
end
2226

23-
it "should return nil if user allows empty metadata.json" do
24-
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
25-
allow(File).to receive(:exists?).with(/metadata.json/).and_return(false)
26-
result = subject.call(['science', true])
27-
expect(result).to eq({})
27+
it "should return nil if user allows empty metadata.json" do
28+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
29+
allow(File).to receive(:exists?).with('/path/to/module/metadata.json').and_return(false)
30+
result = subject.call(['science', true])
31+
expect(result).to eq({})
32+
end
2833
end
2934
end

spec/functions/loadjson_spec.rb

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,42 @@
44
it { is_expected.not_to eq(nil) }
55
it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
66

7-
context 'when a non-existing file is specified' do
8-
let(:filename) { '/tmp/doesnotexist' }
9-
before {
10-
File.expects(:exists?).with(filename).returns(false).once
11-
PSON.expects(:load).never
12-
}
13-
it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
14-
end
7+
describe "when calling with valid arguments" do
8+
before :each do
9+
allow(File).to receive(:read).with(/\/stdlib\/metadata.json/, {:encoding=>"utf-8"}).and_return('{"name": "puppetlabs-stdlib"}')
10+
end
1511

16-
context 'when an existing file is specified' do
17-
let(:filename) { '/tmp/doesexist' }
18-
let(:data) { { 'key' => 'value' } }
19-
let(:json) { '{"key":"value"}' }
20-
before {
21-
File.expects(:exists?).with(filename).returns(true).once
22-
File.expects(:read).with(filename).returns(json).once
23-
PSON.expects(:load).with(json).returns(data).once
24-
}
25-
it { is_expected.to run.with_params(filename).and_return(data) }
26-
end
12+
context 'when a non-existing file is specified' do
13+
let(:filename) { '/tmp/doesnotexist' }
14+
before {
15+
allow(File).to receive(:exists?).with(filename).and_return(false).once
16+
allow(PSON).to receive(:load).never
17+
}
18+
it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
19+
end
20+
21+
context 'when an existing file is specified' do
22+
let(:filename) { '/tmp/doesexist' }
23+
let(:data) { { 'key' => 'value' } }
24+
let(:json) { '{"key":"value"}' }
25+
before {
26+
allow(File).to receive(:exists?).with(filename).and_return(true).once
27+
allow(File).to receive(:read).with(filename).and_return(json).once
28+
allow(File).to receive(:read).with(filename).and_return(json).once
29+
allow(PSON).to receive(:load).with(json).and_return(data).once
30+
}
31+
it { is_expected.to run.with_params(filename).and_return(data) }
32+
end
2733

28-
context 'when the file could not be parsed' do
29-
let(:filename) { '/tmp/doesexist' }
30-
let(:json) { '{"key":"value"}' }
31-
before {
32-
File.expects(:exists?).with(filename).returns(true).once
33-
File.expects(:read).with(filename).returns(json).once
34-
PSON.stubs(:load).with(json).once.raises StandardError, 'Something terrible have happened!'
35-
}
36-
it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
34+
context 'when the file could not be parsed' do
35+
let(:filename) { '/tmp/doesexist' }
36+
let(:json) { '{"key":"value"}' }
37+
before {
38+
allow(File).to receive(:exists?).with(filename).and_return(true).once
39+
allow(File).to receive(:read).with(filename).and_return(json).once
40+
allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
41+
}
42+
it { is_expected.to run.with_params(filename, {'default' => 'value'}).and_return({'default' => 'value'}) }
43+
end
3744
end
3845
end

0 commit comments

Comments
 (0)