Skip to content

Commit 9b85961

Browse files
committed
Create space for non-title_pattern testing
1 parent 7f24c8c commit 9b85961

File tree

1 file changed

+152
-135
lines changed

1 file changed

+152
-135
lines changed

spec/puppet/resource_api_spec.rb

Lines changed: 152 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,21 +1022,13 @@ def set(_context, changes) end
10221022
end
10231023
end
10241024

1025-
context 'when registering a type with title_patterns', agent_test: true do
1025+
context 'when registering a type with multiple namevars', agent_test: true do
1026+
let(:name) { 'multiple' }
1027+
let(:title_patterns) { nil }
10261028
let(:definition) do
1027-
{
1028-
name: 'composite',
1029+
result = {
1030+
name: name,
10291031
desc: 'some desc',
1030-
title_patterns: [
1031-
{
1032-
pattern: %r{^(?<package>.*[^/])/(?<manager>.*)$},
1033-
desc: 'Where the package and the manager are provided with a slash separator',
1034-
},
1035-
{
1036-
pattern: %r{^(?<package>.*)$},
1037-
desc: 'Where only the package is provided',
1038-
},
1039-
],
10401032
attributes: {
10411033
ensure: {
10421034
type: 'Enum[present, absent]',
@@ -1054,190 +1046,215 @@ def set(_context, changes) end
10541046
},
10551047
},
10561048
}
1049+
result[:title_patterns] = title_patterns if title_patterns
1050+
result
10571051
end
10581052

10591053
before(:each) do
10601054
described_class.register_type(definition)
10611055
end
10621056

10631057
describe 'the registered type' do
1064-
subject(:type) { Puppet::Type.type(:composite) }
1058+
subject(:type) { Puppet::Type.type(:with_patterns) }
10651059

10661060
it { is_expected.not_to be_nil }
10671061
it { expect(type.parameters).to eq [:package, :manager] }
10681062
end
10691063

1070-
describe 'the type\'s class' do
1071-
let(:provider_class) do
1072-
Class.new do
1073-
def get(_context)
1074-
[{ title: 'php/yum', package: 'php', manager: 'yum', ensure: 'present' }]
1075-
end
1064+
context 'with title_patterns' do
1065+
let(:name) { 'with_patterns' }
1066+
let(:title_patterns) {
1067+
[
1068+
{
1069+
pattern: %r{^(?<package>.*[^/])/(?<manager>.*)$},
1070+
desc: 'Where the package and the manager are provided with a slash separator',
1071+
},
1072+
{
1073+
pattern: %r{^(?<package>.*)$},
1074+
desc: 'Where only the package is provided',
1075+
},
1076+
]
1077+
}
10761078

1077-
def set(_context, _changes); end
1078-
end
1079-
end
1080-
let(:type_class) { Puppet::Type.type(:composite) }
1079+
describe 'the registered type' do
1080+
subject(:type) { Puppet::Type.type(:with_patterns) }
10811081

1082-
before(:each) do
1083-
stub_const('Puppet::Provider::Composite', Module.new)
1084-
stub_const('Puppet::Provider::Composite::Composite', provider_class)
1082+
it { is_expected.not_to be_nil }
1083+
it { expect(type.parameters).to eq [:package, :manager] }
10851084
end
10861085

1087-
describe '.title_patterns' do
1088-
it 'returns correctly generated pattern' do
1089-
# [[ %r{^(?<package>.*[^/])/(?<manager>.*)$},[[:package],[:manager]]],[%r{^(?<package>.*)$},[[:package]]]]
1090-
1091-
expect(type_class.title_patterns.first[0]).to be_a Regexp
1092-
expect(type_class.title_patterns.first[0]).to eq(%r{^(?<package>.*[^/])/(?<manager>.*)$})
1093-
expect(type_class.title_patterns.first[1].size).to eq 2
1094-
expect(type_class.title_patterns.first[1][0][0]).to eq :package
1095-
expect(type_class.title_patterns.first[1][1][0]).to eq :manager
1086+
describe 'the type\'s class' do
1087+
let(:provider_class) do
1088+
Class.new do
1089+
def get(_context)
1090+
[{ title: 'php/yum', package: 'php', manager: 'yum', ensure: 'present' }]
1091+
end
10961092

1097-
expect(type_class.title_patterns.last[0]).to be_a Regexp
1098-
expect(type_class.title_patterns.last[0]).to eq(%r{^(?<package>.*)$})
1099-
expect(type_class.title_patterns.last[1].size).to eq 1
1100-
expect(type_class.title_patterns.last[1][0][0]).to eq :package
1093+
def set(_context, _changes); end
1094+
end
11011095
end
1102-
end
1096+
let(:type_class) { Puppet::Type.type(:with_patterns) }
11031097

1104-
describe '.instances' do
1105-
it 'uses the title provided by the provider' do
1106-
expect(type_class.instances[0].title).to eq('php/yum')
1098+
before(:each) do
1099+
stub_const('Puppet::Provider::WithPatterns', Module.new)
1100+
stub_const('Puppet::Provider::WithPatterns::WithPatterns', provider_class)
11071101
end
1108-
end
11091102

1110-
context 'when flushing an instance' do
1111-
let(:provider_instance) { instance_double(provider_class, 'provider_instance') }
1103+
describe '.title_patterns' do
1104+
it 'returns correctly generated pattern' do
1105+
# [[ %r{^(?<package>.*[^/])/(?<manager>.*)$},[[:package],[:manager]]],[%r{^(?<package>.*)$},[[:package]]]]
11121106

1113-
before(:each) do
1114-
allow(provider_class).to receive(:new).and_return(provider_instance)
1115-
end
1107+
expect(type_class.title_patterns.first[0]).to be_a Regexp
1108+
expect(type_class.title_patterns.first[0]).to eq(%r{^(?<package>.*[^/])/(?<manager>.*)$})
1109+
expect(type_class.title_patterns.first[1].size).to eq 2
1110+
expect(type_class.title_patterns.first[1][0][0]).to eq :package
1111+
expect(type_class.title_patterns.first[1][1][0]).to eq :manager
11161112

1117-
after(:each) do
1118-
# reset cached provider between tests
1119-
type_class.instance_variable_set(:@my_provider, nil)
1113+
expect(type_class.title_patterns.last[0]).to be_a Regexp
1114+
expect(type_class.title_patterns.last[0]).to eq(%r{^(?<package>.*)$})
1115+
expect(type_class.title_patterns.last[1].size).to eq 1
1116+
expect(type_class.title_patterns.last[1][0][0]).to eq :package
1117+
end
11201118
end
11211119

1122-
it 'uses a hash as `name` when setting values' do
1123-
allow(provider_instance).to receive(:get).and_return([{ title: 'php/yum', package: 'php', manager: 'yum', ensure: 'present' }])
1124-
expect(provider_instance).to receive(:set) { |_context, changes|
1125-
expect(changes.keys).to eq [{ package: 'php', manager: 'yum' }]
1126-
}
1127-
type_class.new(title: 'php/yum', ensure: :absent).flush
1120+
describe '.instances' do
1121+
it 'uses the title provided by the provider' do
1122+
expect(type_class.instances[0].title).to eq('php/yum')
1123+
end
11281124
end
1129-
end
11301125

1131-
context 'when no title is returned by get' do
1132-
let(:provider_class) do
1133-
Class.new do
1134-
def get(_context)
1135-
[{ package: 'php', manager: 'yum', ensure: 'present' }]
1136-
end
1126+
context 'when flushing an instance' do
1127+
let(:provider_instance) { instance_double(provider_class, 'provider_instance') }
11371128

1138-
def set(_context, _changes); end
1129+
before(:each) do
1130+
allow(provider_class).to receive(:new).and_return(provider_instance)
11391131
end
1140-
end
1141-
let(:type) { type_class.new(title: 'mytitle', package: 'php', manager: 'yum') }
11421132

1143-
context 'when Puppet strict setting is :off' do
1144-
let(:strict_level) { :off }
1145-
1146-
it 'instances will not log a warning' do
1147-
expect(Puppet).not_to receive(:warning)
1148-
type_class.instances
1133+
after(:each) do
1134+
# reset cached provider between tests
1135+
type_class.instance_variable_set(:@my_provider, nil)
11491136
end
11501137

1151-
it 'refresh_current_state will not log a warning' do
1152-
expect(Puppet).not_to receive(:warning)
1153-
type.refresh_current_state
1138+
it 'uses a hash as `name` when setting values' do
1139+
allow(provider_instance).to receive(:get).and_return([{ title: 'php/yum', package: 'php', manager: 'yum', ensure: 'present' }])
1140+
expect(provider_instance).to receive(:set) { |_context, changes|
1141+
expect(changes.keys).to eq [{ package: 'php', manager: 'yum' }]
1142+
}
1143+
type_class.new(title: 'php/yum', ensure: :absent).flush
11541144
end
11551145
end
11561146

1157-
context 'when Puppet strict setting is :error' do
1158-
let(:strict_level) { :error }
1147+
context 'when no title is returned by get' do
1148+
let(:provider_class) do
1149+
Class.new do
1150+
def get(_context)
1151+
[{ package: 'php', manager: 'yum', ensure: 'present' }]
1152+
end
11591153

1160-
it 'instances will throw an exception' do
1161-
expect {
1162-
type_class.instances
1163-
}.to raise_error(Puppet::DevError, %r{has not provided a title attribute})
1154+
def set(_context, _changes); end
1155+
end
11641156
end
1157+
let(:type) { type_class.new(title: 'mytitle', package: 'php', manager: 'yum') }
11651158

1166-
it 'refresh_current_state will throw an exception' do
1167-
expect {
1159+
context 'when Puppet strict setting is :off' do
1160+
let(:strict_level) { :off }
1161+
1162+
it 'instances will not log a warning' do
1163+
expect(Puppet).not_to receive(:warning)
1164+
type_class.instances
1165+
end
1166+
1167+
it 'refresh_current_state will not log a warning' do
1168+
expect(Puppet).not_to receive(:warning)
11681169
type.refresh_current_state
1169-
}.to raise_error(Puppet::DevError, %r{has not provided a title attribute})
1170+
end
11701171
end
1171-
end
11721172

1173-
context 'when Puppet strict setting is :warning' do
1174-
let(:strict_level) { :warning }
1173+
context 'when Puppet strict setting is :error' do
1174+
let(:strict_level) { :error }
11751175

1176-
it 'instances will not log a warning' do
1177-
expect(Puppet).to receive(:warning).with(%r{has not provided a title attribute})
1178-
type_class.instances
1179-
end
1176+
it 'instances will throw an exception' do
1177+
expect {
1178+
type_class.instances
1179+
}.to raise_error(Puppet::DevError, %r{has not provided a title attribute})
1180+
end
11801181

1181-
it 'refresh_current_state will not log a warning' do
1182-
expect(Puppet).to receive(:warning).with(%r{has not provided a title attribute})
1183-
type.refresh_current_state
1182+
it 'refresh_current_state will throw an exception' do
1183+
expect {
1184+
type.refresh_current_state
1185+
}.to raise_error(Puppet::DevError, %r{has not provided a title attribute})
1186+
end
11841187
end
1185-
end
1186-
end
11871188

1188-
context 'when the title does not match a title pattern' do
1189-
let(:provider_class) do
1190-
Class.new do
1191-
def get(_context)
1192-
[{ title: 'Nomatch', package: 'php', manager: 'yum', ensure: 'present' }]
1189+
context 'when Puppet strict setting is :warning' do
1190+
let(:strict_level) { :warning }
1191+
1192+
it 'instances will not log a warning' do
1193+
expect(Puppet).to receive(:warning).with(%r{has not provided a title attribute})
1194+
type_class.instances
11931195
end
11941196

1195-
def set(_context, _changes); end
1197+
it 'refresh_current_state will not log a warning' do
1198+
expect(Puppet).to receive(:warning).with(%r{has not provided a title attribute})
1199+
type.refresh_current_state
1200+
end
11961201
end
11971202
end
1198-
let(:type) { type_class.new(title: 'mytitle', package: 'php', manager: 'yum') }
11991203

1200-
context 'when Puppet strict setting is :off' do
1201-
let(:strict_level) { :off }
1204+
context 'when the title does not match a title pattern' do
1205+
let(:provider_class) do
1206+
Class.new do
1207+
def get(_context)
1208+
[{ title: 'Nomatch', package: 'php', manager: 'yum', ensure: 'present' }]
1209+
end
12021210

1203-
it 'instances will not log a warning' do
1204-
expect(Puppet).not_to receive(:warning)
1205-
type_class.instances
1206-
end
1207-
1208-
it 'refresh_current_state will not log a warning' do
1209-
expect(Puppet).not_to receive(:warning)
1210-
type.refresh_current_state
1211+
def set(_context, _changes); end
1212+
end
12111213
end
1212-
end
1214+
let(:type) { type_class.new(title: 'mytitle', package: 'php', manager: 'yum') }
12131215

1214-
context 'when Puppet strict setting is :error' do
1215-
let(:strict_level) { :error }
1216+
context 'when Puppet strict setting is :off' do
1217+
let(:strict_level) { :off }
12161218

1217-
it 'instances will throw an exception' do
1218-
expect {
1219+
it 'instances will not log a warning' do
1220+
expect(Puppet).not_to receive(:warning)
12191221
type_class.instances
1220-
}.to raise_error(Puppet::DevError, %r{has provided a title attribute which does not match})
1221-
end
1222+
end
12221223

1223-
it 'refresh_current_state will throw an exception' do
1224-
expect {
1224+
it 'refresh_current_state will not log a warning' do
1225+
expect(Puppet).not_to receive(:warning)
12251226
type.refresh_current_state
1226-
}.to raise_error(Puppet::DevError, %r{has provided a title attribute which does not match})
1227+
end
12271228
end
1228-
end
12291229

1230-
context 'when Puppet strict setting is :warning' do
1231-
let(:strict_level) { :warning }
1230+
context 'when Puppet strict setting is :error' do
1231+
let(:strict_level) { :error }
1232+
1233+
it 'instances will throw an exception' do
1234+
expect {
1235+
type_class.instances
1236+
}.to raise_error(Puppet::DevError, %r{has provided a title attribute which does not match})
1237+
end
12321238

1233-
it 'instances will not log a warning' do
1234-
expect(Puppet).to receive(:warning).with(%r{has provided a title attribute which does not match})
1235-
type_class.instances
1239+
it 'refresh_current_state will throw an exception' do
1240+
expect {
1241+
type.refresh_current_state
1242+
}.to raise_error(Puppet::DevError, %r{has provided a title attribute which does not match})
1243+
end
12361244
end
12371245

1238-
it 'refresh_current_state will not log a warning' do
1239-
expect(Puppet).to receive(:warning).with(%r{has provided a title attribute which does not match})
1240-
type.refresh_current_state
1246+
context 'when Puppet strict setting is :warning' do
1247+
let(:strict_level) { :warning }
1248+
1249+
it 'instances will not log a warning' do
1250+
expect(Puppet).to receive(:warning).with(%r{has provided a title attribute which does not match})
1251+
type_class.instances
1252+
end
1253+
1254+
it 'refresh_current_state will not log a warning' do
1255+
expect(Puppet).to receive(:warning).with(%r{has provided a title attribute which does not match})
1256+
type.refresh_current_state
1257+
end
12411258
end
12421259
end
12431260
end

0 commit comments

Comments
 (0)