foodcritic 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/CHANGELOG.md +691 -0
  2. data/LICENSE +21 -0
  3. data/README.md +28 -0
  4. data/chef_dsl_metadata/chef_0.10.10.json +22 -0
  5. data/chef_dsl_metadata/chef_10.12.0.json +22 -0
  6. data/chef_dsl_metadata/chef_10.14.0.json +22 -0
  7. data/chef_dsl_metadata/chef_10.14.2.json +22 -0
  8. data/chef_dsl_metadata/chef_10.14.4.json +22 -0
  9. data/chef_dsl_metadata/chef_10.16.0.json +22 -0
  10. data/chef_dsl_metadata/chef_10.16.2.json +22 -0
  11. data/chef_dsl_metadata/chef_10.16.4.json +22 -0
  12. data/chef_dsl_metadata/chef_10.16.6.json +22 -0
  13. data/chef_dsl_metadata/chef_10.18.0.json +22 -0
  14. data/chef_dsl_metadata/chef_10.18.2.json +22 -0
  15. data/chef_dsl_metadata/chef_10.20.0.json +22 -0
  16. data/chef_dsl_metadata/chef_10.22.0.json +22 -0
  17. data/chef_dsl_metadata/chef_10.24.0.json +22 -0
  18. data/chef_dsl_metadata/chef_10.24.4.json +8460 -0
  19. data/chef_dsl_metadata/chef_10.26.0.json +8460 -0
  20. data/chef_dsl_metadata/chef_11.0.0.json +22 -0
  21. data/chef_dsl_metadata/chef_11.2.0.json +22 -0
  22. data/chef_dsl_metadata/chef_11.4.0.json +22 -0
  23. data/chef_dsl_metadata/chef_11.4.2.json +8794 -0
  24. data/chef_dsl_metadata/chef_11.4.4.json +8794 -0
  25. data/features/002_check_string_interpolation.feature +45 -0
  26. data/features/003_check_for_chef_server.feature +56 -0
  27. data/features/004_check_service_resource_used.feature +53 -0
  28. data/features/005_check_for_resource_repetition.feature +64 -0
  29. data/features/006_check_file_mode.feature +35 -0
  30. data/features/007_check_for_undeclared_recipe_dependencies.feature +71 -0
  31. data/features/008_check_for_boilerplate_metadata.feature +25 -0
  32. data/features/009_check_for_unrecognised_resource_attributes.feature +77 -0
  33. data/features/010_check_search_syntax.feature +20 -0
  34. data/features/011_check_for_markdown_readme.feature +20 -0
  35. data/features/012_check_for_deprecated_readme_format.feature +20 -0
  36. data/features/013_check_for_hardcoded_tmpdir.feature +25 -0
  37. data/features/014_check_for_long_ruby_blocks.feature +30 -0
  38. data/features/015_check_for_definitions.feature +21 -0
  39. data/features/016_check_for_no_lwrp_default_action.feature +20 -0
  40. data/features/017_check_for_no_lwrp_notifications.feature +25 -0
  41. data/features/018_check_for_old_lwrp_notification_syntax.feature +25 -0
  42. data/features/019_check_for_consistent_node_access.feature +107 -0
  43. data/features/021_check_for_dodgy_lwrp_conditions.feature +28 -0
  44. data/features/022_check_for_dodgy_conditions_within_loop.feature +28 -0
  45. data/features/023_check_for_condition_around_resource.feature +52 -0
  46. data/features/024_check_for_missing_platforms.feature +43 -0
  47. data/features/025_check_for_deprecated_gem_install.feature +30 -0
  48. data/features/026_check_for_conditional_block_string.feature +20 -0
  49. data/features/027_check_for_internal_attribute_use.feature +22 -0
  50. data/features/028_check_for_incorrect_platform_method.feature +20 -0
  51. data/features/029_check_for_no_leading_cookbook_name.feature +18 -0
  52. data/features/030_check_for_debugger_breakpoints.feature +25 -0
  53. data/features/031_check_for_metadata_existence.feature +15 -0
  54. data/features/032_check_for_invalid_notification_timing.feature +22 -0
  55. data/features/033_check_for_missing_template.feature +75 -0
  56. data/features/034_check_for_unused_template_variables.feature +37 -0
  57. data/features/037_check_for_invalid_notification_action.feature +34 -0
  58. data/features/038_check_for_invalid_action.feature +51 -0
  59. data/features/039_check_for_key_access_to_node_methods.feature +33 -0
  60. data/features/040_check_raw_git_usage.feature +37 -0
  61. data/features/041_check_raw_download.feature +26 -0
  62. data/features/042_check_for_deprecated_require_recipe.feature +15 -0
  63. data/features/043_check_for_old_notification_style.feature +35 -0
  64. data/features/044_check_for_bare_attribute_keys.feature +43 -0
  65. data/features/045_check_for_cookbook_name_in_metadata.feature +20 -0
  66. data/features/046_check_for_assign_unless_nil_attributes.feature +21 -0
  67. data/features/build_framework_support.feature +99 -0
  68. data/features/checking_all_types_of_file.feature +40 -0
  69. data/features/choose_rules_to_apply.feature +49 -0
  70. data/features/command_line_help.feature +43 -0
  71. data/features/continuous_integration_support.feature +35 -0
  72. data/features/ignore_via_line_comments.feature +51 -0
  73. data/features/include_custom_rules.feature +29 -0
  74. data/features/individual_file.feature +12 -0
  75. data/features/limit_rules_to_specific_versions.feature +65 -0
  76. data/features/multiple_paths.feature +11 -0
  77. data/features/show_lines_matched.feature +20 -0
  78. data/features/sort_warnings.feature +10 -0
  79. data/features/specify_search_grammar.feature +25 -0
  80. data/features/step_definitions/cookbook_steps.rb +1791 -0
  81. data/features/support/command_helpers.rb +312 -0
  82. data/features/support/cookbook_helpers.rb +495 -0
  83. data/features/support/env.rb +11 -0
  84. data/lib/foodcritic.rb +0 -1
  85. data/lib/foodcritic/api.rb +3 -2
  86. data/lib/foodcritic/command_line.rb +4 -0
  87. data/lib/foodcritic/linter.rb +29 -6
  88. data/lib/foodcritic/output.rb +74 -26
  89. data/lib/foodcritic/rules.rb +6 -5
  90. data/lib/foodcritic/version.rb +1 -1
  91. data/man/foodcritic.1 +58 -0
  92. data/man/foodcritic.1.ronn +57 -0
  93. data/spec/foodcritic/api_spec.rb +1615 -0
  94. data/spec/foodcritic/chef_spec.rb +66 -0
  95. data/spec/foodcritic/command_line_spec.rb +51 -0
  96. data/spec/foodcritic/domain_spec.rb +24 -0
  97. data/spec/foodcritic/linter_spec.rb +91 -0
  98. data/spec/foodcritic/template_spec.rb +49 -0
  99. data/spec/regression/cookbooks.txt +135 -0
  100. data/spec/regression/expected-output.txt +443 -0
  101. data/spec/regression/regression_spec.rb +17 -0
  102. data/spec/regression_helpers.rb +37 -0
  103. data/spec/spec_helper.rb +10 -0
  104. metadata +87 -24
