puppet-strings 0.4.0 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +122 -0
  3. data/COMMITTERS.md +185 -0
  4. data/CONTRIBUTING.md +89 -0
  5. data/Gemfile +38 -0
  6. data/JSON.md +511 -0
  7. data/LICENSE +13 -0
  8. data/README.md +416 -0
  9. data/Rakefile +49 -0
  10. data/lib/puppet-strings.rb +63 -0
  11. data/lib/puppet-strings/json.rb +49 -0
  12. data/lib/puppet-strings/tasks.rb +10 -0
  13. data/lib/puppet-strings/tasks/generate.rb +23 -0
  14. data/lib/puppet-strings/tasks/gh_pages.rb +43 -0
  15. data/lib/puppet-strings/yard.rb +96 -0
  16. data/lib/puppet-strings/yard/code_objects.rb +8 -0
  17. data/lib/puppet-strings/yard/code_objects/base.rb +14 -0
  18. data/lib/puppet-strings/yard/code_objects/class.rb +59 -0
  19. data/lib/puppet-strings/yard/code_objects/defined_type.rb +58 -0
  20. data/lib/puppet-strings/yard/code_objects/function.rb +93 -0
  21. data/lib/puppet-strings/yard/code_objects/group.rb +30 -0
  22. data/lib/puppet-strings/yard/code_objects/provider.rb +93 -0
  23. data/lib/puppet-strings/yard/code_objects/type.rb +146 -0
  24. data/lib/puppet-strings/yard/handlers.rb +16 -0
  25. data/lib/puppet-strings/yard/handlers/puppet/base.rb +44 -0
  26. data/lib/puppet-strings/yard/handlers/puppet/class_handler.rb +23 -0
  27. data/lib/puppet-strings/yard/handlers/puppet/defined_type_handler.rb +23 -0
  28. data/lib/puppet-strings/yard/handlers/puppet/function_handler.rb +42 -0
  29. data/lib/puppet-strings/yard/handlers/ruby/base.rb +38 -0
  30. data/lib/puppet-strings/yard/handlers/ruby/function_handler.rb +357 -0
  31. data/lib/puppet-strings/yard/handlers/ruby/provider_handler.rb +113 -0
  32. data/lib/puppet-strings/yard/handlers/ruby/type_handler.rb +194 -0
  33. data/lib/puppet-strings/yard/parsers.rb +7 -0
  34. data/lib/puppet-strings/yard/parsers/puppet/parser.rb +70 -0
  35. data/lib/puppet-strings/yard/parsers/puppet/statement.rb +146 -0
  36. data/lib/puppet-strings/yard/tags.rb +6 -0
  37. data/lib/puppet-strings/yard/tags/overload_tag.rb +109 -0
  38. data/lib/puppet-strings/yard/tags/parameter_directive.rb +24 -0
  39. data/lib/puppet-strings/yard/tags/property_directive.rb +24 -0
  40. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_class.erb +9 -0
  41. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_defined_type.erb +9 -0
  42. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_function.erb +10 -0
  43. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_provider.erb +10 -0
  44. data/lib/puppet-strings/yard/templates/default/fulldoc/html/full_list_puppet_type.erb +9 -0
  45. data/lib/puppet-strings/yard/templates/default/fulldoc/html/setup.rb +64 -0
  46. data/lib/puppet-strings/yard/templates/default/layout/html/objects.erb +35 -0
  47. data/lib/puppet-strings/yard/templates/default/layout/html/setup.rb +172 -0
  48. data/lib/puppet-strings/yard/templates/default/puppet_class/html/box_info.erb +26 -0
  49. data/lib/puppet-strings/yard/templates/default/puppet_class/html/header.erb +1 -0
  50. data/lib/puppet-strings/yard/templates/default/puppet_class/html/overview.erb +6 -0
  51. data/lib/puppet-strings/yard/templates/default/puppet_class/html/setup.rb +14 -0
  52. data/lib/puppet-strings/yard/templates/default/puppet_class/html/source.erb +12 -0
  53. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/box_info.erb +10 -0
  54. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/header.erb +1 -0
  55. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/overview.erb +6 -0
  56. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/setup.rb +5 -0
  57. data/lib/puppet-strings/yard/templates/default/puppet_defined_type/html/source.erb +12 -0
  58. data/lib/puppet-strings/yard/templates/default/puppet_function/html/box_info.erb +14 -0
  59. data/lib/puppet-strings/yard/templates/default/puppet_function/html/header.erb +1 -0
  60. data/lib/puppet-strings/yard/templates/default/puppet_function/html/overview.erb +18 -0
  61. data/lib/puppet-strings/yard/templates/default/puppet_function/html/setup.rb +5 -0
  62. data/lib/puppet-strings/yard/templates/default/puppet_function/html/source.erb +12 -0
  63. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/box_info.erb +14 -0
  64. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/collection.erb +10 -0
  65. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/features.erb +12 -0
  66. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/header.erb +1 -0
  67. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/overview.erb +6 -0
  68. data/lib/puppet-strings/yard/templates/default/puppet_provider/html/setup.rb +29 -0
  69. data/lib/puppet-strings/yard/templates/default/puppet_type/html/box_info.erb +20 -0
  70. data/lib/puppet-strings/yard/templates/default/puppet_type/html/features.erb +13 -0
  71. data/lib/puppet-strings/yard/templates/default/puppet_type/html/header.erb +1 -0
  72. data/lib/puppet-strings/yard/templates/default/puppet_type/html/overview.erb +6 -0
  73. data/lib/puppet-strings/yard/templates/default/puppet_type/html/parameters.erb +35 -0
  74. data/lib/puppet-strings/yard/templates/default/puppet_type/html/setup.rb +32 -0
  75. data/lib/puppet-strings/yard/templates/default/tags/html/puppet_overload.erb +12 -0
  76. data/lib/puppet-strings/yard/templates/default/tags/setup.rb +15 -0
  77. data/lib/puppet/application/strings.rb +1 -0
  78. data/lib/puppet/face/strings.rb +80 -39
  79. data/spec/acceptance/emit_json_options.rb +41 -0
  80. data/spec/acceptance/lib/util.rb +15 -0
  81. data/spec/acceptance/running_strings_generate.rb +54 -0
  82. data/spec/fixtures/acceptance/modules/test/functions/add.pp +9 -0
  83. data/spec/fixtures/acceptance/modules/test/lib/puppet/functions/4x_function.rb +5 -0
  84. data/spec/fixtures/acceptance/modules/test/lib/puppet/parser/functions/function3x.rb +2 -0
  85. data/spec/fixtures/acceptance/modules/test/lib/puppet/provider/server/linux.rb +9 -0
  86. data/spec/fixtures/acceptance/modules/test/lib/puppet/type/database.rb +15 -0
  87. data/spec/fixtures/acceptance/modules/test/manifests/init.pp +27 -0
  88. data/spec/fixtures/acceptance/modules/test/manifests/triple_nested_classes.pp +27 -0
  89. data/spec/fixtures/acceptance/modules/test/metadata.json +6 -0
  90. data/spec/fixtures/unit/json/output.json +348 -0
  91. data/spec/fixtures/unit/json/output_without_puppet_function.json +301 -0
  92. data/spec/spec_helper.rb +21 -0
  93. data/spec/spec_helper_acceptance.rb +27 -0
  94. data/spec/unit/puppet-strings/json_spec.rb +136 -0
  95. data/spec/unit/puppet-strings/yard/handlers/puppet/class_handler_spec.rb +155 -0
  96. data/spec/unit/puppet-strings/yard/handlers/puppet/defined_type_handler_spec.rb +155 -0
  97. data/spec/unit/puppet-strings/yard/handlers/puppet/function_handler_spec.rb +169 -0
  98. data/spec/unit/puppet-strings/yard/handlers/ruby/function_handler_spec.rb +613 -0
  99. data/spec/unit/puppet-strings/yard/handlers/ruby/provider_handler_spec.rb +90 -0
  100. data/spec/unit/puppet-strings/yard/handlers/ruby/type_handler_spec.rb +214 -0
  101. data/spec/unit/puppet-strings/yard/parsers/puppet/parser_spec.rb +171 -0
  102. metadata +115 -92
  103. data/lib/puppet-strings/rake_tasks.rb +0 -18
  104. data/lib/puppet_x/puppetlabs/strings.rb +0 -64
  105. data/lib/puppet_x/puppetlabs/strings/actions.rb +0 -92
  106. data/lib/puppet_x/puppetlabs/strings/pops/yard_statement.rb +0 -79
  107. data/lib/puppet_x/puppetlabs/strings/pops/yard_transformer.rb +0 -47
  108. data/lib/puppet_x/puppetlabs/strings/util.rb +0 -65
  109. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/defined_type_object.rb +0 -33
  110. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/host_class_object.rb +0 -22
  111. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/method_object.rb +0 -62
  112. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/provider_object.rb +0 -24
  113. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/puppet_namespace_object.rb +0 -48
  114. data/lib/puppet_x/puppetlabs/strings/yard/code_objects/type_object.rb +0 -42
  115. data/lib/puppet_x/puppetlabs/strings/yard/core_ext/yard.rb +0 -40
  116. data/lib/puppet_x/puppetlabs/strings/yard/handlers/base.rb +0 -13
  117. data/lib/puppet_x/puppetlabs/strings/yard/handlers/defined_type_handler.rb +0 -31
  118. data/lib/puppet_x/puppetlabs/strings/yard/handlers/heredoc_helper.rb +0 -80
  119. data/lib/puppet_x/puppetlabs/strings/yard/handlers/host_class_handler.rb +0 -42
  120. data/lib/puppet_x/puppetlabs/strings/yard/handlers/provider_handler.rb +0 -95
  121. data/lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_3x_function_handler.rb +0 -54
  122. data/lib/puppet_x/puppetlabs/strings/yard/handlers/puppet_4x_function_handler.rb +0 -234
  123. data/lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb +0 -295
  124. data/lib/puppet_x/puppetlabs/strings/yard/json_registry_store.rb +0 -85
  125. data/lib/puppet_x/puppetlabs/strings/yard/monkey_patches.rb +0 -68
  126. data/lib/puppet_x/puppetlabs/strings/yard/parser.rb +0 -30
  127. data/lib/puppet_x/puppetlabs/strings/yard/tags/directives.rb +0 -9
  128. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/docstring.erb +0 -34
  129. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/header.erb +0 -5
  130. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/parameter_details.erb +0 -6
  131. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/html/setup.rb +0 -1
  132. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/definedtype/setup.rb +0 -49
  133. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_class.erb +0 -2
  134. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_manifest.erb +0 -1
  135. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_plugin.erb +0 -21
  136. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_provider.erb +0 -1
  137. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/full_list_puppet_type.erb +0 -1
  138. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/fulldoc/html/setup.rb +0 -82
  139. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/box_info.erb +0 -22
  140. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/setup.rb +0 -1
  141. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/html/subclasses.erb +0 -4
  142. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/hostclass/setup.rb +0 -21
  143. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/html_helper.rb +0 -139
  144. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/layout/html/setup.rb +0 -18
  145. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/html/header.erb +0 -17
  146. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/setup.rb +0 -21
  147. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/method_details/text/header.erb +0 -2
  148. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/command_details.erb +0 -8
  149. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/confine_details.erb +0 -10
  150. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/default_details.erb +0 -10
  151. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/docstring.erb +0 -34
  152. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/feature_details.erb +0 -10
  153. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/header.erb +0 -5
  154. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/html/setup.rb +0 -1
  155. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/provider/setup.rb +0 -50
  156. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/box_info.erb +0 -11
  157. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/header.erb +0 -5
  158. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/method_details_list.erb +0 -53
  159. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/method_summary.erb +0 -20
  160. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/html/setup.rb +0 -1
  161. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/puppetnamespace/setup.rb +0 -91
  162. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/template_helper.rb +0 -192
  163. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/docstring.erb +0 -34
  164. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/header.erb +0 -5
  165. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/parameter_details.erb +0 -12
  166. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/provider_details.erb +0 -10
  167. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/html/setup.rb +0 -1
  168. data/lib/puppet_x/puppetlabs/strings/yard/templates/default/type/setup.rb +0 -55
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'puppet-strings/yard'
3
+
4
+ describe PuppetStrings::Yard::Handlers::Ruby::ProviderHandler do
5
+ subject {
6
+ YARD::Parser::SourceParser.parse_string(source, :ruby)
7
+ YARD::Registry.all(:puppet_provider)
8
+ }
9
+
10
+ describe 'parsing source without a provider definition' do
11
+ let(:source) { 'puts "hi"' }
12
+
13
+ it 'no providers should be in the registry' do
14
+ expect(subject.empty?).to eq(true)
15
+ end
16
+ end
17
+
18
+ describe 'parsing a provider with a missing description' do
19
+ let(:source) { <<-SOURCE
20
+ Puppet::Type.type(:custom).provide :linux do
21
+ end
22
+ SOURCE
23
+ }
24
+
25
+ it 'should log a warning' do
26
+ expect{ subject }.to output(/\[warn\]: Missing a description for Puppet provider 'linux' \(resource type 'custom'\) at \(stdin\):1\./).to_stdout_from_any_process
27
+ end
28
+ end
29
+
30
+ describe 'parsing a provider with an invalid docstring assignment' do
31
+ let(:source) { <<-SOURCE
32
+ Puppet::Type.type(:custom).provide :linux do
33
+ @doc = 123
34
+ end
35
+ SOURCE
36
+ }
37
+
38
+ it 'should log an error' do
39
+ expect { subject }.to output(/Failed to parse docstring/).to_stdout_from_any_process
40
+ end
41
+ end
42
+
43
+ describe 'parsing a provider with a valid docstring assignment' do
44
+ let(:source) { <<-SOURCE
45
+ Puppet::Type.type(:custom).provide :linux do
46
+ @doc = 'An example provider on Linux.'
47
+ end
48
+ SOURCE
49
+ }
50
+
51
+ it 'should correctly detect the docstring' do
52
+ expect(subject.size).to eq(1)
53
+ object = subject.first
54
+ expect(object.docstring).to eq('An example provider on Linux.')
55
+ end
56
+ end
57
+
58
+ describe 'parsing a provider definition' do
59
+ let(:source) { <<-SOURCE
60
+ Puppet::Type.type(:custom).provide :linux do
61
+ desc 'An example provider on Linux.'
62
+ confine kernel: 'Linux'
63
+ confine osfamily: 'RedHat'
64
+ defaultfor kernel: 'Linux'
65
+ has_feature :implements_some_feature
66
+ has_feature :some_other_feature
67
+ commands foo: /usr/bin/foo
68
+ end
69
+ SOURCE
70
+ }
71
+
72
+ it 'should register a provider object' do
73
+ expect(subject.size).to eq(1)
74
+ object = subject.first
75
+ expect(object).to be_a(PuppetStrings::Yard::CodeObjects::Provider)
76
+ expect(object.namespace).to eq(PuppetStrings::Yard::CodeObjects::Providers.instance('custom'))
77
+ expect(object.name).to eq(:linux)
78
+ expect(object.type_name).to eq('custom')
79
+ expect(object.docstring).to eq('An example provider on Linux.')
80
+ expect(object.docstring.tags.size).to eq(1)
81
+ tags = object.docstring.tags(:api)
82
+ expect(tags.size).to eq(1)
83
+ expect(tags[0].text).to eq('public')
84
+ expect(object.confines).to eq({ 'kernel' => 'Linux', 'osfamily' => 'RedHat'})
85
+ expect(object.defaults).to eq({ 'kernel' => 'Linux'})
86
+ expect(object.features).to eq(['implements_some_feature', 'some_other_feature'])
87
+ expect(object.commands).to eq({'foo' => '/usr/bin/foo'})
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,214 @@
1
+ require 'spec_helper'
2
+ require 'puppet-strings/yard'
3
+
4
+ describe PuppetStrings::Yard::Handlers::Ruby::TypeHandler do
5
+ subject {
6
+ YARD::Parser::SourceParser.parse_string(source, :ruby)
7
+ YARD::Registry.all(:puppet_type)
8
+ }
9
+
10
+ describe 'parsing source without a type definition' do
11
+ let(:source) { 'puts "hi"' }
12
+
13
+ it 'no types should be in the registry' do
14
+ expect(subject.empty?).to eq(true)
15
+ end
16
+ end
17
+
18
+ describe 'parsing a type with a missing description' do
19
+ let(:source) { <<-SOURCE
20
+ Puppet::Type.newtype(:database) do
21
+ end
22
+ SOURCE
23
+ }
24
+
25
+ it 'should log a warning' do
26
+ expect{ subject }.to output(/\[warn\]: Missing a description for Puppet resource type 'database' at \(stdin\):1\./).to_stdout_from_any_process
27
+ end
28
+ end
29
+
30
+ describe 'parsing a type with an invalid docstring assignment' do
31
+ let(:source) { <<-SOURCE
32
+ Puppet::Type.newtype(:database) do
33
+ @doc = 123
34
+ end
35
+ SOURCE
36
+ }
37
+
38
+ it 'should log an error' do
39
+ expect { subject }.to output(/Failed to parse docstring/).to_stdout_from_any_process
40
+ end
41
+ end
42
+
43
+ describe 'parsing a type with a valid docstring assignment' do
44
+ let(:source) { <<-SOURCE
45
+ Puppet::Type.newtype(:database) do
46
+ @doc = 'An example database server resource type.'
47
+ end
48
+ SOURCE
49
+ }
50
+
51
+ it 'should correctly detect the docstring' do
52
+ expect(subject.size).to eq(1)
53
+ object = subject.first
54
+ expect(object.docstring).to eq('An example database server resource type.')
55
+ end
56
+ end
57
+
58
+ describe 'parsing a type definition' do
59
+ let(:source) { <<-SOURCE
60
+ # @!puppet.type.param [value1, value2] dynamic_param Documentation for a dynamic parameter.
61
+ # @!puppet.type.property [foo, bar] dynamic_prop Documentation for a dynamic property.
62
+ Puppet::Type.newtype(:database) do
63
+ desc 'An example database server resource type.'
64
+ feature :encryption, 'The provider supports encryption.', methods: [:encrypt]
65
+ ensurable do
66
+ desc 'What state the database should be in.'
67
+ defaultvalues
68
+ aliasvalue(:up, :present)
69
+ aliasvalue(:down, :absent)
70
+ defaultto :up
71
+ end
72
+
73
+ newparam(:address) do
74
+ isnamevar
75
+ desc 'The database server name.'
76
+ end
77
+
78
+ newparam(:encryption_key, required_features: :encryption) do
79
+ desc 'The encryption key to use.'
80
+ end
81
+
82
+ newparam(:encrypt, :parent => Puppet::Parameter::Boolean) do
83
+ desc 'Whether or not to encrypt the database.'
84
+ defaultto false
85
+ end
86
+
87
+ newparam(:backup) do
88
+ desc 'How often to backup the database.'
89
+ defaultto :never
90
+ newvalues(:daily, :monthly, :never)
91
+ end
92
+
93
+ newproperty(:file) do
94
+ desc 'The database file to use.'
95
+ end
96
+
97
+ newproperty(:log_level) do
98
+ desc 'The log level to use.'
99
+ newvalue(:debug)
100
+ newvalue(:warn)
101
+ newvalue(:error)
102
+ defaultto 'warn'
103
+ end
104
+ end
105
+ SOURCE
106
+ }
107
+
108
+ it 'should register a type object' do
109
+ expect(subject.size).to eq(1)
110
+ object = subject.first
111
+ expect(object).to be_a(PuppetStrings::Yard::CodeObjects::Type)
112
+ expect(object.namespace).to eq(PuppetStrings::Yard::CodeObjects::Types.instance)
113
+ expect(object.name).to eq(:database)
114
+ expect(object.docstring).to eq('An example database server resource type.')
115
+ expect(object.docstring.tags.size).to eq(1)
116
+ tags = object.docstring.tags(:api)
117
+ expect(tags.size).to eq(1)
118
+ expect(tags[0].text).to eq('public')
119
+ expect(object.properties.size).to eq(4)
120
+ expect(object.properties[0].name).to eq('dynamic_prop')
121
+ expect(object.properties[0].docstring).to eq('Documentation for a dynamic property.')
122
+ expect(object.properties[0].isnamevar).to eq(false)
123
+ expect(object.properties[0].values).to eq(%w(foo bar))
124
+ expect(object.properties[1].name).to eq('ensure')
125
+ expect(object.properties[1].docstring).to eq('What state the database should be in.')
126
+ expect(object.properties[1].isnamevar).to eq(false)
127
+ expect(object.properties[1].default).to eq('up')
128
+ expect(object.properties[1].values).to eq(%w(present absent up down))
129
+ expect(object.properties[1].aliases).to eq({ 'down' => 'absent', 'up' => 'present' })
130
+ expect(object.properties[2].name).to eq('file')
131
+ expect(object.properties[2].docstring).to eq('The database file to use.')
132
+ expect(object.properties[2].isnamevar).to eq(false)
133
+ expect(object.properties[2].default).to be_nil
134
+ expect(object.properties[2].values).to eq([])
135
+ expect(object.properties[2].aliases).to eq({})
136
+ expect(object.properties[3].name).to eq('log_level')
137
+ expect(object.properties[3].docstring).to eq('The log level to use.')
138
+ expect(object.properties[3].isnamevar).to eq(false)
139
+ expect(object.properties[3].default).to eq('warn')
140
+ expect(object.properties[3].values).to eq(%w(debug warn error))
141
+ expect(object.properties[3].aliases).to eq({})
142
+ expect(object.parameters.size).to eq(5)
143
+ expect(object.parameters[0].name).to eq('dynamic_param')
144
+ expect(object.parameters[0].docstring).to eq('Documentation for a dynamic parameter.')
145
+ expect(object.parameters[0].isnamevar).to eq(false)
146
+ expect(object.parameters[0].values).to eq(%w(value1 value2))
147
+ expect(object.parameters[1].name).to eq('address')
148
+ expect(object.parameters[1].docstring).to eq('The database server name.')
149
+ expect(object.parameters[1].isnamevar).to eq(true)
150
+ expect(object.parameters[1].default).to be_nil
151
+ expect(object.parameters[1].values).to eq([])
152
+ expect(object.parameters[1].aliases).to eq({})
153
+ expect(object.parameters[2].name).to eq('encryption_key')
154
+ expect(object.parameters[2].docstring).to eq('The encryption key to use.')
155
+ expect(object.parameters[2].isnamevar).to eq(false)
156
+ expect(object.parameters[2].default).to be_nil
157
+ expect(object.parameters[2].values).to eq([])
158
+ expect(object.parameters[2].aliases).to eq({})
159
+ expect(object.parameters[3].name).to eq('encrypt')
160
+ expect(object.parameters[3].docstring).to eq('Whether or not to encrypt the database.')
161
+ expect(object.parameters[3].isnamevar).to eq(false)
162
+ expect(object.parameters[3].default).to eq('false')
163
+ expect(object.parameters[3].values).to eq(%w(true false yes no))
164
+ expect(object.parameters[3].aliases).to eq({})
165
+ expect(object.parameters[4].name).to eq('backup')
166
+ expect(object.parameters[4].docstring).to eq('How often to backup the database.')
167
+ expect(object.parameters[4].isnamevar).to eq(false)
168
+ expect(object.parameters[4].default).to eq('never')
169
+ expect(object.parameters[4].values).to eq(%w(daily monthly never))
170
+ expect(object.features.size).to eq(1)
171
+ expect(object.features[0].name).to eq('encryption')
172
+ expect(object.features[0].docstring).to eq('The provider supports encryption.')
173
+ end
174
+ end
175
+
176
+ describe 'parsing an ensurable type with default ensure values' do
177
+ let(:source) { <<-SOURCE
178
+ Puppet::Type.newtype(:database) do
179
+ desc 'An example database server resource type.'
180
+ ensurable
181
+ end
182
+ SOURCE
183
+ }
184
+
185
+ it 'should register a type object with default ensure values' do
186
+ expect(subject.size).to eq(1)
187
+ object = subject.first
188
+ expect(object.properties[0].name).to eq('ensure')
189
+ expect(object.properties[0].docstring).to eq('The basic property that the resource should be in.')
190
+ expect(object.properties[0].default).to eq('present')
191
+ expect(object.properties[0].values).to eq(%w(present absent))
192
+ end
193
+ end
194
+
195
+ describe 'parsing a type with a parameter with the name of "name"' do
196
+ let(:source) { <<-SOURCE
197
+ Puppet::Type.newtype(:database) do
198
+ desc 'An example database server resource type.'
199
+ newparam(:name) do
200
+ desc 'The database server name.'
201
+ end
202
+ end
203
+ SOURCE
204
+ }
205
+
206
+ it 'should register a type object with the "name" parameter as the namevar' do
207
+ expect(subject.size).to eq(1)
208
+ object = subject.first
209
+ expect(object.parameters.size).to eq(1)
210
+ expect(object.parameters[0].name).to eq('name')
211
+ expect(object.parameters[0].isnamevar).to eq(true)
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+ require 'puppet-strings/yard'
3
+
4
+ describe PuppetStrings::Yard::Parsers::Puppet::Parser do
5
+ subject { PuppetStrings::Yard::Parsers::Puppet::Parser.new(source, file) }
6
+ let(:file) { 'test.pp' }
7
+
8
+ describe 'initialization of the parser' do
9
+ let(:source) { 'notice hi' }
10
+
11
+ it 'should store the original source' do
12
+ expect(subject.source).to eq(source)
13
+ end
14
+
15
+ it 'should store the original file name' do
16
+ expect(subject.file).to eq(file)
17
+ end
18
+
19
+ it 'should have no relevant statements' do
20
+ subject.parse
21
+ expect(subject.enumerator.empty?).to be_truthy
22
+ end
23
+ end
24
+
25
+ describe 'parsing invalid Puppet source code' do
26
+ let(:source) { <<SOURCE
27
+ class foo {
28
+ SOURCE
29
+ }
30
+
31
+ it 'should raise an exception' do
32
+ expect{ subject.parse }.to output(/\[error\]: Failed to parse test.pp: Syntax error at end of file/).to_stdout_from_any_process
33
+ end
34
+ end
35
+
36
+ describe 'parsing class definitions' do
37
+ let(:source) { <<SOURCE
38
+ notice hello
39
+ # A simple foo class.
40
+ # @param param1 First param.
41
+ # @param param2 Second param.
42
+ # @param param3 Third param.
43
+ class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
44
+ file { '/tmp/foo':
45
+ ensure => present
46
+ }
47
+ }
48
+ SOURCE
49
+ }
50
+
51
+ it 'should only return the class statement' do
52
+ subject.parse
53
+ expect(subject.enumerator.size).to eq(1)
54
+ statement = subject.enumerator.first
55
+ expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::ClassStatement)
56
+ expect(statement.source).to eq("class foo(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {\n file { '/tmp/foo':\n ensure => present\n }\n}")
57
+ expect(statement.file).to eq(file)
58
+ expect(statement.line).to eq(6)
59
+ expect(statement.docstring).to eq('A simple foo class.')
60
+ expect(statement.name).to eq('foo')
61
+ expect(statement.parent_class).to eq('foo::bar')
62
+ expect(statement.parameters.size).to eq(3)
63
+ expect(statement.parameters[0].name).to eq('param1')
64
+ expect(statement.parameters[0].type).to eq('Integer')
65
+ expect(statement.parameters[0].value).to be_nil
66
+ expect(statement.parameters[1].name).to eq('param2')
67
+ expect(statement.parameters[1].type).to be_nil
68
+ expect(statement.parameters[1].value).to be_nil
69
+ expect(statement.parameters[2].name).to eq('param3')
70
+ expect(statement.parameters[2].type).to eq('String')
71
+ expect(statement.parameters[2].value).to eq('hi')
72
+ end
73
+ end
74
+
75
+ describe 'parsing nested class definitions' do
76
+ let(:source) { <<SOURCE
77
+ class foo {
78
+ class bar {
79
+ }
80
+ }
81
+ SOURCE
82
+ }
83
+
84
+ it 'should parse both class statements' do
85
+ subject.parse
86
+ expect(subject.enumerator.size).to eq(2)
87
+ statement = subject.enumerator[0]
88
+ expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::ClassStatement)
89
+ expect(statement.name).to eq('foo::bar')
90
+ expect(statement.parameters.size).to eq(0)
91
+ statement = subject.enumerator[1]
92
+ expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::ClassStatement)
93
+ expect(statement.name).to eq('foo')
94
+ expect(statement.parameters.size).to eq(0)
95
+ end
96
+ end
97
+
98
+ describe 'parsing defined types' do
99
+ let(:source) { <<SOURCE
100
+ notice hello
101
+ # A simple foo defined type.
102
+ # @param param1 First param.
103
+ # @param param2 Second param.
104
+ # @param param3 Third param.
105
+ define foo(Integer $param1, $param2, String $param3 = hi) {
106
+ file { '/tmp/foo':
107
+ ensure => present
108
+ }
109
+ }
110
+ SOURCE
111
+ }
112
+
113
+ it 'should parse the defined type statement' do
114
+ subject.parse
115
+ expect(subject.enumerator.size).to eq(1)
116
+ statement = subject.enumerator.first
117
+ expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::DefinedTypeStatement)
118
+ expect(statement.name).to eq('foo')
119
+ expect(statement.source).to eq("define foo(Integer $param1, $param2, String $param3 = hi) {\n file { '/tmp/foo':\n ensure => present\n }\n}")
120
+ expect(statement.file).to eq(file)
121
+ expect(statement.line).to eq(6)
122
+ expect(statement.docstring).to eq('A simple foo defined type.')
123
+ expect(statement.parameters.size).to eq(3)
124
+ expect(statement.parameters[0].name).to eq('param1')
125
+ expect(statement.parameters[0].type).to eq('Integer')
126
+ expect(statement.parameters[0].value).to be_nil
127
+ expect(statement.parameters[1].name).to eq('param2')
128
+ expect(statement.parameters[1].type).to be_nil
129
+ expect(statement.parameters[1].value).to be_nil
130
+ expect(statement.parameters[2].name).to eq('param3')
131
+ expect(statement.parameters[2].type).to eq('String')
132
+ expect(statement.parameters[2].value).to eq('hi')
133
+ end
134
+ end
135
+
136
+ describe 'parsing puppet functions', if: TEST_PUPPET_FUNCTIONS do
137
+ let(:source) { <<SOURCE
138
+ notice hello
139
+ # A simple foo function.
140
+ # @param param1 First param.
141
+ # @param param2 Second param.
142
+ # @param param3 Third param.
143
+ function foo(Integer $param1, $param2, String $param3 = hi) {
144
+ notice world
145
+ }
146
+ SOURCE
147
+ }
148
+
149
+ it 'should parse the puppet function statement' do
150
+ subject.parse
151
+ expect(subject.enumerator.size).to eq(1)
152
+ statement = subject.enumerator.first
153
+ expect(statement).to be_a(PuppetStrings::Yard::Parsers::Puppet::FunctionStatement)
154
+ expect(statement.name).to eq('foo')
155
+ expect(statement.source).to eq("function foo(Integer $param1, $param2, String $param3 = hi) {\n notice world\n}")
156
+ expect(statement.file).to eq(file)
157
+ expect(statement.line).to eq(6)
158
+ expect(statement.docstring).to eq('A simple foo function.')
159
+ expect(statement.parameters.size).to eq(3)
160
+ expect(statement.parameters[0].name).to eq('param1')
161
+ expect(statement.parameters[0].type).to eq('Integer')
162
+ expect(statement.parameters[0].value).to be_nil
163
+ expect(statement.parameters[1].name).to eq('param2')
164
+ expect(statement.parameters[1].type).to be_nil
165
+ expect(statement.parameters[1].value).to be_nil
166
+ expect(statement.parameters[2].name).to eq('param3')
167
+ expect(statement.parameters[2].type).to eq('String')
168
+ expect(statement.parameters[2].value).to eq('hi')
169
+ end
170
+ end
171
+ end