Skip to content

Commit 89c8808

Browse files
committed
(maint) implement desc/docs fallback
This implements the changes specified in puppetlabs/puppet-specifications#141 > The text and examples have been inconsistent with how `desc` vs `docs` > has been handled. This change fixes the text and examples to all show > and require `desc`, but accept `docs` in places where we showed it > previously.
1 parent 7bcd01a commit 89c8808

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

lib/puppet/resource_api.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def register_type(definition)
3737
unknown_features = definition[:features] - supported_features
3838
Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty?
3939

40+
# fixup desc/docs backwards compatibility
41+
if definition.key? :docs
42+
if definition[:desc]
43+
raise Puppet::DevError, '`%{name}` has both `desc` and `docs`, prefer using `desc`' % { name: definition[:name] }
44+
end
45+
definition[:desc] = definition[:docs]
46+
definition.delete(:docs)
47+
end
48+
Puppet.warning('`%{name}` has no documentation, add it using a `desc` key' % { name: definition[:name] }) unless definition.key? :desc
49+
4050
# fixup any weird behavior ;-)
4151
definition[:attributes].each do |name, attr|
4252
next unless attr[:behavior]
@@ -56,7 +66,7 @@ def register_type(definition)
5666
end
5767

5868
Puppet::Type.newtype(definition[:name].to_sym) do
59-
@docs = definition[:docs]
69+
@docs = definition[:desc]
6070

6171
# Keeps a copy of the provider around. Weird naming to avoid clashes with puppet's own `provider` member
6272
define_singleton_method(:my_provider) do

spec/puppet/resource_api/base_context_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def send_log(log, msg)
1313
TestContext.new(definition)
1414
end
1515

16-
let(:definition) { { name: 'some_resource', attributes: { name: 'some_resource' }, features: feature_support } }
16+
let(:definition) { { name: 'some_resource', desc: 'a test resource', attributes: { name: { type: 'String', desc: 'message' } }, features: feature_support } }
1717
let(:feature_support) { [] }
1818

1919
it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a Hash} }

spec/puppet/resource_api/puppet_context_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe Puppet::ResourceApi::PuppetContext do
44
subject(:context) { described_class.new(definition) }
55

6-
let(:definition) { { name: 'some_resource' } }
6+
let(:definition) { { name: 'some_resource', desc: 'a test resource', attributes: {} } }
77

88
describe '#device' do
99
context 'when a NetworkDevice is configured' do

spec/puppet/resource_api_spec.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
context 'when registering a minimal type' do
4848
let(:definition) { { name: 'minimal', attributes: {} } }
4949

50-
it { expect { described_class.register_type(definition) }.not_to raise_error }
50+
it {
51+
expect(Puppet).to receive(:warning).with('`minimal` has no documentation, add it using a `desc` key')
52+
described_class.register_type(definition)
53+
}
5154

5255
describe 'the registered type' do
5356
subject(:type) { Puppet::Type.type(:minimal) }
@@ -62,10 +65,33 @@
6265
end
6366
end
6467

68+
context 'when registering a type with both desc and docs key' do
69+
let(:definition) { { name: 'both', desc: 'the desc', docs: 'the docs', attributes: {} } }
70+
71+
it {
72+
expect { described_class.register_type(definition) }.to raise_error Puppet::DevError, '`both` has both `desc` and `docs`, prefer using `desc`'
73+
}
74+
end
75+
76+
context 'when registering a type with a docs key' do
77+
let(:definition) { { name: 'both', docs: 'the docs', attributes: {} } }
78+
79+
it { expect { described_class.register_type(definition) }.not_to raise_error }
80+
81+
describe 'the registered type' do
82+
subject(:type) { Puppet::Type.type(:both) }
83+
84+
it { is_expected.not_to be_nil }
85+
it { is_expected.to be_respond_to :instances }
86+
it { expect(type.instance_variable_get(:@docs)).to eq 'the docs' }
87+
end
88+
end
89+
6590
context 'when registering a type with multiple attributes' do
6691
let(:definition) do
6792
{
6893
name: type_name,
94+
desc: 'a test resource',
6995
attributes: {
7096
name: {
7197
type: 'String',
@@ -769,6 +795,7 @@ def set(_context, _changes); end
769795
let(:definition) do
770796
{
771797
name: 'init_behaviour',
798+
desc: 'a test resource',
772799
attributes: {
773800
ensure: {
774801
type: 'Enum[present, absent]',
@@ -1253,7 +1280,7 @@ def set(_context, _changes); end
12531280
context 'when loading a provider that doesn\'t create the correct class' do
12541281
let(:definition) { { name: 'no_class', attributes: {} } }
12551282

1256-
it { expect { described_class.load_provider('no_class') }.to raise_error Puppet::DevError, %r{Puppet::Provider::NoClass::NoClass} }
1283+
it { expect { described_class.load_provider('no_class') }.to raise_error Puppet::DevError, %r{provider class Puppet::Provider::NoClass::NoClass not found} }
12571284
end
12581285

12591286
context 'when loading a provider that creates the correct class' do
@@ -1810,6 +1837,7 @@ def set(_context, changes) end
18101837
let(:definition) do
18111838
{
18121839
name: 'test_noop_support',
1840+
desc: 'a test resource',
18131841
features: ['no such feature'],
18141842
attributes: {},
18151843
}
@@ -1826,6 +1854,7 @@ def set(_context, changes) end
18261854
let(:definition) do
18271855
{
18281856
name: 'test_behaviour',
1857+
desc: 'a test resource',
18291858
attributes: {
18301859
id: {
18311860
type: 'String',
@@ -1842,6 +1871,7 @@ def set(_context, changes) end
18421871
let(:definition) do
18431872
{
18441873
name: 'test_behaviour',
1874+
desc: 'a test resource',
18451875
attributes: {
18461876
param: {
18471877
type: 'String',
@@ -1858,6 +1888,7 @@ def set(_context, changes) end
18581888
let(:definition) do
18591889
{
18601890
name: 'test_behaviour',
1891+
desc: 'a test resource',
18611892
attributes: {
18621893
param_ro: {
18631894
type: 'String',
@@ -1874,6 +1905,7 @@ def set(_context, changes) end
18741905
let(:definition) do
18751906
{
18761907
name: 'test_behaviour',
1908+
desc: 'a test resource',
18771909
attributes: {
18781910
param_ro: {
18791911
type: 'String',
@@ -1890,6 +1922,7 @@ def set(_context, changes) end
18901922
let(:definition) do
18911923
{
18921924
name: 'test_behaviour',
1925+
desc: 'a test resource',
18931926
attributes: {
18941927
source: {
18951928
type: 'String',

0 commit comments

Comments
 (0)