@@ -0,0 +1,29 @@
1
+ Feature: Include custom rules
2
+
3
+ In order to be able detect cookbook issues that are specific to my organisation
4
+ As a developer
5
+ I want to be able to write custom rules and include them in my check
6
+
7
+ Scenario: Valid custom rule
8
+ Given a cookbook with a single recipe that reads node attributes via strings
9
+ And I have installed the lint tool
10
+ When I run it on the command line including a custom rule file containing a rule that matches
11
+ Then a warning for the custom rule should be displayed
12
+
13
+ Scenario: Valid custom rules (directory)
14
+ Given a cookbook with a single recipe that reads node attributes via strings
15
+ And I have installed the lint tool
16
+ When I run it on the command line including a custom rule directory containing a rule that matches
17
+ Then a warning for the custom rule should be displayed
18
+
19
+ Scenario: Missing file
20
+ Given a cookbook with a single recipe that reads node attributes via strings
21
+ And I have installed the lint tool
22
+ When I run it on the command line including a missing custom rule file
23
+ Then a 'No such file or directory' error should be displayed
24
+
25
+ Scenario: Non-ruby file
26
+ Given a cookbook with a single recipe that reads node attributes via strings
27
+ And I have installed the lint tool
28
+ When I run it on the command line including a file which does not contain Ruby code
29
+ Then an 'undefined method' error should be displayed
@@ -0,0 +1,12 @@
1
+ Feature: Individual file
2
+
3
+ In order to get even faster feedback on changes to cookbooks
4
+ As a developer
5
+ I want to lint individual files in a cookbook
6
+
7
+ Scenario: Linting an individual file shows warnings only from that file
8
+ Given a cookbook with a single recipe that reads node attributes via symbols,strings
9
+ And a cookbook that declares normal attributes via symbols
10
+ When I check the recipe
11
+ Then the attribute consistency warning 019 should be displayed for the recipe
12
+ And the attribute consistency warning 019 should not be displayed for the attributes
@@ -0,0 +1,65 @@
1
+ Feature: Limit rules to specific versions
2
+
3
+ In order to not be shown warnings that do not apply to my version of Chef
4
+ As a developer
5
+ I want to be able to specify the version of Chef I am using
6
+
7
+ Scenario: Rule with no version constraint - no version specified
8
+ Given a rule that does not declare a version constraint
9
+ And a cookbook that matches that rule
10
+ When I check the cookbook without specifying a Chef version
11
+ Then the warning should be displayed
12
+
13
+ Scenario Outline: Rule with no version constraint - specifying version
14
+ Given a rule that does not declare a version constraint
15
+ And a cookbook that matches that rule
16
+ When I check the cookbook specifying <version> as the Chef version
17
+ Then the warning should be displayed
18
+
19
+ Examples:
20
+ | version |
21
+ | 0.10.10.beta.1 |
22
+ | 0.10.8 |
23
+ | 0.10.6 |
24
+ | 0.10.6.rc.5 |
25
+ | 0.10.6.beta.3 |
26
+ | 0.10.4 |
27
+ | 0.10.2 |
28
+ | 0.10.0 |
29
+ | 0.9.18 |
30
+
31
+ Scenario: Rule with version constraint - no version specified
32
+ Given a rule that declares a version constraint
33
+ And the current stable version of Chef falls within it
34
+ And a cookbook that matches that rule
35
+ When I check the cookbook without specifying a Chef version
36
+ Then the warning should be displayed
37
+
38
+ Scenario: Rule with version constraint - no version specified
39
+ Given a rule that declares a version constraint
40
+ And the current stable version of Chef does not fall within it
41
+ And a cookbook that matches that rule
42
+ When I check the cookbook without specifying a Chef version
43
+ Then the warning should not be displayed
44
+
45
+ Scenario Outline: Rule with version constraint - specifying version
46
+ Given a rule that declares a version constraint of <from_version> to <to_version>
47
+ And a cookbook that matches that rule
48
+ When I check the cookbook specifying <version> as the Chef version
49
+ Then the warning <warning>
50
+
51
+ Examples:
52
+ | version | from_version | to_version | warning |
53
+ | 0.10.10.beta.1 | 0.10.10 | | should not be displayed |
54
+ | 0.10.10 | 0.10.10 | | should be displayed |
55
+ | 0.10.8 | 0.10.10 | | should not be displayed |
56
+ | 0.10.6.rc.5 | 0.10.8 | | should not be displayed |
57
+ | 0.10.6.rc.5 | 0.10.6 | | should not be displayed |
58
+ | 0.10.4 | 0.9.14 | | should be displayed |
59
+ | 0.10.4 | 0.10.10 | | should not be displayed |
60
+ | 0.10.8 | 0.9.18 | 0.10.6 | should not be displayed |
61
+ | 0.10.6 | 0.9.18 | 0.10.6 | should be displayed |
62
+ | 0.9.18 | 0.9.18 | 0.10.6 | should be displayed |
63
+ | 0.9.14 | 0.9.18 | 0.10.6 | should not be displayed |
64
+ | 0.9.14 | | 0.10.6 | should be displayed |
65
+ | 0.9.14 | | 0.9.12 | should not be displayed |
@@ -0,0 +1,11 @@
1
+ Feature: Multiple paths
2
+
3
+ In order to avoid needing to run foodcritic multiple times
4
+ As a developer
5
+ I want to lint multiple paths at once
6
+
7
+ Scenario: Linting multiple individual cookbooks
8
+ Given a cookbook with a single recipe that reads node attributes via symbols,strings
9
+ And another cookbook with a single recipe that reads node attributes via strings
10
+ When I check both cookbooks
11
+ Then the attribute consistency warning 019 should be shown
@@ -0,0 +1,20 @@
1
+ @context
2
+ Feature: Show Lines Matched
3
+
4
+ In order to understand more quickly the reason for a warning
5
+ As a developer
6
+ I want to be able to see the lines the warning matches against, with context
7
+
8
+ Scenario: Recipe with a single warning
9
+ Given a cookbook with a single recipe that reads node attributes via symbols,strings
10
+ When I check the cookbook, specifying that context should be shown
11
+ Then the recipe filename should be displayed
12
+ And the attribute consistency warning 019 should be displayed below
13
+ And the line number and line of code that triggered the warning should be displayed
14
+
15
+ Scenario: Recipe with a multiple warnings of the same type
16
+ Given a cookbook with a single recipe that reads multiple node attributes via symbols,strings
17
+ When I check the cookbook, specifying that context should be shown
18
+ Then the recipe filename should be displayed
19
+ And the attribute consistency warning 019 should be displayed below
20
+ And the line number and line of code that triggered the warnings should be displayed
@@ -0,0 +1,10 @@
1
+ Feature: Sort warnings
2
+
3
+ In order to make it easier to see which lines of my recipe have been flagged with warnings
4
+ As a developer
5
+ I want warnings to appear in line order
6
+
7
+ Scenario: Recipe has warnings on lines that don't sort non-numerically
8
+ Given a cookbook with a single recipe which accesses node attributes with symbols on lines 2 and 10
9
+ When I check the cookbook
10
+ Then the attribute consistency warning 019 should warn on lines 2 and 10 in that order
@@ -0,0 +1,25 @@
1
+ Feature: Specify search grammar
2
+
3
+ In order to allow the use of alternate search grammars when validating search syntax
4
+ As a developer
5
+ I want to be able to specify the grammar to use as a command line option
6
+
7
+ Scenario: No grammar passed
8
+ Given a cookbook recipe that attempts to perform a search with invalid syntax
9
+ When I check the cookbook
10
+ Then the invalid search syntax warning 010 should be displayed
11
+
12
+ Scenario: Missing grammar passed
13
+ Given a cookbook recipe that attempts to perform a search with invalid syntax
14
+ When I check the cookbook specifying a search grammar that does not exist
15
+ Then the check should abort with an error
16
+
17
+ Scenario: Invalid grammar passed
18
+ Given a cookbook recipe that attempts to perform a search with invalid syntax
19
+ When I check the cookbook specifying a search grammar that is not in treetop format
20
+ Then the check should abort with an error
21
+
22
+ Scenario: Valid grammar passed
23
+ Given a cookbook recipe that attempts to perform a search with invalid syntax
24
+ When I check the cookbook specifying a search grammar that is a valid treetop grammar
25
+ Then the invalid search syntax warning 010 should be displayed
@@ -0,0 +1,1791 @@
1
+ Given 'a cookbook attributes file that declares and refers to a local variable' do
2
+ write_attributes %q{
3
+ master = search(:nodes, 'foo:master')
4
+ default[:foo][:master] = master
5
+ }
6
+ end
7
+
8
+ Given /^a cookbook attributes file that refers to an attribute with (.*)$/ do |reference|
9
+ write_attributes %Q{
10
+ default['myhostname'] = #{reference}
11
+ }
12
+ end
13
+
14
+ Given 'a cookbook attributes file that sets an attribute to be the result of a library call' do
15
+ write_attributes %q{
16
+ ::Chef::Node.send(:include, Opscode::OpenSSL::Password)
17
+ default[:admin_password] = secure_password
18
+ }
19
+ end
20
+
21
+ Given 'a cookbook attributes file with a brace block that takes arguments' do
22
+ write_attributes %q{
23
+ foo = {'foo' => 'bar'}
24
+ foo.each{|k, v| default['waka'][k] = v}
25
+ }
26
+ end
27
+
28
+ Given 'a cookbook attributes file with a do block that takes arguments' do
29
+ write_attributes %q{
30
+ foo = {'foo' => 'bar'}
31
+ foo.each do |k, v|
32
+ default['waka'][k] = v
33
+ end
34
+ }
35
+ end
36
+
37
+ Given /^a cookbook attributes file with assignment (.*)$/ do |assignment|
38
+ write_attributes assignment
39
+ end
40
+
41
+ Given /^a cookbook recipe that declares (too many )?execute resources varying only in the command in branching conditionals$/ do |too_many|
42
+ extra_resource = %q{
43
+ execute "bing" do
44
+ action :run
45
+ end
46
+ }
47
+ write_recipe %Q{
48
+ if true
49
+ execute "foo" do
50
+ action :run
51
+ end
52
+ else
53
+ #{extra_resource if too_many}
54
+ execute "bar" do
55
+ action :run
56
+ end
57
+ execute "baz" do
58
+ action :run
59
+ end
60
+ end
61
+ }.strip
62
+ end
63
+
64
+ Given /^a cookbook recipe that declares a ([^ ]+) resource with the ([^ ]+) attribute set to (.*)$/ do |resource, attribute, value|
65
+ write_recipe %Q{
66
+ #{resource} "foo" do
67
+ #{attribute} #{value}
68
+ end
69
+ }
70
+ end
71
+
72
+ Given 'a cookbook recipe with a deploy resource that contains a template resource' do
73
+ write_recipe %q{
74
+ deploy '/foo/bar' do
75
+ before_restart do
76
+ template "/tmp/config.conf" do
77
+ source "foo.conf.erb"
78
+ variables({
79
+ :config_var => 'foo'
80
+ })
81
+ end
82
+ end
83
+ end
84
+ }
85
+ write_file "cookbooks/example/templates/default/foo.conf.erb", %q{
86
+ <%= @config_var %>
87
+ }
88
+ end
89
+
90
+ Given 'a cookbook recipe with a resource that notifies where the action is an expression' do
91
+ write_recipe %q{
92
+ notify_action = node['platform_family'] == "mac_os_x" ? :restart : :reload
93
+
94
+ service 'svc' do
95
+ action :nothing
96
+ end
97
+
98
+ template '/tmp/foo' do
99
+ source 'foo.erb'
100
+ notifies notify_action, 'service[svc]'
101
+ end
102
+ }
103
+ end
104
+
105
+ Given /^a cookbook recipe with an execute resource named (.*)$/ do |name|
106
+ write_recipe %Q{
107
+ execute "#{name}" do
108
+ action :run
109
+ end
110
+ }
111
+ end
112
+
113
+ Given /^a cookbook recipe with an execute resource that runs the command (.*)$/ do |command|
114
+ write_recipe %Q{
115
+ execute "do_stuff" do
116
+ command "#{command}"
117
+ end
118
+ }
119
+ end
120
+
121
+ Given /^a cookbook recipe that refers to (node.*)$/ do |reference|
122
+ write_recipe %Q{
123
+ Chef::Log.info #{reference}
124
+ }
125
+ end
126
+
127
+ Given 'a cookbook recipe that refers to an attribute with a bare keyword' do
128
+ write_recipe %q{
129
+ node['myhostname'] = hostname
130
+ }
131
+ end
132
+
133
+ Given /^a cookbook recipe that wraps a platform\-specific resource in a (.*) conditional$/ do |conditional|
134
+ write_recipe %Q{
135
+ if #{conditional}
136
+ Chef::Log.info('We matched the platform')
137
+ end
138
+ }
139
+ end
140
+
141
+ Given 'a cookbook provider that declares execute resources varying only in the command in separate actions' do
142
+ write_provider 'site', %q{
143
+ action :start do
144
+ execute "foo" do
145
+ action :run
146
+ end
147
+ new_resource.updated_by_last_action(true)
148
+ end
149
+ action :stop do
150
+ execute "bar" do
151
+ action :run
152
+ end
153
+ new_resource.updated_by_last_action(true)
154
+ end
155
+ action :restart do
156
+ execute "baz" do
157
+ action :run
158
+ end
159
+ new_resource.updated_by_last_action(true)
160
+ end
161
+ }.strip
162
+ end
163
+
164
+ Given 'a cookbook provider that declares execute resources varying only in the command in the same action' do
165
+ write_provider 'site', %q{
166
+ action :start do
167
+ execute "foo" do
168
+ action :run
169
+ end
170
+ execute "bar" do
171
+ action :run
172
+ end
173
+ execute "baz" do
174
+ action :run
175
+ end
176
+ new_resource.updated_by_last_action(true)
177
+ end
178
+ }.strip
179
+ end
180
+
181
+ Given /^a cookbook recipe that attempts to perform a search with (.*)$/ do |search_type|
182
+ recipe_with_search(search_type.include?('subexpression') ? :with_subexpression : search_type.gsub(' ', '_').to_sym)
183
+ end
184
+
185
+ Given /^a cookbook recipe that declares a resource called ([^ ]+) with the condition (.*)(in|outside) a loop$/ do |name,condition,is_loop|
186
+ write_recipe %Q{
187
+ #{'%w{rover fido}.each do |pet_name|' if is_loop == 'in'}
188
+ execute #{name} do
189
+ command "echo 'Feeding: \#{pet_name}'; touch '/tmp/\#{pet_name}'"
190
+ #{condition.nil? ? 'not_if { ::File.exists?("/tmp/\#{pet_name}")}' : condition}
191
+ end
192
+ #{'end' if is_loop == 'in'}
193
+ }
194
+ end
195
+
196
+ Given /^a cookbook recipe that declares (a resource|multiple resources) nested in a ([a-z_]+) condition with (.*)$/ do |arity, wrapping_condition, condition_attribute|
197
+ blk = "{ File.exists?('/etc/passwd') }"
198
+ str = "'test -f /etc/passwd'"
199
+ conds = wrapping_condition.split('_')
200
+ write_recipe %Q{
201
+ #{conds.first} node['foo'] == 'bar'
202
+ service "apache" do
203
+ action :enable
204
+ #{
205
+ case condition_attribute
206
+ when /(only_if|not_if) block/ then "#{$1} #{blk}"
207
+ when /(only_if|not_if) string/ then "#{$1} #{str}"
208
+ end
209
+ }
210
+ end
211
+ #{%q{service "httpd" do
212
+ action :enable
213
+ end} if arity.include?('multiple')}
214
+ #{"elsif true\nlog 'bar'" if conds.include? 'elsif'}
215
+ #{"else\nlog 'foo'" if conds.include? 'else'}
216
+ end
217
+ }
218
+ end
219
+
220
+ Given /^a cookbook recipe that declares a resource with a (.*)$/ do |conditional|
221
+ write_recipe %Q{
222
+ template "/tmp/foo" do
223
+ mode "0644"
224
+ source "foo.erb"
225
+ #{conditional}
226
+ end
227
+ }
228
+ end
229
+
230
+ Given /^a cookbook recipe (?:that declares a resource with no conditions at all|with no notifications)$/ do
231
+ write_recipe %q{
232
+ service "apache" do
233
+ action :enable
234
+ end
235
+ }
236
+ end
237
+
238
+ Given 'a cookbook recipe that declares multiple directories with different file modes' do
239
+ write_recipe %q{
240
+ directory "#{node["nagios"]["dir"]}/dist" do
241
+ owner "nagios"
242
+ group "nagios"
243
+ mode 0755
244
+ end
245
+
246
+ directory node["nagios"]["state_dir"] do
247
+ owner "nagios"
248
+ group "nagios"
249
+ mode 0751
250
+ end
251
+
252
+ directory "#{node["nagios"]["state_dir"]}/rw" do
253
+ owner "nagios"
254
+ group node["apache"]["user"]
255
+ mode 2710
256
+ end
257
+ }
258
+ end
259
+
260
+ Given 'a cookbook recipe that declares multiple package resources mixed with other resources' do
261
+ write_recipe %q{
262
+ package "erlang-base" do
263
+ action :install
264
+ end
265
+ package "erlang-corba" do
266
+ action :install
267
+ end
268
+ package "erlang-crypto" do
269
+ action :install
270
+ end
271
+ service "apache" do
272
+ supports :restart => true, :reload => true
273
+ action :enable
274
+ end
275
+ template "/tmp/somefile" do
276
+ mode "0644"
277
+ source "somefile.erb"
278
+ not_if "test -f /etc/passwd"
279
+ end
280
+ package "rabbitmq-server" do
281
+ action :install
282
+ end
283
+ }
284
+ end
285
+
286
+ Given /^a cookbook recipe that declares multiple (varying|non-varying) template resources within a block$/ do |vary|
287
+ do_vary = vary == 'varying'
288
+ write_recipe %Q{
289
+ node['apps'].each do |app|
290
+ template "/etc/#\{app\}.conf" do
291
+ owner "root"
292
+ group "root"
293
+ #{'mode "0600"' if do_vary}
294
+ end
295
+ template "/etc/init.d/#\{app\}" do
296
+ owner "root"
297
+ group "root"
298
+ #{'mode "0700"' if do_vary}
299
+ end
300
+ template "/home/#\{app\}/foo" do
301
+ owner "root"
302
+ group "root"
303
+ #{'mode "0600"' if do_vary}
304
+ end
305
+ end
306
+ }
307
+ end
308
+
309
+ Given 'a cookbook recipe that declares multiple resources varying only in the package name' do
310
+ write_recipe %q{
311
+ package "erlang-base" do
312
+ action :install
313
+ end
314
+ package "erlang-corba" do
315
+ action :install
316
+ end
317
+ package "erlang-crypto" do
318
+ action :install
319
+ end
320
+ package "rabbitmq-server" do
321
+ action :install
322
+ end
323
+ }
324
+ end
325
+
326
+ Given 'a cookbook recipe that declares multiple resources with more variation' do
327
+ write_recipe %q{
328
+ package "erlang-base" do
329
+ action :install
330
+ end
331
+ package "erlang-corba" do
332
+ action :install
333
+ end
334
+ package "erlang-crypto" do
335
+ version '13.b.3'
336
+ action :install
337
+ end
338
+ package "rabbitmq-server" do
339
+ action :install
340
+ end
341
+ }
342
+ end
343
+
344
+ Given 'a cookbook recipe that declares non contiguous package resources mixed with other resources' do
345
+ write_recipe %q{
346
+ package "erlang-base" do
347
+ action :install
348
+ end
349
+ service "apache" do
350
+ supports :restart => true, :reload => true
351
+ action :enable
352
+ end
353
+ package "erlang-crypto" do
354
+ action :install
355
+ end
356
+ template "/tmp/somefile" do
357
+ mode "0644"
358
+ source "somefile.erb"
359
+ not_if "test -f /etc/passwd"
360
+ end
361
+ package "rabbitmq-server" do
362
+ action :install
363
+ end
364
+ }
365
+ end
366
+
367
+ Given 'a cookbook recipe that declares two or fewer resources varying only in the package name' do
368
+ write_recipe %q{
369
+ package "erlang-base" do
370
+ action :install
371
+ end
372
+ package "erlang-corba" do
373
+ action :install
374
+ end
375
+ }
376
+ end
377
+
378
+ Given 'a cookbook recipe that has a confusingly named local variable "default"' do
379
+ write_recipe %q{
380
+ default = {'run_list' => 'foo'}; Chef::Log.info default['run_list']
381
+ }
382
+ end
383
+
384
+ Given 'a cookbook recipe that has a wrapping condition containing a resource with no condition attribute and a Ruby statement' do
385
+ write_recipe %q{
386
+ if node['foo'] == 'bar'
387
+ Chef::Log.info "Enabling apache to start at boot"
388
+ service "apache" do
389
+ action :enable
390
+ end
391
+ end
392
+ }
393
+ end
394
+
395
+ Given 'a cookbook recipe that has a wrapping condition containing a resource with no condition attribute within a loop' do
396
+ write_recipe %q{
397
+ unless node['bar'].include? 'something'
398
+ bars.each do |bar|
399
+ service bar['name'] do
400
+ action :enable
401
+ end
402
+ end
403
+ end
404
+ }
405
+ end
406
+
407
+ Given /^a cookbook recipe that includes a local recipe(.*)$/ do |diff_name|
408
+ cookbook = diff_name.empty? ? 'example' : 'foo'
409
+ write_recipe %Q{
410
+ include_recipe '#{cookbook}::server'
411
+ }
412
+ write_metadata %Q{
413
+ name '#{cookbook}'
414
+ }
415
+ end
416
+
417
+ Given /^a cookbook recipe that includes a recipe name from an( embedded)? expression(.*)$/ do |embedded, expr|
418
+ if embedded
419
+ write_recipe %Q{
420
+ include_recipe "#{expr.strip}"
421
+ }
422
+ else
423
+ write_recipe %q{
424
+ include_recipe node['foo']['bar']
425
+ }
426
+ end
427
+ write_metadata %q{
428
+ depends "foo"
429
+ }
430
+ end
431
+
432
+ Given /^a cookbook recipe that includes a(n un| )?declared recipe dependency( unscoped)?$/ do |undeclared,unscoped|
433
+ recipe_with_dependency(:is_declared => undeclared.strip.empty?, :is_scoped => unscoped.nil?)
434
+ end
435
+
436
+ Given 'a cookbook recipe that includes both declared and undeclared recipe dependencies' do
437
+ write_recipe %q{
438
+ include_recipe "foo::default"
439
+ include_recipe "bar::default"
440
+ file "/tmp/something" do
441
+ action :delete
442
+ end
443
+ include_recipe "baz::default"
444
+ }
445
+ write_metadata %q{
446
+ ['foo', 'bar'].each{|cbk| depends cbk}
447
+ }
448
+ end
449
+
450
+ Given /^a cookbook recipe that includes several declared recipe dependencies - (brace|block)$/ do |brace_or_block|
451
+ cookbook_declares_dependencies(brace_or_block.to_sym)
452
+ end
453
+
454
+ Given /a cookbook recipe that (install|upgrade)s (a gem|multiple gems)(.*)$/ do |action, arity, approach|
455
+ if arity == 'a gem'
456
+ if approach.empty?
457
+ recipe_installs_gem(:simple, action.to_sym)
458
+ else
459
+ recipe_installs_gem(:compile_time, action.to_sym)
460
+ end
461
+ elsif approach.include? 'array'
462
+ recipe_installs_gem(:compile_time_from_array, action.to_sym)
463
+ else
464
+ recipe_installs_gem(:compile_time_from_word_list, action.to_sym)
465
+ end
466
+ end
467
+
468
+ Given /^a cookbook recipe that refers to a (missing |local )?template( in a subdirectory)?$/ do |missing_or_local, sub_dir|
469
+ sub_dir = sub_dir ? 'sub_dir/' : ''
470
+ write_recipe %Q{
471
+ template "/tmp/config.conf" do
472
+ #{'local true' if missing_or_local == 'local '}
473
+ source "#{sub_dir}config.conf.erb"
474
+ variables({
475
+ :config_var => 'foo'
476
+ })
477
+ end
478
+ }
479
+ unless missing_or_local
480
+ write_file "cookbooks/example/templates/default/#{sub_dir}config.conf.erb", %q{
481
+ <%= @config_var %>
482
+ }
483
+ end
484
+ end
485
+
486
+ Given 'a cookbook recipe that refers to a template without an erb extension' do
487
+ write_recipe %q{
488
+ template '/etc/resolv.conf' do
489
+ source 'resolv.conf'
490
+ end
491
+ }
492
+ write_file 'cookbooks/example/templates/default/resolv.conf', ''
493
+ end
494
+
495
+ Given 'a cookbook recipe that defines a template where name is a complex expression' do
496
+ write_recipe %q{
497
+ template ::File.join(new_resource.foo.bar, "str", new_resource.baz) do
498
+ variables({
499
+ :config_var => 'foo'
500
+ })
501
+ end
502
+ }
503
+ write_file 'cookbooks/example/templates/default/barstrbaz.conf.erb', %q{
504
+ <%= @config_var %>
505
+ }
506
+ end
507
+
508
+ Given 'a cookbook recipe that defines a template where both the name and source are complex expressions' do
509
+ write_recipe %q{
510
+ template ::File.join(new_resource.foo.bar, "str", new_resource.baz) do
511
+ source new_resource.foo.template
512
+ variables({
513
+ :config_var => 'foo'
514
+ })
515
+ end
516
+ }
517
+ write_file 'cookbooks/example/templates/default/barstrbaz.conf.erb', %q{
518
+ <%= @config_var %>
519
+ }
520
+ end
521
+
522
+
523
+ Given 'a cookbook recipe that defines a template where name and source are both simple expressions' do
524
+ write_recipe %q{
525
+ template "/tmp/config-#{foo}.conf" do
526
+ source "config-#{foo}.erb"
527
+ variables({
528
+ :config_var => 'foo'
529
+ })
530
+ end
531
+ }
532
+ write_file 'cookbooks/example/templates/default/config-foo.conf.erb', %q{
533
+ <%= @config_var %>
534
+ }
535
+ end
536
+
537
+ Given /^a cookbook recipe that (refers to|infers) a template with an expression$/ do |type|
538
+ write_attributes %q{
539
+ default['foo']['name'] = 'foo'
540
+ }
541
+ write_recipe case type
542
+ when 'infers'
543
+ %q{
544
+ template "/tmp/config-#{node['foo']['name']}.conf" do
545
+ variables({
546
+ :config_var => 'foo'
547
+ })
548
+ end
549
+ }
550
+ else
551
+ %q{
552
+ template "/tmp/config.conf" do
553
+ source "config-#{node['foo']['name']}.erb"
554
+ variables({
555
+ :config_var => 'foo'
556
+ })
557
+ end
558
+ }
559
+ end
560
+ write_file 'cookbooks/example/templates/default/config-foo.conf.erb', %q{
561
+ <%= @config_var %>
562
+ }
563
+ end
564
+
565
+ Given 'a cookbook recipe that uses a template from another cookbook' do
566
+ write_recipe %q{
567
+ template "foo" do
568
+ cookbook "othercookbook"
569
+ source "source_in_the_other_cookbook.erb"
570
+ end
571
+ }
572
+ end
573
+
574
+ Given /^a cookbook recipe that uses a(?:n)? (missing )?inferred template$/ do |missing|
575
+ write_recipe %Q{
576
+ template "/tmp/config.conf" do
577
+ variables({
578
+ :config_var => 'foo'
579
+ })
580
+ end
581
+ }
582
+ unless missing
583
+ write_file 'cookbooks/example/templates/default/config.conf.erb', %q{
584
+ <%= @config_var %>
585
+ }
586
+ end
587
+ end
588
+
589
+ Given /^a cookbook recipe that uses execute to (sleep and then )?([^ ]+) a service via (.*)$/ do |sleep, action, method|
590
+ method = 'service' if method == 'the service command'
591
+ recipe_controls_service(method.include?('full path') ? :service_full_path : method.gsub(/[^a-z_]/, '_').to_sym, sleep, action)
592
+ end
593
+
594
+ Given 'a cookbook recipe that uses execute to list a directory' do
595
+ write_recipe %Q{
596
+ execute "nothing-to-see-here" do
597
+ command "ls"
598
+ action :run
599
+ end
600
+ }
601
+ end
602
+
603
+ Given 'a cookbook recipe that uses execute with a name attribute to start a service' do
604
+ write_recipe %Q{
605
+ execute "/etc/init.d/foo start" do
606
+ cwd "/tmp"
607
+ end
608
+ }
609
+ end
610
+
611
+ Given /^a cookbook recipe with a ([^ ]+) resource that subscribes to ([^ ]+) when notified by a remote_file$/ do |source, action|
612
+ write_recipe %Q{
613
+ #{source} "foo" do
614
+ action :nothing
615
+ subscribes :#{action}, resources(:remote_file => "/foo/bar"), :immediately
616
+ end
617
+ }
618
+ end
619
+
620
+ Given /^a cookbook recipe with a ([^ ]+) resource with action (.*)$/ do |resource, action|
621
+ write_recipe %Q{
622
+ #{resource} "foo" do
623
+ action :#{action}
624
+ end
625
+ }
626
+ end
627
+
628
+ Given /^a cookbook recipe with a ([^ ]+) resource with actions (.*)$/ do |resource, actions|
629
+ write_recipe %Q{
630
+ #{resource} "foo" do
631
+ action [#{actions.split(', ').map{|a| ":#{a}"}.join(", ")}]
632
+ end
633
+ }
634
+ end
635
+
636
+ Given 'a cookbook recipe with a case condition unrelated to platform' do
637
+ write_recipe %Q{
638
+ case day_of_week
639
+ when "Monday", "Tuesday"
640
+ package "foo" do
641
+ action :install
642
+ end
643
+ when "Wednesday", "Thursday"
644
+ package "bar" do
645
+ action :install
646
+ end
647
+ end
648
+ }.strip
649
+ end
650
+
651
+ Given /^a cookbook recipe with a resource that ([^ ]+)(?: )?([^ ]+)?$/ do |type,notification_timing|
652
+ write_recipe %Q{
653
+ template "/etc/foo.conf" do
654
+ #{type} :restart, "service[foo]"#{", :#{notification_timing}" if notification_timing}
655
+ end
656
+ }
657
+ end
658
+
659
+ Given /^a cookbook recipe with a resource that (notifies|subscribes) a ([^ ]+) to ([^ ]+)$/ do |type, resource, action|
660
+ notification = case type
661
+ when 'notifies' then %Q{notifies :#{action}, "#{resource}[foo]"}
662
+ when 'subscribes' then %Q{subscribes :#{action}, resources(:#{resource} => "foo")}
663
+ end
664
+ write_recipe %Q{
665
+ template "/etc/apache.conf" do
666
+ #{notification}
667
+ end
668
+ }
669
+ end
670
+
671
+ Given 'a cookbook recipe with a resource that uses the old notification syntax' do
672
+ write_recipe %q{
673
+ template "/etc/www/configures-apache.conf" do
674
+ notifies :restart, resources(:service => "apache")
675
+ end
676
+ }
677
+ end
678
+
679
+ Given /^a cookbook recipe with a '([^']+)' condition for flavours (.*)$/ do |type,flavours|
680
+ platforms = %Q{"#{flavours.split(',').join('","')}"}
681
+ if type == 'case'
682
+ @expected_line = 6
683
+ write_recipe %Q{
684
+ case node[:platform]
685
+ when "debian", "ubuntu"
686
+ package "foo" do
687
+ action :install
688
+ end
689
+ when #{platforms}
690
+ package "bar" do
691
+ action :install
692
+ end
693
+ end
694
+ }.strip
695
+ elsif type == 'platform?'
696
+ @expected_line = 1
697
+ write_recipe %Q{
698
+ if platform?(#{platforms})
699
+ package "bar" do
700
+ action :install
701
+ end
702
+ end
703
+ }.strip
704
+ else
705
+ fail "Unrecognised type: #{type}"
706
+ end
707
+ end
708
+
709
+ Given 'a cookbook recipe with a service resource that does not specify an action' do
710
+ write_recipe %q{
711
+ service "foo" do
712
+ start_command "/sbin/service foo start"
713
+ end
714
+ }.strip
715
+ end
716
+
717
+ Given 'a cookbook recipe with a service resource with an action specified via a variable' do
718
+ write_recipe %q{
719
+ service "foo" do
720
+ action action
721
+ end
722
+ }.strip
723
+ end
724
+
725
+ Given 'a cookbook template that uses all variables passed' do
726
+ write_recipe %q{
727
+ template "/tmp/config.conf" do
728
+ source "config.conf.erb"
729
+ variables(
730
+ :config_var => node[:configs][:config_var]
731
+ )
732
+ end
733
+ }
734
+ write_file 'cookbooks/example/templates/default/config.conf.erb', %q{
735
+ <%= @config_var %>
736
+ }
737
+ end
738
+
739
+ Given /^a cookbook that passes no variables to a template$/ do
740
+ write_recipe %q{
741
+ template "/tmp/config.conf" do
742
+ source "config.conf.erb"
743
+ end
744
+ }
745
+ end
746
+
747
+ Given /^a cookbook that passes variables (.*) to a template with extension (.*)$/ do |vars, ext|
748
+ write_recipe %Q{
749
+ template "/tmp/config.conf" do
750
+ source "config#{ext}"
751
+ variables(
752
+ :#{vars.split(',').map{|v| "#{v} => node[:#{v}]"}.join(",\n:")}
753
+ )
754
+ end
755
+ }
756
+ end
757
+
758
+ Given /^a cookbook that contains a (short|long) ruby block$/ do |length|
759
+ recipe_with_ruby_block(length.to_sym)
760
+ end
761
+
762
+ Given 'a cookbook that contains a definition' do
763
+ write_definition("apache_site", %q{
764
+ define :apache_site, :enable => true do
765
+ log "I am a definition"
766
+ end
767
+ })
768
+ write_recipe %q{
769
+ apache_site "default"
770
+ }
771
+ end
772
+
773
+ Given /^a cookbook that contains a LWRP (?:with a single notification|that uses the current notification syntax)$/ do
774
+ cookbook_with_lwrp({:notifies => :does_notify})
775
+ end
776
+
777
+ Given 'a cookbook that contains a LWRP with a single notification without parentheses' do
778
+ cookbook_with_lwrp({:notifies => :does_notify_without_parens})
779
+ end
780
+
781
+ Given /^a cookbook that contains a LWRP that declares a resource called ([^ ]+) with the condition (.*)$/ do |name,condition|
782
+ write_resource("site", %q{
783
+ actions :create
784
+ attribute :name, :name_attribute => true
785
+ })
786
+ write_provider("site", %Q{
787
+ action :create do
788
+ execute #{name} do
789
+ command "echo 'Creating: \#{new_resource.name}'; touch '/tmp/\#{new_resource.name}'"
790
+ #{condition}
791
+ end
792
+ end
793
+ })
794
+ end
795
+
796
+ Given /^a cookbook that contains a LWRP that (?:does not trigger notifications|declares a resource with no condition)$/ do
797
+ write_resource("site", %q{
798
+ actions :create
799
+ attribute :name, :kind_of => String, :name_attribute => true
800
+ })
801
+ write_provider("site", %q{
802
+ action :create do
803
+ log "Here is where I would create a site"
804
+ end
805
+ })
806
+ end
807
+
808
+ Given /^a cookbook that contains a LWRP that uses the deprecated notification syntax(.*)$/ do |qualifier|
809
+ cookbook_with_lwrp({:notifies => qualifier.include?('class variable') ? :class_variable : :deprecated_syntax})
810
+ end
811
+
812
+ Given 'a cookbook that contains a LWRP with multiple notifications' do
813
+ write_resource("site", %q{
814
+ actions :create, :delete
815
+ attribute :name, :kind_of => String, :name_attribute => true
816
+ })
817
+ write_provider("site", %q{
818
+ action :create do
819
+ log "Here is where I would create a site"
820
+ new_resource.updated_by_last_action(true)
821
+ end
822
+ action :delete do
823
+ log "Here is where I would delete a site"
824
+ new_resource.updated_by_last_action(true)
825
+ end
826
+ })
827
+ end
828
+
829
+ Given /^a cookbook that contains a LWRP with (no|a) default action( defined via a constructor)?$/ do |has_default_action,no_dsl|
830
+ default_action = if has_default_action == 'no'
831
+ :no_default_action
832
+ elsif no_dsl.nil?
833
+ :dsl_default_action
834
+ else
835
+ :ruby_default_action
836
+ end
837
+ cookbook_with_lwrp({:default_action => default_action,
838
+ :notifies => :does_notify})
839
+ end
840
+
841
+ Given 'a cookbook that contains no ruby blocks' do
842
+ write_recipe %q{
843
+ package "tar" do
844
+ action :install
845
+ end
846
+ }
847
+ end
848
+
849
+ Given /^a cookbook that declares ([a-z]+) attributes via symbols$/ do |attribute_type|
850
+ attributes_with_symbols(attribute_type)
851
+ end
852
+
853
+ Given /^a cookbook that does not contain a definition and has (no|a) definitions directory$/ do |has_dir|
854
+ create_dir 'cookbooks/example/definitions/' unless has_dir == 'no'
855
+ write_recipe %q{
856
+ log "A defining characteristic of this cookbook is that it has no definitions"
857
+ }
858
+ end
859
+
860
+ Given 'a cookbook that does not have a README at all' do
861
+ write_recipe %q{
862
+ log "Use the source luke"
863
+ }
864
+ end
865
+
866
+ Given 'a cookbook that does not have defined metadata' do
867
+ write_recipe %q{
868
+ include_recipe "foo::default"
869
+ }
870
+ end
871
+
872
+ Given /^a cookbook that downloads a file to (.*)$/ do |path|
873
+ recipe_downloads_file({'/tmp' => :tmp_dir, '/tmp with an expression' => :tmp_dir_expr,
874
+ 'the Chef file cache' => :chef_file_cache_dir,
875
+ 'a users home directory' => :home_dir}[path])
876
+ end
877
+
878
+ Given /^a cookbook that has ([^ ]+) problems$/ do |problems|
879
+ cookbook_that_matches_rules(
880
+ problems.split(',').map do |problem|
881
+ case problem
882
+ when 'no ' then next
883
+ when 'style' then 'FC002'
884
+ when 'correctness' then 'FC006'
885
+ end
886
+ end
887
+ )
888
+ end
889
+
890
+ Given 'a cookbook that has a README in markdown format' do
891
+ write_recipe %q{
892
+ log "Hello"
893
+ }
894
+ write_file 'cookbooks/example/README.md', %q{
895
+ Description
896
+ ===========
897
+
898
+ Hi. This is markdown.
899
+ }
900
+ end
901
+
902
+ Given 'a cookbook that has a README in RDoc format' do
903
+ write_recipe %q{
904
+ log "Hello"
905
+ }
906
+ write_file 'cookbooks/example/README.rdoc', %q{
907
+ = DESCRIPTION:
908
+
909
+ I used to be the preferred format but not any more (sniff).
910
+ }
911
+ end
912
+
913
+ Given /^a cookbook that has maintainer metadata set to (.*) and ([^ ]+)$/ do |name,email|
914
+ cookbook_with_maintainer(nil_if_unspecified(name), nil_if_unspecified(email))
915
+ end
916
+
917
+ Given 'a cookbook that has the default boilerplate metadata generated by knife' do
918
+ cookbook_with_maintainer('YOUR_COMPANY_NAME', 'YOUR_EMAIL')
919
+ end
920
+
921
+ Given /^a cookbook that matches rules (.*)$/ do |rules|
922
+ cookbook_that_matches_rules(rules.split(','))
923
+ end
924
+
925
+ Given 'a cookbook that matches that rule' do
926
+ write_recipe %q{
927
+ execute "bar" do
928
+ action :run
929
+ end
930
+ }
931
+ end
932
+
933
+ Given /^a cookbook with a ([^ ]+) that (includes|does not include) a breakpoint$/ do |component,includes|
934
+ content = case component
935
+ when 'template' then includes == 'includes' ? "Hello <% require 'pry'; binding.pry %>" : 'Hello World'
936
+ else includes == 'includes' ? 'binding.pry' : '# No breakpoint'
937
+ end
938
+ write_recipe ''
939
+ case component
940
+ when 'library' then write_library 'foo', content
941
+ when 'metadata' then write_metadata content
942
+ when 'provider' then write_provider 'foo', content
943
+ when 'recipe' then write_recipe content
944
+ when 'resource' then write_resource 'foo', content
945
+ when 'template' then write_file 'cookbooks/example/templates/default/foo.erb',
946
+ content
947
+ else fail "Unrecognised component: #{component}"
948
+ end
949
+ end
950
+
951
+ Given /^a cookbook with a single recipe for which the first hash (key|value) is an interpolated string$/ do |key_or_value|
952
+ write_recipe case key_or_value
953
+ when 'key' then %q{{"#{foo}" => 'bar', 'bar' => 'foo'}}
954
+ when 'value' then %q{{'foo' => "#{bar}", 'bar' => 'foo'}}
955
+ end
956
+ end
957
+
958
+ Given 'a cookbook with a single recipe that mixes node access types in an interpolated value' do
959
+ write_recipe %q{
960
+ execute "interpolated-example" do
961
+ command "#{node['foo'][:bar]}'"
962
+ end
963
+ }
964
+ end
965
+
966
+ Given 'a cookbook with a single recipe that reads multiple node attributes via symbols,strings' do
967
+ write_recipe %q{
968
+ node[:foo] = 'bar'
969
+ node[:baz] = 'foo'
970
+ node[:wham] = 'shazam'
971
+ node['testing'] = 'bar'
972
+ node['testing2'] = 'bar2'
973
+ }
974
+ end
975
+
976
+ Given 'a cookbook with a single recipe that accesses nested node attributes via symbols' do
977
+ write_recipe %q{node[:foo][:foo2] = 'bar'}
978
+ end
979
+
980
+ Given 'a cookbook with a single recipe that reads node attributes via symbols and quoted_symbols' do
981
+ write_recipe %q{default[:foo][:'bar-baz']}
982
+ end
983
+
984
+ Given /^a cookbook with a single recipe that triggers FC019 with comment (.*)$/ do |comment|
985
+ write_recipe %Q{
986
+ file node[:bar] do
987
+ content node['foo'] #{comment}
988
+ action:create
989
+ end
990
+ }.strip
991
+ end
992
+
993
+ Given 'a cookbook with a single recipe that calls a patched node method' do
994
+ write_library 'search', %q{
995
+ class Chef
996
+ class Node
997
+ def in_tier?(*tier)
998
+ tier.flatten.include?(node['tier'])
999
+ end
1000
+ end
1001
+ end
1002
+ }
1003
+ write_recipe %q{
1004
+ if node['something']['bar'] || node.in_tier?('foof')
1005
+ Chef::Log.info("Node has been patched")
1006
+ end
1007
+ }
1008
+ end
1009
+
1010
+ Given /^a cookbook with a single recipe that explicitly calls a node method( with multiple arguments)?$/ do |multi|
1011
+ write_recipe %Q{
1012
+ if node[:bar] and node.foo(#{multi ? 'bar, baz' : ''})
1013
+ Chef::Log.info('Explicit node method call should be ignored')
1014
+ end
1015
+ }
1016
+ end
1017
+
1018
+ Given 'a cookbook with a single recipe that passes node attributes accessed via symbols to a template' do
1019
+ write_recipe %q{
1020
+ template "/etc/foo" do
1021
+ source "foo.erb"
1022
+ variables({
1023
+ :port => node[:foo][:port],
1024
+ :user => node[:foo][:user]
1025
+ })
1026
+ end
1027
+ }.strip
1028
+ end
1029
+
1030
+ Given 'a cookbook with a single recipe that uses a hash value to access a node attribute' do
1031
+ write_recipe %q{
1032
+ some_hash = {
1033
+ :key => "value"
1034
+ }
1035
+ execute "accesses-hash" do
1036
+ command "echo #{node['foo'][some_hash[:key]]}"
1037
+ end
1038
+ }.strip
1039
+ end
1040
+
1041
+ Given /a(nother)? cookbook with a single recipe that (reads|updates|ignores)(nested)? node attributes via ([a-z,]*)(?:(?: and calls node\.)?([a-z_?]+)?| with (.*)?)(?: only)?$/ do |more_than_one,op,nested,types,method,expr|
1042
+ cookbook_name = more_than_one.nil? ? 'example' : 'another_example'
1043
+
1044
+ access = nested.nil? ? {:strings => "['foo']", :symbols => '[:foo]', :vivified => '.foo'} :
1045
+ {:strings => "['bar']['baz']", :symbols => '[:fee][:fi][:fo][:fum]', :vivified => '.bar.baz'}
1046
+
1047
+ recipe_content =
1048
+ (if types == 'none'
1049
+ "log 'hello world'"
1050
+ elsif op == 'reads'
1051
+ types.split(',').map{|type| "log node#{access[type.to_sym]}"}.join("\n")
1052
+ else
1053
+ types.split(',').map{|type| "node#{access[type.to_sym]} = 'foo'"}.join("\n")
1054
+ end)
1055
+
1056
+ recipe_content += "\n#{expr}"
1057
+
1058
+ unless method.nil?
1059
+ recipe_content += {:platform? => "node.platform?('redhat')",
1060
+ :run_list => "log 'hello' if node.run_list.roles.include?(node[:foo][:bar])",
1061
+ :run_state => "node.run_state[:reboot_requested] = true",
1062
+ :set => "node.set['foo']['bar']['baz'] = 'secret'"}[method.to_sym]
1063
+ end
1064
+
1065
+ write_recipe(recipe_content, cookbook_name)
1066
+
1067
+ end
1068
+
1069
+ Given 'a cookbook with a single recipe that searches based on a node attribute accessed via strings' do
1070
+ write_recipe %q{
1071
+ remote = search(:node, "name:#{node['drbd']['remote_host']}")[0]
1072
+ }.strip
1073
+ end
1074
+
1075
+ Given 'a cookbook with a single recipe which accesses node attributes with symbols on lines 2 and 10' do
1076
+ write_recipe %q{
1077
+ # Here we access the node attributes via a symbol
1078
+ foo = node[:foo]
1079
+
1080
+ # String access is in the majority
1081
+ node['foo']
1082
+ node['bar']
1083
+ node['baz']
1084
+
1085
+ # Second access via a symbol
1086
+ bar = node[:bar]
1087
+ }
1088
+ end
1089
+
1090
+ Given 'a cookbook with a single recipe that assigns node attributes accessed via symbols to a local variable' do
1091
+ write_recipe %q{baz = node[:foo]}
1092
+ end
1093
+
1094
+ Given /^a cookbook with a single recipe that creates a directory resource with (.*)$/ do |path_type|
1095
+ recipe_with_dir_path({'an interpolated name' => :interpolated_symbol,
1096
+ 'an interpolated name from a string' => :interpolated_string,
1097
+ 'a string literal' => :string_literal,
1098
+ 'a compound expression' => :compound_symbols,
1099
+ 'an interpolated variable and a literal' => :interpolated_symbol_and_literal,
1100
+ 'a literal and interpolated variable' => :literal_and_interpolated_symbol}[path_type])
1101
+ end
1102
+
1103
+ Given 'a cookbook with a single recipe that searches but checks first (alternation) to see if this is server' do
1104
+ write_recipe %q{
1105
+ if Chef::Config[:solo] || we_dont_want_to_use_search
1106
+ # set up stuff from attributes
1107
+ else
1108
+ # set up stuff from search
1109
+ nodes = search(:node, "hostname:[* TO *] AND chef_environment:#{node.chef_environment}")
1110
+ end
1111
+ }
1112
+ end
1113
+
1114
+ Given /^a cookbook with a single recipe that searches but checks first( \(string\))? to see if this is server$/ do |str|
1115
+ write_recipe %Q{
1116
+ if Chef::Config[#{str ? "'solo'" : ":solo"}]
1117
+ Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
1118
+ else
1119
+ nodes = search(:node, "hostname:[* TO *] AND chef_environment:#\{node.chef_environment\}")
1120
+ end
1121
+ }
1122
+ end
1123
+
1124
+ Given /^a cookbook with a single recipe that searches but checks with a negative first to see if this is server$/ do
1125
+ write_recipe %q{
1126
+ unless Chef::Config['solo']
1127
+ nodes = search(:node, "hostname:[* TO *]")
1128
+ else
1129
+ Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
1130
+ end
1131
+ }
1132
+ end
1133
+
1134
+ Given /^a cookbook with a single recipe that searches but checks first \(method\) to see if this is server$/ do
1135
+ write_recipe %Q{
1136
+ if Chef::Config.solo
1137
+ Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
1138
+ else
1139
+ nodes = search(:node, "hostname:[* TO *] AND chef_environment:#\{node.chef_environment\}")
1140
+ end
1141
+ }
1142
+ end
1143
+
1144
+ Given /^a cookbook with a single recipe that searches but returns first \((oneline|multiline)\) if search is not supported$/ do |format|
1145
+ if format == 'oneline'
1146
+ write_recipe %q{
1147
+ return Chef::Log.warn("This recipe uses search. Chef Solo does not support search.") if Chef::Config[:solo]
1148
+ nodes = search(:node, "hostname:[* TO *] AND chef_environment:#{node.chef_environment}")
1149
+ }
1150
+ else
1151
+ write_recipe %q{
1152
+ if Chef::Config[:solo]
1153
+ return Chef::Log.warn("This recipe uses search. Chef Solo does not support search.")
1154
+ end
1155
+ nodes = search(:node, "hostname:[* TO *] AND chef_environment:#{node.chef_environment}")
1156
+ }
1157
+ end
1158
+ end
1159
+
1160
+ Given 'a cookbook with a single recipe that searches without checking if this is server' do
1161
+ write_recipe %q{nodes = search(:node, "hostname:[* TO *] AND chef_environment:#{node.chef_environment}")}
1162
+ end
1163
+
1164
+ Given 'a cookbook with five recipes' do
1165
+
1166
+ end
1167
+
1168
+ Given /^a cookbook with metadata that declares a recipe with (.*)$/ do |declaration|
1169
+ write_metadata declaration
1170
+ write_recipe ""
1171
+ end
1172
+
1173
+ Given /^a cookbook with metadata that (specifies|does not specify) the cookbook name$/ do |specifies|
1174
+ write_metadata %Q{
1175
+ #{"name 'example'" if specifies == 'specifies'}
1176
+ }
1177
+ end
1178
+
1179
+ Given /^a ([a-z_]+) resource declared with the mode ([^\s]+)(?: with comment (.*)?)?$/ do |resource,mode,comment|
1180
+ recipe_resource_with_mode(resource, mode, comment)
1181
+ end
1182
+
1183
+ Given 'a file resource declared without a mode' do
1184
+ write_recipe %q{
1185
+ file "/tmp/something" do
1186
+ action :delete
1187
+ end
1188
+ }
1189
+ end
1190
+
1191
+ Given /^a file with multiple errors on one line(?: with comment (.*))?$/ do |comment|
1192
+ write_file "cookbooks/example/recipes/default.rb", %Q{node['run_state']['nginx_force_recompile'] = "\#{foo}"#{comment}}
1193
+ end
1194
+
1195
+ Given /^a Rakefile that defines (no lint task|a lint task with no block|a lint task with an empty block|a lint task with a block setting options to)(.*)?$/ do |task,options|
1196
+ rakefile(
1197
+ case task
1198
+ when /no block/ then :no_block
1199
+ when /empty block/ then :empty_block
1200
+ when /a block/ then :block
1201
+ end,
1202
+ options.strip.empty? ? {} : {:options => options.strip})
1203
+ end
1204
+
1205
+ Given /^a Rakefile that defines a lint task specifying files to lint as (.*)$/ do |files|
1206
+ rakefile(:block, :files => files)
1207
+ end
1208
+
1209
+ Given 'a Rakefile that defines a lint task specifying a different name' do
1210
+ rakefile(:block, :name => 'lint')
1211
+ end
1212
+
1213
+ Given 'a recipe that contains a ruby block without a block attribute' do
1214
+ write_recipe %q{
1215
+ ruby_block "missing block" do
1216
+ puts "Missing a block attribute"
1217
+ end
1218
+ }
1219
+ end
1220
+
1221
+ Given 'a recipe that contains both long and short ruby blocks' do
1222
+ recipe_with_ruby_block(:both)
1223
+ end
1224
+
1225
+ Given /^a recipe that declares a ([^ ]+) resource with these attributes: (.*)$/ do |type,attributes|
1226
+ recipe_with_resource(type, attributes.split(','))
1227
+ end
1228
+
1229
+ Given 'a recipe that declares a resource with an attribute value set to the result of a method call' do
1230
+ write_recipe %q{
1231
+ cron "run a command at a random minute" do
1232
+ user "root"
1233
+ minute rand(60)
1234
+ command "/usr/bin/whatever"
1235
+ end
1236
+ }
1237
+ end
1238
+
1239
+ Given 'a recipe that declares a resource with only a name attribute' do
1240
+ write_recipe %q{
1241
+ package 'foo'
1242
+ }
1243
+ end
1244
+
1245
+ Given 'a recipe that declares a resource with recognised attributes and a conditional execution ruby block' do
1246
+ write_recipe %q{
1247
+ file "/tmp/something" do
1248
+ owner "root"
1249
+ group "root"
1250
+ mode "0755"
1251
+ not_if do
1252
+ require 'foo'
1253
+ Foo.bar?(filename)
1254
+ end
1255
+ action :create
1256
+ end
1257
+ }
1258
+ end
1259
+
1260
+ Given 'a recipe that declares a resource with recognised attributes and a nested block' do
1261
+ write_recipe %q{
1262
+ deploy_revision "foo" do
1263
+ revision "HEAD"
1264
+ repository "git://github.com/git/git.git"
1265
+ deploy_to "/foo"
1266
+ action :deploy
1267
+ before_migrate do
1268
+ execute "bundle install" do
1269
+ cwd release_path
1270
+ action :run
1271
+ end
1272
+ end
1273
+ end
1274
+ }
1275
+ end
1276
+
1277
+ Given 'a recipe that declares a resource with standard attributes' do
1278
+ write_recipe %q{
1279
+ file "/tmp/something" do
1280
+ owner "root"
1281
+ group "root"
1282
+ mode "0755"
1283
+ action :create
1284
+ end
1285
+ }
1286
+ end
1287
+
1288
+ Given 'a recipe that declares a user-defined resource' do
1289
+ write_recipe %q{
1290
+ apple "golden-delicious" do
1291
+ colour "yellow"
1292
+ action :consume
1293
+ end
1294
+ }
1295
+ end
1296
+
1297
+ Given 'a recipe that declares multiple resources of the same type of which one has a bad attribute' do
1298
+ write_recipe %q{
1299
+ file "/tmp/something" do
1300
+ owner "root"
1301
+ group "root"
1302
+ mode "0755"
1303
+ action :create
1304
+ end
1305
+ file "/tmp/something" do
1306
+ punter "root"
1307
+ group "root"
1308
+ mode "0755"
1309
+ action :create
1310
+ end
1311
+ package "foo" do
1312
+ action :install
1313
+ end
1314
+ }
1315
+ end
1316
+
1317
+ Given /^a rule that (declares|does not declare) a version constraint(?: of ([^ ]+)? to ([^ ]+)?)?$/ do |constraint, from, to|
1318
+ if from || to
1319
+ rule_with_version_constraint(from, to)
1320
+ else
1321
+ from_version = case constraint
1322
+ when /not/ then nil
1323
+ else '0.10.6'
1324
+ end
1325
+ rule_with_version_constraint(from_version, nil)
1326
+ end
1327
+ end
1328
+
1329
+ Given /^another cookbook that has (an older )?chef-solo-search installed$/ do |older|
1330
+ if older.nil?
1331
+ write_library 'search', %q{
1332
+ class Chef
1333
+ module Mixin
1334
+ module Language
1335
+ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
1336
+ # https://github.com/edelight/chef-solo-search
1337
+ end
1338
+ end
1339
+ end
1340
+ end
1341
+ }
1342
+ else
1343
+ write_library 'search', %q{
1344
+ class Chef
1345
+ class Recipe
1346
+ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
1347
+ # https://github.com/edelight/chef-solo-search
1348
+ end
1349
+ end
1350
+ end
1351
+ }
1352
+ end
1353
+ end
1354
+
1355
+ Given 'I have installed the lint tool' do
1356
+
1357
+ end
1358
+
1359
+ Given 'metadata' do
1360
+ write_metadata %q{
1361
+ name 'example'
1362
+ }.strip
1363
+ end
1364
+
1365
+ Given /^(?:a cookbook that has|the cookbook has) a Gemfile that includes rake and foodcritic$/ do
1366
+ buildable_gemfile
1367
+ end
1368
+
1369
+ Given /^the current stable version of Chef (falls|does not fall) within it$/ do |falls_within|
1370
+ rule_with_version_constraint("98.10", nil) unless falls_within.include?('falls')
1371
+ end
1372
+
1373
+ Given 'the gems have been vendored' do
1374
+ vendor_gems
1375
+ end
1376
+
1377
+ Given /^the template (.+)?contains the expression (.*)$/ do |ext,expr|
1378
+ file = if ext
1379
+ "templates/default/config#{ext.strip}"
1380
+ else
1381
+ 'templates/default/config.conf.erb'
1382
+ end
1383
+ write_file "cookbooks/example/#{file}", %Q{
1384
+ <%= #{expr} %>
1385
+ }
1386
+ end
1387
+
1388
+ Given 'unit tests under a top-level test directory' do
1389
+ minitest_spec_attributes
1390
+ end
1391
+
1392
+ Given 'a recipe that installs a gem with 5 retries' do
1393
+ write_recipe %q{
1394
+ gem_package "foo" do
1395
+ retries 5
1396
+ action :install
1397
+ end
1398
+ }
1399
+ end
1400
+
1401
+ Given 'a recipe that installs a package with yum specifying the architecture' do
1402
+ write_recipe %q{
1403
+ yum_package "foo" do
1404
+ arch "x86_64"
1405
+ action :install
1406
+ end
1407
+ }
1408
+ end
1409
+
1410
+ Given 'a recipe that reconfigures a package' do
1411
+ write_recipe %q{
1412
+ apt_package "foo" do
1413
+ action :reconfig
1414
+ end
1415
+ }
1416
+ end
1417
+
1418
+ Given /^a recipe that uses require_recipe$/ do
1419
+ write_recipe %Q{
1420
+ require_recipe "foo::default"
1421
+ }
1422
+ end
1423
+
1424
+ Given /^a recipe that uses include_recipe$/ do
1425
+ write_recipe %Q{
1426
+ include_recipe "foo::default"
1427
+ }
1428
+ end
1429
+
1430
+ Given /^the cookbook metadata declares support for (.*)$/ do |supported_platforms|
1431
+ write_metadata(supported_platforms.split(',').map do |platform|
1432
+ "supports '#{platform}'"
1433
+ end.join("\n"))
1434
+ end
1435
+
1436
+ Given 'the cookbook metadata declares support with versions specified' do
1437
+ write_metadata %q{
1438
+ supports 'redhat', '>= 6'
1439
+ supports 'scientific', '>= 6'
1440
+ }.strip
1441
+ end
1442
+
1443
+ Given 'three of the recipes read node attributes via strings' do
1444
+ (1..3).map{|i| "string_#{i}"}.each do |recipe|
1445
+ write_file "cookbooks/example/recipes/#{recipe}.rb", "Chef::Log.warn node['foo']"
1446
+ end
1447
+ end
1448
+
1449
+ Given 'two of the recipes read node attributes via symbols' do
1450
+ (1..2).map{|i| "symbol_#{i}"}.each do |recipe|
1451
+ write_file "cookbooks/example/recipes/#{recipe}.rb", "Chef::Log.warn node[:foo]"
1452
+ end
1453
+ end
1454
+
1455
+ When /^I check the cookbook specifying ([^ ]+) as the Chef version$/ do |version|
1456
+ options = ['-c', version, 'cookbooks/example']
1457
+ in_current_dir do
1458
+ options = ['-I', 'rules/test.rb'] + options if Dir.exists?('rules')
1459
+ end
1460
+ run_lint(options)
1461
+ end
1462
+
1463
+ When /^I check the cookbook( tree)?(?: specifying tags(.*))?(, specifying that context should be shown)?$/ do |whole_tree, tags, context|
1464
+ options = tags.nil? ? [] : tags.split(' ')
1465
+ options += ['-C'] unless context.nil?
1466
+ run_lint(options + ["cookbooks/#{whole_tree.nil? ? 'example' : ''}"])
1467
+ end
1468
+
1469
+ Given /^the cookbook directory has a \.foodcritic file specifying tags (.*)$/ do |tags|
1470
+ write_file "cookbooks/example/.foodcritic", tags
1471
+ run_lint(["cookbooks/example"])
1472
+ end
1473
+
1474
+ When 'I check both cookbooks' do
1475
+ run_lint(["cookbooks/another_example", "cookbooks/example"])
1476
+ end
1477
+
1478
+ When 'I check the cookbook without specifying a Chef version' do
1479
+ run_lint(['-I', 'rules/test.rb', 'cookbooks/example'])
1480
+ end
1481
+
1482
+ When 'I check the recipe' do
1483
+ run_lint(["cookbooks/example/recipes/default.rb"])
1484
+ end
1485
+
1486
+ When 'I list the available build tasks' do
1487
+ list_available_build_tasks
1488
+ end
1489
+
1490
+ When /^I run it on the command line including a custom rule (file|directory) containing a rule that matches$/ do |path_type|
1491
+ write_file 'rules/custom_rules.rb', %q{
1492
+ rule "BAR001", "Use symbols in preference to strings to access node attributes" do
1493
+ tags %w{style attributes}
1494
+ recipe do |ast|
1495
+ attribute_access(ast, :type => :string).map{|ar| match(ar)}
1496
+ end
1497
+ end
1498
+ }
1499
+ run_lint(['-I',
1500
+ path_type == 'file' ? 'rules/custom_rules.rb' : 'rules',
1501
+ 'cookbooks/example'])
1502
+ end
1503
+
1504
+ When /^I run it on the command line including a file which does not contain Ruby code$/ do
1505
+ write_file 'rules/invalid_rules.rb', 'echo "not ruby"'
1506
+ capture_error do
1507
+ run_lint(['-I', 'rules/invalid_rules.rb', 'cookbooks/example'])
1508
+ end
1509
+ end
1510
+
1511
+ When /^I run it on the command line including a missing custom rule file$/ do
1512
+ capture_error do
1513
+ run_lint(['-I', 'rules/missing_rules.rb', 'cookbooks/example'])
1514
+ end
1515
+ end
1516
+
1517
+ When 'I run it on the command line specifying a cookbook that does not exist' do
1518
+ run_lint(['no-such-cookbook'])
1519
+ end
1520
+
1521
+ When 'I run it on the command line with no arguments' do
1522
+ run_lint([])
1523
+ end
1524
+
1525
+ When /^I run it on the command line with the (?:unimplemented |)([^ ]+) option( with an argument)?$/ do |option, with_argument|
1526
+ options = []
1527
+ if option.match(/\-\w$/)
1528
+ options << option
1529
+ else
1530
+ options << "--#{option}"
1531
+ end
1532
+ options << "cookbooks/example" unless with_argument.nil?
1533
+ run_lint(options)
1534
+ end
1535
+
1536
+ When 'I run the build' do
1537
+ run_build
1538
+ end
1539
+
1540
+ Then 'a warning for the custom rule should be displayed' do
1541
+ expect_output('BAR001: Use symbols in preference to strings to access node attributes: cookbooks/example/recipes/default.rb:1')
1542
+ end
1543
+
1544
+ Then /^an? '([^']+)' error should be displayed$/ do |expected_error|
1545
+ last_error.should include expected_error
1546
+ end
1547
+
1548
+ Then 'the attribute consistency warning 019 should be shown for both of the recipes that use symbols' do
1549
+ expect_warning 'FC019', :file => 'recipes/symbol_1.rb'
1550
+ expect_warning 'FC019', :file => 'recipes/symbol_2.rb'
1551
+ end
1552
+
1553
+ Then /^the bare attribute keys warning 044 should not be displayed against the (brace|do) block$/ do |block_type|
1554
+ line = block_type == 'brace' ? 2 : 3
1555
+ expect_warning 'FC044', {:expect_warning => false, :line => line, :file_type => :attributes}
1556
+ end
1557
+
1558
+ Then /^the bare attribute keys warning 044 should not be displayed against the (?:local variable|library call)$/ do
1559
+ expect_warning 'FC044', {:expect_warning => false, :line => 2, :file_type => :attributes}
1560
+ end
1561
+
1562
+ Then 'the long ruby block warning 014 should be displayed against the long block only' do
1563
+ expect_warning 'FC014', {:expect_warning => false, :line => 1}
1564
+ expect_warning 'FC014', {:expect_warning => true, :line => 11}
1565
+ end
1566
+
1567
+ Then /^the lint task will be listed( under the different name)?$/ do |diff_name|
1568
+ expected_name = diff_name ? 'lint' : 'foodcritic'
1569
+ build_tasks.should include([expected_name, 'Lint Chef cookbooks'])
1570
+ end
1571
+
1572
+ Then 'no error should have occurred' do
1573
+ assert_no_error_occurred
1574
+ end
1575
+
1576
+ Then /^no warnings will be displayed against the tests$/ do
1577
+ assert_no_test_warnings
1578
+ end
1579
+
1580
+ Then 'the attribute consistency warning 019 should warn on lines 2 and 10 in that order' do
1581
+ expected_warnings = [2, 10].map do |line|
1582
+ "FC019: Access node attributes in a consistent manner: cookbooks/example/recipes/default.rb:#{line}"
1583
+ end
1584
+ expect_output(expected_warnings.join("\n"))
1585
+ end
1586
+
1587
+ Then 'the attribute consistency warning 019 should be displayed for the recipe' do
1588
+ expect_warning('FC019', :line => 2)
1589
+ end
1590
+
1591
+ Then 'the attribute consistency warning 019 should not be displayed for the attributes' do
1592
+ expect_warning('FC019', :file_type => :attributes, :line => 1, :expect_warning => false)
1593
+ end
1594
+
1595
+ Then /^the warning ([0-9]+ )?should (not )?be (?:displayed|shown)$/ do |warning,should_not|
1596
+ code = warning.nil? ? 'FCTEST001' : "FC#{warning.strip}"
1597
+ expect_warning code, {:expect_warning => should_not.nil?}
1598
+ end
1599
+
1600
+ Then /^the (?:[a-zA-Z \-_]+) warning ([0-9]+) should (not )?be displayed(?: against the (attributes|libraries|definition|metadata|provider|resource|README.md|README.rdoc) file)?( below)?$/ do |code, no_display, file, warning_only|
1601
+ options = {}
1602
+ options[:expect_warning] = no_display != 'not '
1603
+ unless file.nil?
1604
+ if file.include?('.')
1605
+ options[:file] = file
1606
+ else
1607
+ options[:file_type] = file.to_sym
1608
+ end
1609
+ end
1610
+ options[:line] = 3 if code == '018' and options[:expect_warning]
1611
+ options[:line] = 2 if ['021', '022'].include?(code)
1612
+ options[:warning_only] = ! warning_only.nil?
1613
+ expect_warning("FC#{code}", options)
1614
+ end
1615
+
1616
+ Then /^the attribute consistency warning 019 should be (shown|not shown)$/ do |show_warning|
1617
+ expect_warning('FC019', :line => nil, :expect_warning => show_warning == 'shown')
1618
+ end
1619
+
1620
+ Then /^the boilerplate metadata warning 008 should warn on lines (.*)$/ do |lines_to_warn|
1621
+ if lines_to_warn.strip == ''
1622
+ expect_no_warning('FC008')
1623
+ else
1624
+ lines_to_warn.split(',').each{|line| expect_warning('FC008', :line => line, :file => 'metadata.rb')}
1625
+ end
1626
+ end
1627
+
1628
+ Then /the build status should be (successful|failed)$/ do |build_outcome|
1629
+ build_outcome == 'successful' ? assert_no_error_occurred : assert_error_occurred
1630
+ end
1631
+
1632
+ Then /^the build will (succeed|fail) with (?:no )?warnings(.*)$/ do |build_outcome, warnings|
1633
+ assert_build_result(build_outcome == 'succeed', warnings.gsub(' ', '').split(','))
1634
+ end
1635
+
1636
+ Then 'the check for server warning 003 should not be displayed against the condition' do
1637
+ expect_warning("FC003", :line => 5, :expect_warning => false)
1638
+ end
1639
+
1640
+ Then /^the check for server warning 003 should not be displayed against the search after the (.*) conditional$/ do |format|
1641
+ line = format == 'oneline' ? 2 : 4
1642
+ expect_warning("FC003", :line => line, :expect_warning => false)
1643
+ end
1644
+
1645
+ Then 'the check for server warning 003 should not be displayed given we have checked' do
1646
+ expect_warning("FC003", :line => 4, :expect_warning => false)
1647
+ end
1648
+
1649
+ Then /^the consider adding platform warning 024 should( not)? be shown$/ do |should_not|
1650
+ expect_warning('FC024', :line => should_not.nil? ? @expected_line : nil, :expect_warning => should_not.nil?)
1651
+ end
1652
+
1653
+ Then /^the conditional block contains only string warning 026 should be (shown|not shown)$/ do |show_warning|
1654
+ expect_warning('FC026', :line => nil, :expect_warning => show_warning == 'shown')
1655
+ end
1656
+
1657
+ Then /^the current version should( not)? be displayed$/ do |no_display|
1658
+ version_str = "foodcritic #{FoodCritic::VERSION}"
1659
+ if no_display.nil?
1660
+ expect_output(version_str)
1661
+ else
1662
+ expect_no_output(version_str)
1663
+ end
1664
+ end
1665
+
1666
+ Then /^the debugger breakpoint warning 030 should be (not )?shown against the (.*)$/ do |should_not, component|
1667
+ filename = case component
1668
+ when 'library' then 'libraries/foo.rb'
1669
+ when 'metadata' then 'metadata.rb'
1670
+ when 'provider' then 'providers/foo.rb'
1671
+ when 'recipe' then 'recipes/default.rb'
1672
+ when 'resource' then 'resources/foo.rb'
1673
+ when 'template' then 'templates/default/foo.erb'
1674
+ end
1675
+ expect_warning('FC030', :line => nil, :expect_warning => should_not.nil?, :file => filename)
1676
+ end
1677
+
1678
+ Then /^the warning (\d+) should be (valid|invalid)$/ do |code, valid|
1679
+ code = "FC#{code}"
1680
+ valid == 'valid' ? expect_no_warning(code) : expect_warning(code)
1681
+ end
1682
+
1683
+ Then /^the incorrect platform usage warning 028 should be (not )?shown$/ do |should_not|
1684
+ expect_warning('FC028', :line => nil, :expect_warning => should_not.nil?)
1685
+ end
1686
+
1687
+ Then /^the line number and line of code that triggered the warning(s)? should be displayed$/ do |multiple|
1688
+ if multiple.nil?
1689
+ expect_line_shown 2, "log node['foo']"
1690
+ else
1691
+ expect_line_shown 4, " node['testing'] = 'bar'"
1692
+ expect_line_shown 5, " node['testing2'] = 'bar2'"
1693
+ end
1694
+ end
1695
+
1696
+ Then 'the missing template warning 033 should not be displayed against the template' do
1697
+ expect_warning('FC033', :line => 3, :expect_warning => false)
1698
+ end
1699
+
1700
+ Then /^the no leading cookbook name warning 029 should be (not )?shown$/ do |should_not|
1701
+ expect_warning('FC029', :line => 1, :expect_warning => should_not.nil?, :file => 'metadata.rb')
1702
+ end
1703
+
1704
+ Then 'the prefer chef_gem to manual install warning 025 should be shown' do
1705
+ expect_warning('FC025', :line => nil)
1706
+ end
1707
+
1708
+ Then 'the recipe filename should be displayed' do
1709
+ expect_output "cookbooks/example/recipes/default.rb"
1710
+ end
1711
+
1712
+ Then /^the resource sets internal attribute warning 027 should be (not )?shown$/ do |should_not|
1713
+ expect_warning('FC027', :line => nil, :expect_warning => should_not.nil?)
1714
+ end
1715
+
1716
+ Then /^the service resource warning 005 should( not)? be visible$/ do |dont_show|
1717
+ expect_warning('FC005', :line => dont_show ? 2 : 7, :expect_warning => ! dont_show)
1718
+ end
1719
+
1720
+ Then /^the service resource warning 005 should( not)? be displayed against the first resource in the block$/ do |dont_show|
1721
+ expect_warning('FC005', :line => 2, :expect_warning => ! dont_show)
1722
+ end
1723
+
1724
+ Then /^the service resource warning 005 should( not)? be shown$/ do |dont_show|
1725
+ expect_warning('FC005', :line => 2, :file_type => :provider,
1726
+ :expect_warning => ! dont_show)
1727
+ end
1728
+
1729
+ Then /^the simple usage text should be displayed along with a (non-)?zero exit code$/ do |non_zero|
1730
+ usage_displayed(non_zero.nil?)
1731
+ end
1732
+
1733
+ Then 'the undeclared dependency warning 007 should be displayed only for the undeclared dependencies' do
1734
+ expect_warning("FC007", :file => 'recipes/default.rb', :line => 1, :expect_warning => false)
1735
+ expect_warning("FC007", :file => 'recipes/default.rb', :line => 2, :expect_warning => false)
1736
+ expect_warning("FC007", :file => 'recipes/default.rb', :line => 6, :expect_warning => true)
1737
+ end
1738
+
1739
+ Then /^the unused template variables warning 034 should (not )?be displayed against the template(.*)?$/ do |not_shown, ext|
1740
+ file = if ext
1741
+ "templates/default/config#{ext.strip}"
1742
+ else
1743
+ 'templates/default/config.conf.erb'
1744
+ end
1745
+ expect_warning('FC034', :file => file, :line => 1,
1746
+ :expect_warning => ! not_shown)
1747
+ end
1748
+
1749
+ Then /^the unrecognised attribute warning 009 should be (true|false)$/ do |shown|
1750
+ shown == 'true' ? expect_warning('FC009') : expect_no_warning('FC009')
1751
+ end
1752
+
1753
+ Then 'the unrecognised attribute warning 009 should be displayed against the correct resource' do
1754
+ expect_warning('FC009', :line => 7)
1755
+ end
1756
+
1757
+ Then 'the usage text should include an option for specifying tags that will fail the build' do
1758
+ expect_usage_option('f', 'epic-fail TAGS',
1759
+ "Fail the build if any of the specified tags are matched ('any' -> fail on any match).")
1760
+ end
1761
+
1762
+ Then /^the warnings shown should be (.*)$/ do |warnings|
1763
+ warnings.split(',').each {|warning| expect_warning(warning, :line => nil)}
1764
+ end
1765
+
1766
+ When /^I check the cookbook specifying a search grammar that (does not exist|is not in treetop format|is a valid treetop grammar)$/ do |search_grammar|
1767
+ case search_grammar
1768
+ when 'is not in treetop format'
1769
+ write_file('search.treetop', 'I am not a valid treetop grammar')
1770
+ when 'is a valid treetop grammar'
1771
+ write_file('search.treetop', IO.read(FoodCritic::Chef::Search.new.chef_search_grammars.first))
1772
+ end
1773
+ run_lint(['--search-grammar', 'search.treetop', 'cookbooks/example'])
1774
+ end
1775
+
1776
+ Then /^the check should abort with an error$/ do
1777
+ assert_error_occurred
1778
+ end
1779
+
1780
+ Given(/^a cookbook with an? (.*) file with an interpolated name$/) do |file_type|
1781
+ content = %q{
1782
+ a = "#{node.hostname}"
1783
+ }
1784
+ write_recipe content if file_type == "recipe"
1785
+ write_attributes content if file_type == "attribute"
1786
+ write_metadata content if file_type == "metadata"
1787
+ write_provider "site", content if file_type == "provider"
1788
+ write_resource "site", content if file_type == "resource"
1789
+ write_definition "apache_site", content if file_type == "definition"
1790
+ write_library "lib", content if file_type == "library"
1791
+ end