Merge pull request #132 from whopper/PDOC-155/master/pup4_type_doc

(PDOC-155) Allow type documentation in Puppet 4 code
This commit is contained in:
Hailee Kenney 2017-01-10 14:15:18 -08:00 committed by GitHub
commit ba4619e345
6 changed files with 80 additions and 12 deletions

View File

@ -210,15 +210,15 @@ The Strings elements appearing in the above comment block are:
* The `include` statement, which adds the usage example code. * The `include` statement, which adds the usage example code.
* Two `@param` tags, with the name of the parameter first, followed by a string describing the parameter's purpose. * Two `@param` tags, with the name of the parameter first, followed by a string describing the parameter's purpose.
Puppet 4 is a typed language, so Puppet Strings automatically documents the parameter types from code. With Puppet 3, however, include the parameter type with the `@param` tag: Puppet 4 is a typed language, so Puppet Strings automatically documents the parameter types from code. You may optionally include a parameter type in the `@param` tag. Strings will emit a warning and ignore the documented type should it differ from the actual type.
With Puppet 3 code, you must always include the parameter type with the `@param` tag:
``` ```
# @param first [String] The first parameter for this class. # @param first [String] The first parameter for this class.
# @param second [Integer] The second parameter for this class. # @param second [Integer] The second parameter for this class.
``` ```
Note that if you document a parameter's type, and that parameter already has a Puppet type specifier, Strings emits a warning.
Defined types are documented in exactly the same way as classes: Defined types are documented in exactly the same way as classes:
``` ```

View File

@ -31,8 +31,8 @@ class PuppetStrings::Yard::Handlers::Puppet::Base < YARD::Handlers::Base
next next
end end
# Warn if the parameter is typed and the tag also has a type # Warn if the parameter type and tag types don't match
log.warn "The @param tag for parameter '#{parameter.name}' should not contain a type specification near #{statement.file}:#{statement.line}: ignoring in favor of parameter type information." if parameter.type && tag.types && !tag.types.empty? log.warn "The type of the @param tag for parameter '#{parameter.name}' does not match the parameter type specification near #{statement.file}:#{statement.line}: ignoring in favor of parameter type information." if parameter.type && tag.types && !tag.types.empty? && parameter.type != tag.types[0]
if parameter.type if parameter.type
tag.types = [parameter.type] tag.types = [parameter.type]

View File

@ -83,7 +83,7 @@ Puppet::Type.type(:database).provide :linux do
defaultfor :osfamily => 'RedHat', :operatingsystemmajrelease => '7' defaultfor :osfamily => 'RedHat', :operatingsystemmajrelease => '7'
has_feature :implements_some_feature has_feature :implements_some_feature
has_feature :some_other_feature has_feature :some_other_feature
commands foo: /usr/bin/foo commands foo: '/usr/bin/foo'
end end
Puppet::Type.newtype(:database) do Puppet::Type.newtype(:database) do

View File

@ -111,7 +111,30 @@ SOURCE
end end
end end
describe 'parsing a class with a typed parameter that also has a @param tag type' do describe 'parsing a class with a typed parameter that also has a @param tag type which matches' do
let(:source) { <<-SOURCE
# A simple foo class.
# @param [Integer] param1 First param.
# @param param2 Second param.
# @param param3 Third param.
class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
file { '/tmp/foo':
ensure => present
}
}
SOURCE
}
it 'should respect the type that was documented' do
expect{ subject }.to output('').to_stdout_from_any_process
expect(subject.size).to eq(1)
tags = subject.first.tags(:param)
expect(tags.size).to eq(3)
expect(tags[0].types).to eq(['Integer'])
end
end
describe 'parsing a class with a typed parameter that also has a @param tag type which does not match' do
let(:source) { <<-SOURCE let(:source) { <<-SOURCE
# A simple foo class. # A simple foo class.
# @param [Boolean] param1 First param. # @param [Boolean] param1 First param.
@ -126,7 +149,7 @@ SOURCE
} }
it 'should output a warning' do it 'should output a warning' do
expect{ subject }.to output(/\[warn\]: The @param tag for parameter 'param1' should not contain a type specification near \(stdin\):5: ignoring in favor of parameter type information\./).to_stdout_from_any_process expect{ subject }.to output(/\[warn\]: The type of the @param tag for parameter 'param1' does not match the parameter type specification near \(stdin\):5: ignoring in favor of parameter type information./).to_stdout_from_any_process
end end
end end

View File

@ -111,7 +111,30 @@ SOURCE
end end
end end
describe 'parsing a defined type with a typed parameter that also has a @param tag type' do describe 'parsing a defined type with a typed parameter that also has a @param tag type which matches' do
let(:source) { <<-SOURCE
# A simple foo defined type.
# @param [Integer] param1 First param.
# @param param2 Second param.
# @param param3 Third param.
define foo(Integer $param1, $param2, String $param3 = hi) {
file { '/tmp/foo':
ensure => present
}
}
SOURCE
}
it 'should respect the type that was documented' do
expect{ subject }.to output('').to_stdout_from_any_process
expect(subject.size).to eq(1)
tags = subject.first.tags(:param)
expect(tags.size).to eq(3)
expect(tags[0].types).to eq(['Integer'])
end
end
describe 'parsing a defined type with a typed parameter that also has a @param tag type which does not match' do
let(:source) { <<-SOURCE let(:source) { <<-SOURCE
# A simple foo defined type. # A simple foo defined type.
# @param [Boolean] param1 First param. # @param [Boolean] param1 First param.
@ -126,7 +149,7 @@ SOURCE
} }
it 'should output a warning' do it 'should output a warning' do
expect{ subject }.to output(/\[warn\]: The @param tag for parameter 'param1' should not contain a type specification near \(stdin\):5: ignoring in favor of parameter type information\./).to_stdout_from_any_process expect{ subject }.to output(/\[warn\]: The type of the @param tag for parameter 'param1' does not match the parameter type specification near \(stdin\):5: ignoring in favor of parameter type information./).to_stdout_from_any_process
end end
end end

View File

@ -115,7 +115,29 @@ SOURCE
end end
end end
describe 'parsing a function with a typed parameter that also has a @param tag type' do describe 'parsing a function with a typed parameter that also has a @param tag type which matches' do
let(:source) { <<-SOURCE
# A simple foo function.
# @param [Integer] param1 First param.
# @param param2 Second param.
# @param param3 Third param.
# @return [Undef] Returns nothing.
function foo(Integer $param1, $param2, String $param3 = hi) {
notice 'hello world'
}
SOURCE
}
it 'should respect the type that was documented' do
expect{ subject }.to output('').to_stdout_from_any_process
expect(subject.size).to eq(1)
tags = subject.first.tags(:param)
expect(tags.size).to eq(3)
expect(tags[0].types).to eq(['Integer'])
end
end
describe 'parsing a function with a typed parameter that also has a @param tag type which does not match' do
let(:source) { <<-SOURCE let(:source) { <<-SOURCE
# A simple foo function. # A simple foo function.
# @param [Boolean] param1 First param. # @param [Boolean] param1 First param.
@ -129,7 +151,7 @@ SOURCE
} }
it 'should output a warning' do it 'should output a warning' do
expect{ subject }.to output(/\[warn\]: The @param tag for parameter 'param1' should not contain a type specification near \(stdin\):6: ignoring in favor of parameter type information\./).to_stdout_from_any_process expect{ subject }.to output(/\[warn\]: The type of the @param tag for parameter 'param1' does not match the parameter type specification near \(stdin\):6: ignoring in favor of parameter type information./).to_stdout_from_any_process
end end
end end