foodcritic 7.0.1 → 7.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile +10 -10
- data/Rakefile +21 -21
- data/bin/foodcritic +1 -1
- data/chef_dsl_metadata/chef_12.12.13.json +17809 -0
- data/chef_dsl_metadata/chef_12.13.37.json +18021 -0
- data/features/step_definitions/cookbook_steps.rb +456 -457
- data/features/support/command_helpers.rb +120 -120
- data/features/support/cookbook_helpers.rb +87 -87
- data/features/support/env.rb +6 -6
- data/foodcritic.gemspec +22 -22
- data/lib/foodcritic.rb +20 -20
- data/lib/foodcritic/api.rb +94 -91
- data/lib/foodcritic/chef.rb +14 -14
- data/lib/foodcritic/command_line.rb +35 -35
- data/lib/foodcritic/domain.rb +4 -4
- data/lib/foodcritic/dsl.rb +1 -2
- data/lib/foodcritic/linter.rb +31 -32
- data/lib/foodcritic/notifications.rb +5 -5
- data/lib/foodcritic/output.rb +5 -5
- data/lib/foodcritic/rake_task.rb +7 -7
- data/lib/foodcritic/rules.rb +234 -234
- data/lib/foodcritic/template.rb +1 -1
- data/lib/foodcritic/version.rb +1 -1
- data/lib/foodcritic/xml.rb +5 -5
- data/spec/foodcritic/api_spec.rb +275 -275
- data/spec/foodcritic/chef_spec.rb +11 -11
- data/spec/foodcritic/command_line_spec.rb +7 -7
- data/spec/foodcritic/domain_spec.rb +20 -20
- data/spec/foodcritic/linter_spec.rb +10 -11
- data/spec/foodcritic/template_spec.rb +8 -8
- data/spec/regression/regression_spec.rb +3 -3
- data/spec/regression_helpers.rb +10 -10
- data/spec/spec_helper.rb +6 -6
- metadata +5 -3
data/lib/foodcritic/template.rb
CHANGED
data/lib/foodcritic/version.rb
CHANGED
data/lib/foodcritic/xml.rb
CHANGED
@@ -8,12 +8,12 @@ module FoodCritic
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def xml_create_node(doc, c)
|
11
|
-
Nokogiri::XML::Node.new(c.first.to_s.gsub(/[^a-z_]/,
|
11
|
+
Nokogiri::XML::Node.new(c.first.to_s.gsub(/[^a-z_]/, ""), doc)
|
12
12
|
end
|
13
13
|
|
14
14
|
def xml_document(doc, xml_node)
|
15
15
|
if doc.nil?
|
16
|
-
doc = Nokogiri::XML(
|
16
|
+
doc = Nokogiri::XML("<opt></opt>")
|
17
17
|
xml_node = doc.root
|
18
18
|
end
|
19
19
|
[doc, xml_node]
|
@@ -29,9 +29,9 @@ module FoodCritic
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def xml_position_node(doc, xml_node, child)
|
32
|
-
pos = Nokogiri::XML::Node.new(
|
33
|
-
pos[
|
34
|
-
pos[
|
32
|
+
pos = Nokogiri::XML::Node.new("pos", doc)
|
33
|
+
pos["line"] = child.first.to_s
|
34
|
+
pos["column"] = child[1].to_s
|
35
35
|
xml_node.add_child(pos)
|
36
36
|
end
|
37
37
|
end
|
data/spec/foodcritic/api_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../spec_helper"
|
2
2
|
|
3
3
|
describe FoodCritic::Api do
|
4
4
|
|
@@ -10,7 +10,7 @@ describe FoodCritic::Api do
|
|
10
10
|
|
11
11
|
describe :exposed_api do
|
12
12
|
let(:ignorable_methods) do
|
13
|
-
api.class.ancestors.map{|a| a.public_methods}.flatten.sort.uniq
|
13
|
+
api.class.ancestors.map { |a| a.public_methods }.flatten.sort.uniq
|
14
14
|
end
|
15
15
|
it "exposes the expected api to rule authors" do
|
16
16
|
(api.public_methods.sort - ignorable_methods).must_equal([
|
@@ -49,7 +49,7 @@ describe FoodCritic::Api do
|
|
49
49
|
:template_file,
|
50
50
|
:template_paths,
|
51
51
|
:templates_included,
|
52
|
-
:valid_query
|
52
|
+
:valid_query?,
|
53
53
|
])
|
54
54
|
end
|
55
55
|
end
|
@@ -82,7 +82,7 @@ describe FoodCritic::Api do
|
|
82
82
|
it "returns vivified attributes access" do
|
83
83
|
call = MiniTest::Mock.new
|
84
84
|
call.expect :xpath, [], [/args_add_block/]
|
85
|
-
call.expect :xpath,
|
85
|
+
call.expect :xpath, %w{node bar}, [/ident/]
|
86
86
|
call.expect :xpath, ["foo"], [/@value/]
|
87
87
|
ast.expect :xpath, [call], [String, FoodCritic::Api::AttFilter]
|
88
88
|
api.attribute_access(ast, :type => :vivified).must_equal([call])
|
@@ -100,35 +100,35 @@ describe FoodCritic::Api do
|
|
100
100
|
end
|
101
101
|
it "allows run_state to be ignored" do
|
102
102
|
ast = parse_ast("node.run_state['bar'] = 'baz'")
|
103
|
-
api.attribute_access(ast, :ignore => [
|
103
|
+
api.attribute_access(ast, :ignore => ["run_state"]).must_be_empty
|
104
104
|
end
|
105
105
|
it "allows run_state to be ignored (symbols access)" do
|
106
106
|
ast = parse_ast("node.run_state[:bar] = 'baz'")
|
107
|
-
api.attribute_access(ast, :ignore => [
|
107
|
+
api.attribute_access(ast, :ignore => ["run_state"]).must_be_empty
|
108
108
|
end
|
109
109
|
it "allows any attribute to be ignored" do
|
110
110
|
ast = parse_ast("node['bar'] = 'baz'")
|
111
|
-
api.attribute_access(ast, :ignore => [
|
111
|
+
api.attribute_access(ast, :ignore => ["bar"]).must_be_empty
|
112
112
|
end
|
113
113
|
it "allows any attribute to be ignored (symbols access)" do
|
114
114
|
ast = parse_ast("node[:bar] = 'baz'")
|
115
|
-
api.attribute_access(ast, :ignore => [
|
115
|
+
api.attribute_access(ast, :ignore => ["bar"]).must_be_empty
|
116
116
|
end
|
117
117
|
it "allows any attribute to be ignored (dot access)" do
|
118
118
|
ast = parse_ast("node.bar = 'baz'")
|
119
|
-
api.attribute_access(ast, :ignore => [
|
119
|
+
api.attribute_access(ast, :ignore => ["bar"]).must_be_empty
|
120
120
|
end
|
121
121
|
it "includes the children of attributes" do
|
122
122
|
ast = parse_ast("node['foo']['bar'] = 'baz'")
|
123
|
-
api.attribute_access(ast).map{|a| a[
|
123
|
+
api.attribute_access(ast).map { |a| a["value"] }.must_equal(%w{foo bar})
|
124
124
|
end
|
125
125
|
it "does not include children of removed attributes" do
|
126
126
|
ast = parse_ast("node['foo']['bar'] = 'baz'")
|
127
|
-
api.attribute_access(ast, :ignore => [
|
127
|
+
api.attribute_access(ast, :ignore => ["foo"]).must_be_empty
|
128
128
|
end
|
129
129
|
it "coerces ignore values to enumerate them" do
|
130
130
|
ast = parse_ast("node.run_state['bar'] = 'baz'")
|
131
|
-
api.attribute_access(ast, :ignore =>
|
131
|
+
api.attribute_access(ast, :ignore => "run_state").must_be_empty
|
132
132
|
end
|
133
133
|
it "can ignore multiple attributes" do
|
134
134
|
ast = parse_ast(%q{
|
@@ -143,7 +143,7 @@ describe FoodCritic::Api do
|
|
143
143
|
describe "#checks_for_chef_solo?" do
|
144
144
|
let(:ast) { MiniTest::Mock.new }
|
145
145
|
it "raises if the provided ast does not support XPath" do
|
146
|
-
lambda{api.checks_for_chef_solo?(nil)}.must_raise(ArgumentError)
|
146
|
+
lambda { api.checks_for_chef_solo?(nil) }.must_raise(ArgumentError)
|
147
147
|
end
|
148
148
|
it "returns false if there is no reference to chef solo" do
|
149
149
|
ast.expect :xpath, [], [String]
|
@@ -151,11 +151,11 @@ describe FoodCritic::Api do
|
|
151
151
|
refute api.checks_for_chef_solo?(ast)
|
152
152
|
end
|
153
153
|
it "returns true if there is one reference to chef solo" do
|
154
|
-
ast.expect :xpath, [
|
154
|
+
ast.expect :xpath, ["aref"], [String]
|
155
155
|
assert api.checks_for_chef_solo?(ast)
|
156
156
|
end
|
157
157
|
it "returns true if there are multiple references to chef solo" do
|
158
|
-
ast.expect :xpath,
|
158
|
+
ast.expect :xpath, %w{aref aref}, [String]
|
159
159
|
assert api.checks_for_chef_solo?(ast)
|
160
160
|
end
|
161
161
|
end
|
@@ -165,7 +165,7 @@ describe FoodCritic::Api do
|
|
165
165
|
refute api.chef_solo_search_supported?(nil)
|
166
166
|
end
|
167
167
|
it "returns false if the recipe path does not exist" do
|
168
|
-
refute api.chef_solo_search_supported?(
|
168
|
+
refute api.chef_solo_search_supported?("/tmp/non-existent-path")
|
169
169
|
end
|
170
170
|
end
|
171
171
|
|
@@ -173,32 +173,32 @@ describe FoodCritic::Api do
|
|
173
173
|
def mock_cookbook_metadata(f)
|
174
174
|
dir = File.dirname(f)
|
175
175
|
unless File.directory?(dir)
|
176
|
-
|
176
|
+
FileUtils.mkdir_p(dir)
|
177
177
|
end
|
178
|
-
File.open(f,
|
178
|
+
File.open(f, "w") { |file| file.write('name "YOUR_COOKBOOK_NAME"') }
|
179
179
|
end
|
180
180
|
|
181
|
-
metadata_path=
|
181
|
+
metadata_path = "/tmp/fc/mock/cb/metadata.rb"
|
182
182
|
it "raises if passed a nil" do
|
183
|
-
lambda{api.cookbook_name(nil)}.must_raise ArgumentError
|
183
|
+
lambda { api.cookbook_name(nil) }.must_raise ArgumentError
|
184
184
|
end
|
185
185
|
it "raises if passed an empty string" do
|
186
|
-
lambda{api.cookbook_name(
|
186
|
+
lambda { api.cookbook_name("") }.must_raise ArgumentError
|
187
187
|
end
|
188
188
|
it "returns the cookbook name when passed a recipe" do
|
189
|
-
recipe_path =
|
190
|
-
api.cookbook_name(recipe_path).must_equal
|
189
|
+
recipe_path = "cookbooks/apache2/recipes/default.rb"
|
190
|
+
api.cookbook_name(recipe_path).must_equal "apache2"
|
191
191
|
end
|
192
192
|
it "returns the cookbook name when passed the cookbook metadata" do
|
193
|
-
api.cookbook_name(
|
193
|
+
api.cookbook_name("cookbooks/apache2/metadata.rb").must_equal "apache2"
|
194
194
|
end
|
195
195
|
it "returns the cookbook name when passed a template" do
|
196
|
-
erb_path =
|
197
|
-
api.cookbook_name(erb_path).must_equal
|
196
|
+
erb_path = "cookbooks/apache2/templates/default/a2ensite.erb"
|
197
|
+
api.cookbook_name(erb_path).must_equal "apache2"
|
198
198
|
end
|
199
199
|
it "returns the cookbook name when passed the cookbook metadata with a name field" do
|
200
200
|
mock_cookbook_metadata(metadata_path)
|
201
|
-
api.cookbook_name(metadata_path).must_equal
|
201
|
+
api.cookbook_name(metadata_path).must_equal "YOUR_COOKBOOK_NAME"
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
@@ -206,32 +206,32 @@ describe FoodCritic::Api do
|
|
206
206
|
def mock_cookbook_metadata(f)
|
207
207
|
dir = File.dirname(f)
|
208
208
|
unless File.directory?(dir)
|
209
|
-
|
209
|
+
FileUtils.mkdir_p(dir)
|
210
210
|
end
|
211
|
-
File.open(f,
|
211
|
+
File.open(f, "w") { |file| file.write('maintainer "YOUR_COMPANY_NAME"') }
|
212
212
|
end
|
213
213
|
|
214
|
-
metadata_path=
|
214
|
+
metadata_path = "/tmp/fc/mock/cb/metadata.rb"
|
215
215
|
it "raises if passed a nil" do
|
216
|
-
lambda{api.cookbook_maintainer(nil)}.must_raise ArgumentError
|
216
|
+
lambda { api.cookbook_maintainer(nil) }.must_raise ArgumentError
|
217
217
|
end
|
218
218
|
it "raises if passed an empty string" do
|
219
|
-
lambda{api.cookbook_maintainer(
|
219
|
+
lambda { api.cookbook_maintainer("") }.must_raise ArgumentError
|
220
220
|
end
|
221
221
|
it "raises if the path does not exist" do
|
222
|
-
lambda{api.cookbook_maintainer(
|
222
|
+
lambda { api.cookbook_maintainer("/tmp/non-existent-path") }.must_raise RuntimeError
|
223
223
|
end
|
224
224
|
it "returns the cookbook maintainer when passed the cookbook metadata" do
|
225
225
|
mock_cookbook_metadata(metadata_path)
|
226
|
-
api.cookbook_maintainer(metadata_path).must_equal
|
226
|
+
api.cookbook_maintainer(metadata_path).must_equal "YOUR_COMPANY_NAME"
|
227
227
|
end
|
228
228
|
it "returns the cookbook maintainer when passed a recipe" do
|
229
229
|
mock_cookbook_metadata(metadata_path)
|
230
|
-
api.cookbook_maintainer(
|
230
|
+
api.cookbook_maintainer("/tmp/fc/mock/cb/recipes/default.rb").must_equal "YOUR_COMPANY_NAME"
|
231
231
|
end
|
232
232
|
it "returns the cookbook maintainer when passed a template" do
|
233
233
|
mock_cookbook_metadata(metadata_path)
|
234
|
-
api.cookbook_maintainer(
|
234
|
+
api.cookbook_maintainer("/tmp/fc/mock/cb/templates/default/mock.erb").must_equal "YOUR_COMPANY_NAME"
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
@@ -239,38 +239,38 @@ describe FoodCritic::Api do
|
|
239
239
|
def mock_cookbook_metadata(f)
|
240
240
|
dir = File.dirname(f)
|
241
241
|
unless File.directory?(dir)
|
242
|
-
|
242
|
+
FileUtils.mkdir_p(dir)
|
243
243
|
end
|
244
|
-
File.open(f,
|
244
|
+
File.open(f, "w") { |file| file.write('maintainer_email "YOUR_EMAIL"') }
|
245
245
|
end
|
246
246
|
|
247
|
-
metadata_path=
|
247
|
+
metadata_path = "/tmp/fc/mock/cb/metadata.rb"
|
248
248
|
it "raises if passed a nil" do
|
249
|
-
lambda{api.cookbook_maintainer_email(nil)}.must_raise ArgumentError
|
249
|
+
lambda { api.cookbook_maintainer_email(nil) }.must_raise ArgumentError
|
250
250
|
end
|
251
251
|
it "raises if passed an empty string" do
|
252
|
-
lambda{api.cookbook_maintainer_email(
|
252
|
+
lambda { api.cookbook_maintainer_email("") }.must_raise ArgumentError
|
253
253
|
end
|
254
254
|
it "raises if the path does not exist" do
|
255
|
-
lambda{api.cookbook_maintainer_email(
|
255
|
+
lambda { api.cookbook_maintainer_email("/tmp/non-existent-path") }.must_raise RuntimeError
|
256
256
|
end
|
257
257
|
it "returns the cookbook maintainer_email when passed the cookbook metadata" do
|
258
258
|
mock_cookbook_metadata(metadata_path)
|
259
|
-
api.cookbook_maintainer_email(metadata_path).must_equal
|
259
|
+
api.cookbook_maintainer_email(metadata_path).must_equal "YOUR_EMAIL"
|
260
260
|
end
|
261
261
|
it "returns the cookbook maintainer_email when passed a recipe" do
|
262
262
|
mock_cookbook_metadata(metadata_path)
|
263
|
-
api.cookbook_maintainer_email(
|
263
|
+
api.cookbook_maintainer_email("/tmp/fc/mock/cb/recipes/default.rb").must_equal "YOUR_EMAIL"
|
264
264
|
end
|
265
265
|
it "returns the cookbook maintainer_email when passed a template" do
|
266
266
|
mock_cookbook_metadata(metadata_path)
|
267
|
-
api.cookbook_maintainer_email(
|
267
|
+
api.cookbook_maintainer_email("/tmp/fc/mock/cb/templates/default/mock.erb").must_equal "YOUR_EMAIL"
|
268
268
|
end
|
269
269
|
end
|
270
270
|
|
271
271
|
describe "#declared_dependencies" do
|
272
272
|
it "raises if the ast does not support XPath" do
|
273
|
-
lambda{api.declared_dependencies(nil)}.must_raise ArgumentError
|
273
|
+
lambda { api.declared_dependencies(nil) }.must_raise ArgumentError
|
274
274
|
end
|
275
275
|
it "returns an empty if there are no declared dependencies" do
|
276
276
|
ast = MiniTest::Mock.new
|
@@ -310,24 +310,24 @@ describe FoodCritic::Api do
|
|
310
310
|
</args_add_block>
|
311
311
|
</command>
|
312
312
|
})
|
313
|
-
api.declared_dependencies(ast).must_equal [
|
313
|
+
api.declared_dependencies(ast).must_equal ["mysql"]
|
314
314
|
end
|
315
315
|
end
|
316
316
|
|
317
317
|
describe "#field" do
|
318
318
|
describe :simple_ast do
|
319
|
-
let(:ast){ parse_ast('name "webserver"') }
|
319
|
+
let(:ast) { parse_ast('name "webserver"') }
|
320
320
|
it "raises if the field name is nil" do
|
321
|
-
lambda{api.field(ast, nil)}.must_raise ArgumentError
|
321
|
+
lambda { api.field(ast, nil) }.must_raise ArgumentError
|
322
322
|
end
|
323
323
|
it "raises if the field name is empty" do
|
324
|
-
lambda{api.field(ast,
|
324
|
+
lambda { api.field(ast, "") }.must_raise ArgumentError
|
325
325
|
end
|
326
326
|
it "returns empty if the field is not present" do
|
327
327
|
api.field(ast, :common_name).must_be_empty
|
328
328
|
end
|
329
329
|
it "accepts a string for the field name" do
|
330
|
-
api.field(ast,
|
330
|
+
api.field(ast, "name").wont_be_empty
|
331
331
|
end
|
332
332
|
it "accepts a symbol for the field name" do
|
333
333
|
api.field(ast, :name).wont_be_empty
|
@@ -356,21 +356,21 @@ describe FoodCritic::Api do
|
|
356
356
|
|
357
357
|
describe "#field_value" do
|
358
358
|
describe :simple_ast do
|
359
|
-
let(:ast){ parse_ast('name "webserver"') }
|
359
|
+
let(:ast) { parse_ast('name "webserver"') }
|
360
360
|
it "raises if the field name is nil" do
|
361
|
-
lambda{api.field_value(ast, nil)}.must_raise ArgumentError
|
361
|
+
lambda { api.field_value(ast, nil) }.must_raise ArgumentError
|
362
362
|
end
|
363
363
|
it "raises if the field name is empty" do
|
364
|
-
lambda{api.field_value(ast,
|
364
|
+
lambda { api.field_value(ast, "") }.must_raise ArgumentError
|
365
365
|
end
|
366
366
|
it "is falsy if the field is not present" do
|
367
367
|
refute api.field_value(ast, :common_name)
|
368
368
|
end
|
369
369
|
it "accepts a string for the field name" do
|
370
|
-
api.field_value(ast,
|
370
|
+
api.field_value(ast, "name").must_equal "webserver"
|
371
371
|
end
|
372
372
|
it "accepts a symbol for the field name" do
|
373
|
-
api.field_value(ast, :name).must_equal
|
373
|
+
api.field_value(ast, :name).must_equal "webserver"
|
374
374
|
end
|
375
375
|
end
|
376
376
|
it "is falsy when the value is an embedded string expression" do
|
@@ -390,7 +390,7 @@ describe FoodCritic::Api do
|
|
390
390
|
name "webserver"
|
391
391
|
name "database"
|
392
392
|
}.strip)
|
393
|
-
api.field_value(ast, :name).must_equal
|
393
|
+
api.field_value(ast, :name).must_equal "database"
|
394
394
|
end
|
395
395
|
end
|
396
396
|
|
@@ -402,7 +402,7 @@ describe FoodCritic::Api do
|
|
402
402
|
api.file_match("foo/bar/foo.rb")[:filename].must_equal "foo/bar/foo.rb"
|
403
403
|
end
|
404
404
|
it "raises an error if the provided filename is nil" do
|
405
|
-
lambda{api.file_match(nil)}.must_raise(ArgumentError)
|
405
|
+
lambda { api.file_match(nil) }.must_raise(ArgumentError)
|
406
406
|
end
|
407
407
|
it "sets the line and column to the beginning of the file" do
|
408
408
|
match = api.file_match("bar.rb")
|
@@ -417,49 +417,49 @@ describe FoodCritic::Api do
|
|
417
417
|
api.find_resources(nil).must_be_empty
|
418
418
|
end
|
419
419
|
it "restricts by resource type when provided" do
|
420
|
-
ast.expect :xpath, [
|
420
|
+
ast.expect :xpath, ["method_add_block"],
|
421
421
|
["//method_add_block[command/ident[@value='file']]" +
|
422
|
-
|
423
|
-
api.find_resources(ast, :type =>
|
422
|
+
"[command/ident/@value != 'action']"]
|
423
|
+
api.find_resources(ast, :type => "file")
|
424
424
|
ast.verify
|
425
425
|
end
|
426
426
|
it "does not restrict by resource type when not provided" do
|
427
|
-
ast.expect :xpath, [
|
427
|
+
ast.expect :xpath, ["method_add_block"],
|
428
428
|
["//method_add_block[command/ident]" +
|
429
|
-
|
429
|
+
"[command/ident/@value != 'action']"]
|
430
430
|
api.find_resources(ast)
|
431
431
|
ast.verify
|
432
432
|
end
|
433
433
|
it "allows resource type to be specified as :any" do
|
434
|
-
ast.expect :xpath, [
|
434
|
+
ast.expect :xpath, ["method_add_block"],
|
435
435
|
["//method_add_block[command/ident]" +
|
436
|
-
|
436
|
+
"[command/ident/@value != 'action']"]
|
437
437
|
api.find_resources(ast, :type => :any)
|
438
438
|
ast.verify
|
439
439
|
end
|
440
440
|
it "returns any matches" do
|
441
|
-
ast.expect :xpath, [
|
442
|
-
api.find_resources(ast).must_equal [
|
441
|
+
ast.expect :xpath, ["method_add_block"], [String]
|
442
|
+
api.find_resources(ast).must_equal ["method_add_block"]
|
443
443
|
end
|
444
444
|
end
|
445
445
|
|
446
446
|
describe "#included_recipes" do
|
447
447
|
it "raises if the ast does not support XPath" do
|
448
|
-
lambda{api.included_recipes(nil)}.must_raise ArgumentError
|
448
|
+
lambda { api.included_recipes(nil) }.must_raise ArgumentError
|
449
449
|
end
|
450
450
|
it "returns an empty hash if there are no included recipes" do
|
451
451
|
ast = MiniTest::Mock.new.expect :xpath, [], [String]
|
452
452
|
api.included_recipes(ast).keys.must_be_empty
|
453
453
|
end
|
454
454
|
it "returns a hash keyed by recipe name" do
|
455
|
-
ast = MiniTest::Mock.new.expect :xpath, [{
|
455
|
+
ast = MiniTest::Mock.new.expect :xpath, [{ "value" => "foo::bar" }],
|
456
456
|
[String]
|
457
|
-
api.included_recipes(ast).keys.must_equal [
|
457
|
+
api.included_recipes(ast).keys.must_equal ["foo::bar"]
|
458
458
|
end
|
459
459
|
it "returns a hash where the values are the matching nodes" do
|
460
|
-
ast = MiniTest::Mock.new.expect :xpath, [{
|
460
|
+
ast = MiniTest::Mock.new.expect :xpath, [{ "value" => "foo::bar" }],
|
461
461
|
[String]
|
462
|
-
api.included_recipes(ast).values.must_equal [[{
|
462
|
+
api.included_recipes(ast).values.must_equal [[{ "value" => "foo::bar" }]]
|
463
463
|
end
|
464
464
|
it "correctly keys an included recipe specified as a string literal" do
|
465
465
|
api.included_recipes(parse_ast(%q{
|
@@ -476,7 +476,7 @@ describe FoodCritic::Api do
|
|
476
476
|
api.included_recipes(ast).keys.must_equal ["foo::"]
|
477
477
|
end
|
478
478
|
it "returns the literal string part of the AST" do
|
479
|
-
api.included_recipes(ast)[
|
479
|
+
api.included_recipes(ast)["foo::"].first.must_respond_to(:xpath)
|
480
480
|
end
|
481
481
|
it "returns empty if asked to exclude statements with embedded expressions" do
|
482
482
|
api.included_recipes(ast, :with_partial_names => false).must_be_empty
|
@@ -495,7 +495,7 @@ describe FoodCritic::Api do
|
|
495
495
|
api.included_recipes(ast).keys.must_equal ["::bar"]
|
496
496
|
end
|
497
497
|
it "returns the literal string part of the AST" do
|
498
|
-
api.included_recipes(ast)[
|
498
|
+
api.included_recipes(ast)["::bar"].first.must_respond_to(:xpath)
|
499
499
|
end
|
500
500
|
it "returns empty if asked to exclude statements with embedded expressions" do
|
501
501
|
api.included_recipes(ast, :with_partial_names => false).must_be_empty
|
@@ -511,7 +511,7 @@ describe FoodCritic::Api do
|
|
511
511
|
api.included_recipes(ast).keys.must_equal ["_foo::bar"]
|
512
512
|
end
|
513
513
|
it "returns the literal string part of the AST" do
|
514
|
-
api.included_recipes(ast)[
|
514
|
+
api.included_recipes(ast)["_foo::bar"].first.must_respond_to(:xpath)
|
515
515
|
end
|
516
516
|
it "returns empty if asked to exclude statements with embedded expressions" do
|
517
517
|
api.included_recipes(ast, :with_partial_names => false).must_be_empty
|
@@ -550,29 +550,29 @@ describe FoodCritic::Api do
|
|
550
550
|
api.literal_searches(ast).must_be_empty
|
551
551
|
end
|
552
552
|
it "returns the AST elements for each literal search" do
|
553
|
-
ast.expect :xpath, [
|
554
|
-
api.literal_searches(ast).must_equal [
|
553
|
+
ast.expect :xpath, ["tstring_content"], [String]
|
554
|
+
api.literal_searches(ast).must_equal ["tstring_content"]
|
555
555
|
end
|
556
556
|
end
|
557
557
|
|
558
558
|
describe "#match" do
|
559
559
|
it "raises if the provided node is nil" do
|
560
|
-
lambda{api.match(nil)}.must_raise(ArgumentError)
|
560
|
+
lambda { api.match(nil) }.must_raise(ArgumentError)
|
561
561
|
end
|
562
562
|
it "raises if the provided node does not support XPath" do
|
563
|
-
lambda{api.match(Object.new)}.must_raise(ArgumentError)
|
563
|
+
lambda { api.match(Object.new) }.must_raise(ArgumentError)
|
564
564
|
end
|
565
565
|
it "returns nil if there is no nested position node" do
|
566
566
|
node = MiniTest::Mock.new
|
567
|
-
node.expect :xpath, [], [
|
567
|
+
node.expect :xpath, [], ["descendant::pos"]
|
568
568
|
api.match(node).must_be_nil
|
569
569
|
end
|
570
570
|
it "uses the position of the first position node if there are multiple" do
|
571
571
|
node = MiniTest::Mock.new
|
572
572
|
node.expect(:xpath,
|
573
|
-
[{
|
574
|
-
{
|
575
|
-
[
|
573
|
+
[{ "name" => "pos", "line" => "1", "column" => "10" },
|
574
|
+
{ "name" => "pos", "line" => "3", "column" => "16" }],
|
575
|
+
["descendant::pos"])
|
576
576
|
match = api.match(node)
|
577
577
|
match[:line].must_equal(1)
|
578
578
|
match[:column].must_equal(10)
|
@@ -580,17 +580,17 @@ describe FoodCritic::Api do
|
|
580
580
|
describe :matched_node_name do
|
581
581
|
let(:node) do
|
582
582
|
node = MiniTest::Mock.new
|
583
|
-
node.expect :xpath, [{
|
584
|
-
|
583
|
+
node.expect :xpath, [{ "name" => "pos", "line" => "1",
|
584
|
+
"column" => "10" }], ["descendant::pos"]
|
585
585
|
node
|
586
586
|
end
|
587
587
|
it "includes the name of the node in the match" do
|
588
|
-
node.expect :name,
|
589
|
-
api.match(node).must_equal({:matched =>
|
590
|
-
|
588
|
+
node.expect :name, "command"
|
589
|
+
api.match(node).must_equal({ :matched => "command", :line => 1,
|
590
|
+
:column => 10 })
|
591
591
|
end
|
592
592
|
it "sets the matched name to empty if the element does not have a name" do
|
593
|
-
api.match(node).must_equal({:matched =>
|
593
|
+
api.match(node).must_equal({ :matched => "", :line => 1, :column => 10 })
|
594
594
|
end
|
595
595
|
end
|
596
596
|
end
|
@@ -809,9 +809,9 @@ describe FoodCritic::Api do
|
|
809
809
|
:type => :notifies,
|
810
810
|
:action => :restart,
|
811
811
|
:resource_type => :service,
|
812
|
-
:resource_name =>
|
812
|
+
:resource_name => "nscd",
|
813
813
|
:timing => :delayed,
|
814
|
-
:style => :old
|
814
|
+
:style => :old,
|
815
815
|
}]
|
816
816
|
)
|
817
817
|
end
|
@@ -828,9 +828,9 @@ describe FoodCritic::Api do
|
|
828
828
|
:type => :notifies,
|
829
829
|
:action => :'soft-restart',
|
830
830
|
:resource_type => :service,
|
831
|
-
:resource_name =>
|
831
|
+
:resource_name => "nscd",
|
832
832
|
:timing => :delayed,
|
833
|
-
:style => :old
|
833
|
+
:style => :old,
|
834
834
|
}]
|
835
835
|
)
|
836
836
|
end
|
@@ -847,9 +847,9 @@ describe FoodCritic::Api do
|
|
847
847
|
:type => :notifies,
|
848
848
|
:action => :restart,
|
849
849
|
:resource_type => :service,
|
850
|
-
:resource_name =>
|
850
|
+
:resource_name => "nscd",
|
851
851
|
:timing => :delayed,
|
852
|
-
:style => :old
|
852
|
+
:style => :old,
|
853
853
|
}]
|
854
854
|
)
|
855
855
|
end
|
@@ -866,9 +866,9 @@ describe FoodCritic::Api do
|
|
866
866
|
:type => :subscribes,
|
867
867
|
:action => :restart,
|
868
868
|
:resource_type => :service,
|
869
|
-
:resource_name =>
|
869
|
+
:resource_name => "nscd",
|
870
870
|
:timing => :delayed,
|
871
|
-
:style => :old
|
871
|
+
:style => :old,
|
872
872
|
}]
|
873
873
|
)
|
874
874
|
end
|
@@ -885,9 +885,9 @@ describe FoodCritic::Api do
|
|
885
885
|
:type => :subscribes,
|
886
886
|
:action => :restart,
|
887
887
|
:resource_type => :service,
|
888
|
-
:resource_name =>
|
888
|
+
:resource_name => "nscd",
|
889
889
|
:timing => :delayed,
|
890
|
-
:style => :old
|
890
|
+
:style => :old,
|
891
891
|
}]
|
892
892
|
)
|
893
893
|
end
|
@@ -904,9 +904,9 @@ describe FoodCritic::Api do
|
|
904
904
|
:type => :notifies,
|
905
905
|
:action => :restart,
|
906
906
|
:resource_type => :service,
|
907
|
-
:resource_name =>
|
907
|
+
:resource_name => "nscd",
|
908
908
|
:timing => :delayed,
|
909
|
-
:style => :new
|
909
|
+
:style => :new,
|
910
910
|
}]
|
911
911
|
)
|
912
912
|
end
|
@@ -923,9 +923,9 @@ describe FoodCritic::Api do
|
|
923
923
|
:type => :notifies,
|
924
924
|
:action => :'soft-restart',
|
925
925
|
:resource_type => :service,
|
926
|
-
:resource_name =>
|
926
|
+
:resource_name => "nscd",
|
927
927
|
:timing => :delayed,
|
928
|
-
:style => :new
|
928
|
+
:style => :new,
|
929
929
|
}]
|
930
930
|
)
|
931
931
|
end
|
@@ -942,9 +942,9 @@ describe FoodCritic::Api do
|
|
942
942
|
:type => :notifies,
|
943
943
|
:action => :restart,
|
944
944
|
:resource_type => :service,
|
945
|
-
:resource_name =>
|
945
|
+
:resource_name => "nscd",
|
946
946
|
:timing => :delayed,
|
947
|
-
:style => :new
|
947
|
+
:style => :new,
|
948
948
|
}]
|
949
949
|
)
|
950
950
|
end
|
@@ -961,9 +961,9 @@ describe FoodCritic::Api do
|
|
961
961
|
:type => :subscribes,
|
962
962
|
:action => :restart,
|
963
963
|
:resource_type => :service,
|
964
|
-
:resource_name =>
|
964
|
+
:resource_name => "nscd",
|
965
965
|
:timing => :delayed,
|
966
|
-
:style => :new
|
966
|
+
:style => :new,
|
967
967
|
}]
|
968
968
|
)
|
969
969
|
end
|
@@ -980,9 +980,9 @@ describe FoodCritic::Api do
|
|
980
980
|
:type => :subscribes,
|
981
981
|
:action => :restart,
|
982
982
|
:resource_type => :service,
|
983
|
-
:resource_name =>
|
983
|
+
:resource_name => "nscd",
|
984
984
|
:timing => :delayed,
|
985
|
-
:style => :new
|
985
|
+
:style => :new,
|
986
986
|
}]
|
987
987
|
)
|
988
988
|
end
|
@@ -1001,18 +1001,18 @@ describe FoodCritic::Api do
|
|
1001
1001
|
:type => :notifies,
|
1002
1002
|
:action => :restart,
|
1003
1003
|
:resource_type => :service,
|
1004
|
-
:resource_name =>
|
1004
|
+
:resource_name => "nscd",
|
1005
1005
|
:timing => :delayed,
|
1006
|
-
:style => :old
|
1006
|
+
:style => :old,
|
1007
1007
|
},
|
1008
1008
|
{
|
1009
1009
|
:type => :subscribes,
|
1010
1010
|
:action => :create,
|
1011
1011
|
:resource_type => :template,
|
1012
|
-
:resource_name =>
|
1012
|
+
:resource_name => "/etc/nscd.conf",
|
1013
1013
|
:timing => :delayed,
|
1014
|
-
:style => :old
|
1015
|
-
}
|
1014
|
+
:style => :old,
|
1015
|
+
},
|
1016
1016
|
])
|
1017
1017
|
end
|
1018
1018
|
it "new-style notifications" do
|
@@ -1029,18 +1029,18 @@ describe FoodCritic::Api do
|
|
1029
1029
|
:type => :notifies,
|
1030
1030
|
:action => :restart,
|
1031
1031
|
:resource_type => :service,
|
1032
|
-
:resource_name =>
|
1032
|
+
:resource_name => "nscd",
|
1033
1033
|
:timing => :delayed,
|
1034
|
-
:style => :new
|
1034
|
+
:style => :new,
|
1035
1035
|
},
|
1036
1036
|
{
|
1037
1037
|
:type => :subscribes,
|
1038
1038
|
:action => :create,
|
1039
1039
|
:resource_type => :template,
|
1040
|
-
:resource_name =>
|
1040
|
+
:resource_name => "/etc/nscd.conf",
|
1041
1041
|
:timing => :delayed,
|
1042
|
-
:style => :new
|
1043
|
-
}
|
1042
|
+
:style => :new,
|
1043
|
+
},
|
1044
1044
|
])
|
1045
1045
|
end
|
1046
1046
|
end
|
@@ -1057,9 +1057,9 @@ describe FoodCritic::Api do
|
|
1057
1057
|
:type => :notifies,
|
1058
1058
|
:action => :restart,
|
1059
1059
|
:resource_type => :service,
|
1060
|
-
:resource_name =>
|
1060
|
+
:resource_name => "nscd",
|
1061
1061
|
:timing => :immediate,
|
1062
|
-
:style => :old
|
1062
|
+
:style => :old,
|
1063
1063
|
}]
|
1064
1064
|
)
|
1065
1065
|
end
|
@@ -1076,9 +1076,9 @@ describe FoodCritic::Api do
|
|
1076
1076
|
:type => :subscribes,
|
1077
1077
|
:action => :restart,
|
1078
1078
|
:resource_type => :service,
|
1079
|
-
:resource_name =>
|
1079
|
+
:resource_name => "nscd",
|
1080
1080
|
:timing => :immediate,
|
1081
|
-
:style => :old
|
1081
|
+
:style => :old,
|
1082
1082
|
}]
|
1083
1083
|
)
|
1084
1084
|
end
|
@@ -1095,9 +1095,9 @@ describe FoodCritic::Api do
|
|
1095
1095
|
:type => :notifies,
|
1096
1096
|
:action => :restart,
|
1097
1097
|
:resource_type => :service,
|
1098
|
-
:resource_name =>
|
1098
|
+
:resource_name => "nscd",
|
1099
1099
|
:timing => :immediate,
|
1100
|
-
:style => :new
|
1100
|
+
:style => :new,
|
1101
1101
|
}]
|
1102
1102
|
)
|
1103
1103
|
end
|
@@ -1114,9 +1114,9 @@ describe FoodCritic::Api do
|
|
1114
1114
|
:type => :subscribes,
|
1115
1115
|
:action => :restart,
|
1116
1116
|
:resource_type => :service,
|
1117
|
-
:resource_name =>
|
1117
|
+
:resource_name => "nscd",
|
1118
1118
|
:timing => :immediate,
|
1119
|
-
:style => :new
|
1119
|
+
:style => :new,
|
1120
1120
|
}]
|
1121
1121
|
)
|
1122
1122
|
end
|
@@ -1133,9 +1133,9 @@ describe FoodCritic::Api do
|
|
1133
1133
|
notifies :restart, resources(:service => "nscd")
|
1134
1134
|
end
|
1135
1135
|
}), :type => :template).first).must_equal([
|
1136
|
-
{:type => :notifies, :action => :restart, :resource_type => :service,
|
1137
|
-
|
1138
|
-
|
1136
|
+
{ :type => :notifies, :action => :restart, :resource_type => :service,
|
1137
|
+
:resource_name => "nscd", :timing => :delayed,
|
1138
|
+
:style => :old },
|
1139
1139
|
])
|
1140
1140
|
end
|
1141
1141
|
it "old-style subscriptions" do
|
@@ -1150,9 +1150,9 @@ describe FoodCritic::Api do
|
|
1150
1150
|
subscribes :restart, resources(:service => "nscd")
|
1151
1151
|
end
|
1152
1152
|
}), :type => :template).first).must_equal([
|
1153
|
-
{:type => :subscribes, :action => :restart, :resource_type => :service,
|
1154
|
-
|
1155
|
-
|
1153
|
+
{ :type => :subscribes, :action => :restart, :resource_type => :service,
|
1154
|
+
:resource_name => "nscd", :timing => :delayed,
|
1155
|
+
:style => :old },
|
1156
1156
|
])
|
1157
1157
|
end
|
1158
1158
|
it "new-style notifications" do
|
@@ -1167,9 +1167,9 @@ describe FoodCritic::Api do
|
|
1167
1167
|
notifies :restart, "service[nscd]"
|
1168
1168
|
end
|
1169
1169
|
}), :type => :template).first).must_equal([
|
1170
|
-
{:type => :notifies, :action => :restart, :resource_type => :service,
|
1171
|
-
|
1172
|
-
|
1170
|
+
{ :type => :notifies, :action => :restart, :resource_type => :service,
|
1171
|
+
:resource_name => "nscd", :timing => :delayed,
|
1172
|
+
:style => :new },
|
1173
1173
|
])
|
1174
1174
|
end
|
1175
1175
|
it "new-style subscriptions" do
|
@@ -1184,9 +1184,9 @@ describe FoodCritic::Api do
|
|
1184
1184
|
subscribes :restart, "service[nscd]"
|
1185
1185
|
end
|
1186
1186
|
}), :type => :template).first).must_equal([
|
1187
|
-
{:type => :subscribes, :action => :restart, :resource_type => :service,
|
1188
|
-
|
1189
|
-
|
1187
|
+
{ :type => :subscribes, :action => :restart, :resource_type => :service,
|
1188
|
+
:resource_name => "nscd", :timing => :delayed,
|
1189
|
+
:style => :new },
|
1190
1190
|
])
|
1191
1191
|
end
|
1192
1192
|
end
|
@@ -1202,12 +1202,12 @@ describe FoodCritic::Api do
|
|
1202
1202
|
end
|
1203
1203
|
})).must_equal(
|
1204
1204
|
[
|
1205
|
-
{:type => :notifies, :action => :stop, :resource_type => :service,
|
1206
|
-
|
1207
|
-
|
1208
|
-
{:type => :notifies, :action => :start, :resource_type => :service,
|
1209
|
-
|
1210
|
-
|
1205
|
+
{ :type => :notifies, :action => :stop, :resource_type => :service,
|
1206
|
+
:resource_name => "nscd", :timing => :delayed,
|
1207
|
+
:style => :old },
|
1208
|
+
{ :type => :notifies, :action => :start, :resource_type => :service,
|
1209
|
+
:resource_name => "nscd", :timing => :delayed,
|
1210
|
+
:style => :old },
|
1211
1211
|
]
|
1212
1212
|
)
|
1213
1213
|
end
|
@@ -1222,12 +1222,12 @@ describe FoodCritic::Api do
|
|
1222
1222
|
end
|
1223
1223
|
})).must_equal(
|
1224
1224
|
[
|
1225
|
-
{:type => :subscribes, :action => :stop, :resource_type => :service,
|
1226
|
-
|
1227
|
-
|
1228
|
-
{:type => :subscribes, :action => :start, :resource_type => :service,
|
1229
|
-
|
1230
|
-
|
1225
|
+
{ :type => :subscribes, :action => :stop, :resource_type => :service,
|
1226
|
+
:resource_name => "nscd", :timing => :delayed,
|
1227
|
+
:style => :old },
|
1228
|
+
{ :type => :subscribes, :action => :start, :resource_type => :service,
|
1229
|
+
:resource_name => "nscd", :timing => :delayed,
|
1230
|
+
:style => :old },
|
1231
1231
|
]
|
1232
1232
|
)
|
1233
1233
|
end
|
@@ -1242,12 +1242,12 @@ describe FoodCritic::Api do
|
|
1242
1242
|
end
|
1243
1243
|
})).must_equal(
|
1244
1244
|
[
|
1245
|
-
{:type => :notifies, :action => :stop, :resource_type => :service,
|
1246
|
-
|
1247
|
-
|
1248
|
-
{:type => :notifies, :action => :start, :resource_type => :service,
|
1249
|
-
|
1250
|
-
|
1245
|
+
{ :type => :notifies, :action => :stop, :resource_type => :service,
|
1246
|
+
:resource_name => "nscd", :timing => :delayed,
|
1247
|
+
:style => :new },
|
1248
|
+
{ :type => :notifies, :action => :start, :resource_type => :service,
|
1249
|
+
:resource_name => "nscd", :timing => :delayed,
|
1250
|
+
:style => :new },
|
1251
1251
|
]
|
1252
1252
|
)
|
1253
1253
|
end
|
@@ -1262,12 +1262,12 @@ describe FoodCritic::Api do
|
|
1262
1262
|
end
|
1263
1263
|
})).must_equal(
|
1264
1264
|
[
|
1265
|
-
{:type => :subscribes, :action => :stop, :resource_type => :service,
|
1266
|
-
|
1267
|
-
|
1268
|
-
{:type => :subscribes, :action => :start, :resource_type => :service,
|
1269
|
-
|
1270
|
-
|
1265
|
+
{ :type => :subscribes, :action => :stop, :resource_type => :service,
|
1266
|
+
:resource_name => "nscd", :timing => :delayed,
|
1267
|
+
:style => :new },
|
1268
|
+
{ :type => :subscribes, :action => :start, :resource_type => :service,
|
1269
|
+
:resource_name => "nscd", :timing => :delayed,
|
1270
|
+
:style => :new },
|
1271
1271
|
]
|
1272
1272
|
)
|
1273
1273
|
end
|
@@ -1280,9 +1280,9 @@ describe FoodCritic::Api do
|
|
1280
1280
|
notifies :run, resources(:execute => "foo")
|
1281
1281
|
end
|
1282
1282
|
})).must_equal(
|
1283
|
-
[{:type => :notifies, :action => :run, :resource_type => :execute,
|
1284
|
-
|
1285
|
-
|
1283
|
+
[{ :type => :notifies, :action => :run, :resource_type => :execute,
|
1284
|
+
:resource_name => "foo", :timing => :delayed,
|
1285
|
+
:style => :old }]
|
1286
1286
|
)
|
1287
1287
|
end
|
1288
1288
|
it "old-style subscriptions" do
|
@@ -1292,9 +1292,9 @@ describe FoodCritic::Api do
|
|
1292
1292
|
subscribes :run, resources(:execute => "foo")
|
1293
1293
|
end
|
1294
1294
|
})).must_equal(
|
1295
|
-
[{:type => :subscribes, :action => :run, :resource_type => :execute,
|
1296
|
-
|
1297
|
-
|
1295
|
+
[{ :type => :subscribes, :action => :run, :resource_type => :execute,
|
1296
|
+
:resource_name => "foo", :timing => :delayed,
|
1297
|
+
:style => :old }]
|
1298
1298
|
)
|
1299
1299
|
end
|
1300
1300
|
it "old-style notifications" do
|
@@ -1304,9 +1304,9 @@ describe FoodCritic::Api do
|
|
1304
1304
|
notifies :run, "execute[foo]"
|
1305
1305
|
end
|
1306
1306
|
})).must_equal(
|
1307
|
-
[{:type => :notifies, :action => :run, :resource_type => :execute,
|
1308
|
-
|
1309
|
-
|
1307
|
+
[{ :type => :notifies, :action => :run, :resource_type => :execute,
|
1308
|
+
:resource_name => "foo", :timing => :delayed,
|
1309
|
+
:style => :new }]
|
1310
1310
|
)
|
1311
1311
|
end
|
1312
1312
|
it "old-style subscriptions" do
|
@@ -1316,9 +1316,9 @@ describe FoodCritic::Api do
|
|
1316
1316
|
subscribes :run, "execute[foo]"
|
1317
1317
|
end
|
1318
1318
|
})).must_equal(
|
1319
|
-
[{:type => :subscribes, :action => :run, :resource_type => :execute,
|
1320
|
-
|
1321
|
-
|
1319
|
+
[{ :type => :subscribes, :action => :run, :resource_type => :execute,
|
1320
|
+
:resource_name => "foo", :timing => :delayed,
|
1321
|
+
:style => :new }]
|
1322
1322
|
)
|
1323
1323
|
end
|
1324
1324
|
end
|
@@ -1486,25 +1486,25 @@ describe FoodCritic::Api do
|
|
1486
1486
|
end
|
1487
1487
|
describe "mark style of notification" do
|
1488
1488
|
it "specifies that the notification was in the old style" do
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1489
|
+
assert api.notifications(parse_ast(%q{
|
1490
|
+
template "/etc/foo.conf" do
|
1491
|
+
notifies :restart, resources(:service => 'foo')
|
1492
|
+
end
|
1493
|
+
})).first[:style].must_equal :old
|
1494
1494
|
end
|
1495
1495
|
it "specifies that the notification was in the new style" do
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1496
|
+
assert api.notifications(parse_ast(%q{
|
1497
|
+
template "/etc/foo.conf" do
|
1498
|
+
notifies :restart, "service[foo]"
|
1499
|
+
end
|
1500
|
+
})).first[:style].must_equal :new
|
1501
1501
|
end
|
1502
1502
|
end
|
1503
1503
|
end
|
1504
1504
|
|
1505
1505
|
describe "#read_ast" do
|
1506
1506
|
it "raises if the file cannot be read" do
|
1507
|
-
lambda {api.read_ast(nil)}.must_raise(TypeError)
|
1507
|
+
lambda { api.read_ast(nil) }.must_raise(TypeError)
|
1508
1508
|
end
|
1509
1509
|
end
|
1510
1510
|
|
@@ -1517,10 +1517,10 @@ describe FoodCritic::Api do
|
|
1517
1517
|
end.new
|
1518
1518
|
end
|
1519
1519
|
it "raises if the resource does not support XPath" do
|
1520
|
-
lambda{api.resource_attribute(nil, "mode")}.must_raise ArgumentError
|
1520
|
+
lambda { api.resource_attribute(nil, "mode") }.must_raise ArgumentError
|
1521
1521
|
end
|
1522
1522
|
it "raises if the attribute name is empty" do
|
1523
|
-
lambda{api.resource_attribute(resource, "")}.must_raise ArgumentError
|
1523
|
+
lambda { api.resource_attribute(resource, "") }.must_raise ArgumentError
|
1524
1524
|
end
|
1525
1525
|
end
|
1526
1526
|
|
@@ -1529,7 +1529,7 @@ describe FoodCritic::Api do
|
|
1529
1529
|
api.resource_attributes(api.find_resources(parse_ast(str)).first)
|
1530
1530
|
end
|
1531
1531
|
it "raises if the resource does not support XPath" do
|
1532
|
-
lambda{api.resource_attributes(nil)}.must_raise ArgumentError
|
1532
|
+
lambda { api.resource_attributes(nil) }.must_raise ArgumentError
|
1533
1533
|
end
|
1534
1534
|
it "returns a string value for a literal string" do
|
1535
1535
|
atts = str_to_atts(%q{
|
@@ -1537,8 +1537,8 @@ describe FoodCritic::Api do
|
|
1537
1537
|
owner "root"
|
1538
1538
|
end
|
1539
1539
|
})
|
1540
|
-
atts[
|
1541
|
-
atts[
|
1540
|
+
atts["owner"].wont_be_nil
|
1541
|
+
atts["owner"].must_equal "root"
|
1542
1542
|
end
|
1543
1543
|
it "returns a truthy value for a literal true" do
|
1544
1544
|
atts = str_to_atts(%q{
|
@@ -1546,8 +1546,8 @@ describe FoodCritic::Api do
|
|
1546
1546
|
recursive true
|
1547
1547
|
end
|
1548
1548
|
})
|
1549
|
-
atts[
|
1550
|
-
atts[
|
1549
|
+
atts["recursive"].wont_be_nil
|
1550
|
+
atts["recursive"].must_equal true
|
1551
1551
|
end
|
1552
1552
|
it "returns a truthy value for a literal false" do
|
1553
1553
|
atts = str_to_atts(%q{
|
@@ -1555,8 +1555,8 @@ describe FoodCritic::Api do
|
|
1555
1555
|
recursive false
|
1556
1556
|
end
|
1557
1557
|
})
|
1558
|
-
atts[
|
1559
|
-
atts[
|
1558
|
+
atts["recursive"].wont_be_nil
|
1559
|
+
atts["recursive"].must_equal false
|
1560
1560
|
end
|
1561
1561
|
it "decodes numeric attributes correctly" do
|
1562
1562
|
atts = str_to_atts(%q{
|
@@ -1565,8 +1565,8 @@ describe FoodCritic::Api do
|
|
1565
1565
|
mode 0755
|
1566
1566
|
end
|
1567
1567
|
})
|
1568
|
-
atts[
|
1569
|
-
atts[
|
1568
|
+
atts["mode"].wont_be_nil
|
1569
|
+
atts["mode"].must_equal "0755"
|
1570
1570
|
end
|
1571
1571
|
describe "block attributes" do
|
1572
1572
|
it "includes attributes with brace block values in the result" do
|
@@ -1577,9 +1577,9 @@ describe FoodCritic::Api do
|
|
1577
1577
|
only_if { File.exists?("/etc/bar") }
|
1578
1578
|
end
|
1579
1579
|
})
|
1580
|
-
atts[
|
1581
|
-
atts[
|
1582
|
-
atts[
|
1580
|
+
atts["only_if"].wont_be_nil
|
1581
|
+
atts["only_if"].must_respond_to :xpath
|
1582
|
+
atts["only_if"].name.must_equal "brace_block"
|
1583
1583
|
end
|
1584
1584
|
it "includes attributes with do block values in the result" do
|
1585
1585
|
atts = str_to_atts(%q{
|
@@ -1592,9 +1592,9 @@ describe FoodCritic::Api do
|
|
1592
1592
|
end
|
1593
1593
|
end
|
1594
1594
|
})
|
1595
|
-
atts[
|
1596
|
-
atts[
|
1597
|
-
atts[
|
1595
|
+
atts["only_if"].wont_be_nil
|
1596
|
+
atts["only_if"].must_respond_to :xpath
|
1597
|
+
atts["only_if"].name.must_equal "do_block"
|
1598
1598
|
end
|
1599
1599
|
it "supports multiple block attributes" do
|
1600
1600
|
atts = str_to_atts(%q{
|
@@ -1605,12 +1605,12 @@ describe FoodCritic::Api do
|
|
1605
1605
|
not_if { true }
|
1606
1606
|
end
|
1607
1607
|
})
|
1608
|
-
atts[
|
1609
|
-
atts[
|
1610
|
-
atts[
|
1611
|
-
atts[
|
1612
|
-
atts[
|
1613
|
-
atts[
|
1608
|
+
atts["only_if"].wont_be_nil
|
1609
|
+
atts["only_if"].must_respond_to :xpath
|
1610
|
+
atts["only_if"].name.must_equal "brace_block"
|
1611
|
+
atts["not_if"].wont_be_nil
|
1612
|
+
atts["not_if"].must_respond_to :xpath
|
1613
|
+
atts["not_if"].name.must_equal "brace_block"
|
1614
1614
|
end
|
1615
1615
|
it "doesn't include method calls in ruby blocks" do
|
1616
1616
|
atts = str_to_atts(%q{
|
@@ -1624,16 +1624,16 @@ describe FoodCritic::Api do
|
|
1624
1624
|
not_if { false }
|
1625
1625
|
end
|
1626
1626
|
})
|
1627
|
-
atts.keys.wont_include
|
1628
|
-
atts[
|
1629
|
-
atts[
|
1630
|
-
atts[
|
1631
|
-
atts[
|
1632
|
-
atts[
|
1633
|
-
atts[
|
1634
|
-
atts[
|
1635
|
-
atts[
|
1636
|
-
atts[
|
1627
|
+
atts.keys.wont_include "foo"
|
1628
|
+
atts["block"].wont_be_nil
|
1629
|
+
atts["block"].must_respond_to :xpath
|
1630
|
+
atts["block"].name.must_equal "do_block"
|
1631
|
+
atts["only_if"].wont_be_nil
|
1632
|
+
atts["only_if"].must_respond_to :xpath
|
1633
|
+
atts["only_if"].name.must_equal "brace_block"
|
1634
|
+
atts["not_if"].wont_be_nil
|
1635
|
+
atts["not_if"].must_respond_to :xpath
|
1636
|
+
atts["not_if"].name.must_equal "brace_block"
|
1637
1637
|
end
|
1638
1638
|
it "includes notifications in the result" do
|
1639
1639
|
atts = str_to_atts(%q{
|
@@ -1641,9 +1641,9 @@ describe FoodCritic::Api do
|
|
1641
1641
|
notifies :restart, "service[apache]"
|
1642
1642
|
end
|
1643
1643
|
})
|
1644
|
-
atts[
|
1645
|
-
atts[
|
1646
|
-
atts[
|
1644
|
+
atts["notifies"].wont_be_nil
|
1645
|
+
atts["notifies"].must_respond_to :xpath
|
1646
|
+
atts["notifies"].name.must_equal "args_add_block"
|
1647
1647
|
end
|
1648
1648
|
it "includes old-style notifications in the result" do
|
1649
1649
|
atts = str_to_atts(%q{
|
@@ -1651,16 +1651,16 @@ describe FoodCritic::Api do
|
|
1651
1651
|
notifies :restart, resources(:service => "apache")
|
1652
1652
|
end
|
1653
1653
|
})
|
1654
|
-
atts[
|
1655
|
-
atts[
|
1656
|
-
atts[
|
1654
|
+
atts["notifies"].wont_be_nil
|
1655
|
+
atts["notifies"].must_respond_to :xpath
|
1656
|
+
atts["notifies"].name.must_equal "args_add_block"
|
1657
1657
|
end
|
1658
1658
|
end
|
1659
1659
|
end
|
1660
1660
|
|
1661
1661
|
describe "#resource_attributes_by_type" do
|
1662
1662
|
it "raises if the ast does not support XPath" do
|
1663
|
-
lambda{api.resource_attributes_by_type(nil)}.must_raise ArgumentError
|
1663
|
+
lambda { api.resource_attributes_by_type(nil) }.must_raise ArgumentError
|
1664
1664
|
end
|
1665
1665
|
it "returns an empty hash if there are no resources" do
|
1666
1666
|
ast = MiniTest::Mock.new.expect :xpath, [], [String]
|
@@ -1670,18 +1670,18 @@ describe FoodCritic::Api do
|
|
1670
1670
|
|
1671
1671
|
describe "#resource_name" do
|
1672
1672
|
it "raises if the resource does not support XPath" do
|
1673
|
-
lambda {api.resource_name(
|
1673
|
+
lambda { api.resource_name("foo") }.must_raise ArgumentError
|
1674
1674
|
end
|
1675
1675
|
it "returns the resource name for a resource" do
|
1676
1676
|
ast = MiniTest::Mock.new
|
1677
|
-
ast.expect :xpath,
|
1678
|
-
api.resource_name(ast).must_equal
|
1677
|
+
ast.expect :xpath, "bob", [String]
|
1678
|
+
api.resource_name(ast).must_equal "bob"
|
1679
1679
|
end
|
1680
1680
|
end
|
1681
1681
|
|
1682
1682
|
describe "#resources_by_type" do
|
1683
1683
|
it "raises if the ast does not support XPath" do
|
1684
|
-
lambda{api.resources_by_type(nil)}.must_raise ArgumentError
|
1684
|
+
lambda { api.resources_by_type(nil) }.must_raise ArgumentError
|
1685
1685
|
end
|
1686
1686
|
it "returns an empty hash if there are no resources" do
|
1687
1687
|
ast = MiniTest::Mock.new.expect :xpath, [], [String]
|
@@ -1691,17 +1691,17 @@ describe FoodCritic::Api do
|
|
1691
1691
|
|
1692
1692
|
describe "#resource_type" do
|
1693
1693
|
it "raises if the resource does not support XPath" do
|
1694
|
-
lambda {api.resource_type(nil)}.must_raise ArgumentError
|
1694
|
+
lambda { api.resource_type(nil) }.must_raise ArgumentError
|
1695
1695
|
end
|
1696
1696
|
it "raises if the resource type cannot be determined" do
|
1697
1697
|
ast = MiniTest::Mock.new
|
1698
|
-
ast.expect :xpath,
|
1699
|
-
lambda {api.resource_type(ast)}.must_raise ArgumentError
|
1698
|
+
ast.expect :xpath, "", [String]
|
1699
|
+
lambda { api.resource_type(ast) }.must_raise ArgumentError
|
1700
1700
|
end
|
1701
1701
|
it "returns the resource type for a resource" do
|
1702
1702
|
ast = MiniTest::Mock.new
|
1703
|
-
ast.expect :xpath,
|
1704
|
-
api.resource_type(ast).must_equal
|
1703
|
+
ast.expect :xpath, "directory", [String]
|
1704
|
+
api.resource_type(ast).must_equal "directory"
|
1705
1705
|
end
|
1706
1706
|
end
|
1707
1707
|
|
@@ -1710,7 +1710,7 @@ describe FoodCritic::Api do
|
|
1710
1710
|
refute api.ruby_code?(nil)
|
1711
1711
|
end
|
1712
1712
|
it "says an empty string is not ruby code" do
|
1713
|
-
refute api.ruby_code?(
|
1713
|
+
refute api.ruby_code?("")
|
1714
1714
|
end
|
1715
1715
|
it "coerces arguments to a string" do
|
1716
1716
|
assert api.ruby_code?(%w{foo bar})
|
@@ -1726,21 +1726,21 @@ describe FoodCritic::Api do
|
|
1726
1726
|
describe "#searches" do
|
1727
1727
|
let(:ast) { MiniTest::Mock.new }
|
1728
1728
|
it "returns empty if the AST does not support XPath expressions" do
|
1729
|
-
api.searches(
|
1729
|
+
api.searches("not-an-ast").must_be_empty
|
1730
1730
|
end
|
1731
1731
|
it "returns empty if the AST has no elements" do
|
1732
1732
|
ast.expect :xpath, [], [String]
|
1733
1733
|
api.searches(ast).must_be_empty
|
1734
1734
|
end
|
1735
1735
|
it "returns the AST elements for each use of search" do
|
1736
|
-
ast.expect :xpath, [
|
1737
|
-
api.searches(ast).must_equal [
|
1736
|
+
ast.expect :xpath, ["ident"], [String]
|
1737
|
+
api.searches(ast).must_equal ["ident"]
|
1738
1738
|
end
|
1739
1739
|
end
|
1740
1740
|
|
1741
1741
|
describe "#standard_cookbook_subdirs" do
|
1742
1742
|
it "is enumerable" do
|
1743
|
-
api.standard_cookbook_subdirs.each{|s| s}
|
1743
|
+
api.standard_cookbook_subdirs.each { |s| s }
|
1744
1744
|
end
|
1745
1745
|
it "is sorted in alphabetical order" do
|
1746
1746
|
api.standard_cookbook_subdirs.must_equal(
|
@@ -1749,14 +1749,14 @@ describe FoodCritic::Api do
|
|
1749
1749
|
it "includes the directories generated by knife create cookbook" do
|
1750
1750
|
%w{attributes definitions files libraries providers recipes resources
|
1751
1751
|
templates}.each do |dir|
|
1752
|
-
|
1752
|
+
api.standard_cookbook_subdirs.must_include dir
|
1753
1753
|
end
|
1754
1754
|
end
|
1755
1755
|
it "does not include the spec directory" do
|
1756
|
-
api.standard_cookbook_subdirs.wont_include
|
1756
|
+
api.standard_cookbook_subdirs.wont_include "spec"
|
1757
1757
|
end
|
1758
1758
|
it "does not include a subdirectory of a subdirectory" do
|
1759
|
-
api.standard_cookbook_subdirs.wont_include
|
1759
|
+
api.standard_cookbook_subdirs.wont_include "default"
|
1760
1760
|
end
|
1761
1761
|
end
|
1762
1762
|
|
@@ -1769,7 +1769,7 @@ describe FoodCritic::Api do
|
|
1769
1769
|
end
|
1770
1770
|
describe :ignored_support_declarations do
|
1771
1771
|
it "should ignore supports without any arguments" do
|
1772
|
-
supports(
|
1772
|
+
supports("supports").must_be_empty
|
1773
1773
|
end
|
1774
1774
|
it "should ignore supports where an embedded string expression is used" do
|
1775
1775
|
supports('supports "red#{hat}"').must_be_empty
|
@@ -1779,46 +1779,46 @@ describe FoodCritic::Api do
|
|
1779
1779
|
supports(%q{
|
1780
1780
|
supports "redhat"
|
1781
1781
|
supports "scientific"
|
1782
|
-
}).must_equal([{:platform =>
|
1783
|
-
|
1782
|
+
}).must_equal([{ :platform => "redhat", :versions => [] },
|
1783
|
+
{ :platform => "scientific", :versions => [] }])
|
1784
1784
|
end
|
1785
1785
|
it "sorts the platform names in alphabetical order" do
|
1786
1786
|
supports(%q{
|
1787
1787
|
supports "scientific"
|
1788
1788
|
supports "redhat"
|
1789
|
-
}).must_equal([{:platform =>
|
1790
|
-
|
1789
|
+
}).must_equal([{ :platform => "redhat", :versions => [] },
|
1790
|
+
{ :platform => "scientific", :versions => [] }])
|
1791
1791
|
end
|
1792
1792
|
it "handles support declarations that include version constraints" do
|
1793
1793
|
supports(%q{
|
1794
1794
|
supports "redhat", '>= 6'
|
1795
|
-
}).must_equal([{:platform =>
|
1795
|
+
}).must_equal([{ :platform => "redhat", :versions => [">= 6"] }])
|
1796
1796
|
end
|
1797
1797
|
it "handles support declarations that include obsoleted version constraints" do
|
1798
1798
|
supports(%q{
|
1799
1799
|
supports 'redhat', '> 5.0', '< 7.0'
|
1800
1800
|
supports 'scientific', '> 5.0', '< 6.0'
|
1801
|
-
}).must_equal([{:platform =>
|
1802
|
-
{:platform =>
|
1801
|
+
}).must_equal([{ :platform => "redhat", :versions => ["> 5.0", "< 7.0"] },
|
1802
|
+
{ :platform => "scientific", :versions => ["> 5.0", "< 6.0"] }])
|
1803
1803
|
end
|
1804
1804
|
it "normalises platform symbol references to strings" do
|
1805
1805
|
supports(%q{
|
1806
1806
|
supports :ubuntu
|
1807
|
-
}).must_equal([{:platform =>
|
1807
|
+
}).must_equal([{ :platform => "ubuntu", :versions => [] }])
|
1808
1808
|
end
|
1809
1809
|
it "handles support declarations as symbols that include version constraints" do
|
1810
1810
|
supports(%q{
|
1811
1811
|
supports :redhat, '>= 6'
|
1812
|
-
}).must_equal([{:platform =>
|
1812
|
+
}).must_equal([{ :platform => "redhat", :versions => [">= 6"] }])
|
1813
1813
|
end
|
1814
1814
|
it "understands support declarations that use word lists" do
|
1815
1815
|
supports(%q{
|
1816
1816
|
%w{redhat centos fedora}.each do |os|
|
1817
1817
|
supports os
|
1818
1818
|
end
|
1819
|
-
}).must_equal([{:platform =>
|
1820
|
-
{:platform =>
|
1821
|
-
|
1819
|
+
}).must_equal([{ :platform => "centos", :versions => [] },
|
1820
|
+
{ :platform => "fedora", :versions => [] },
|
1821
|
+
{ :platform => "redhat", :versions => [] }])
|
1822
1822
|
end
|
1823
1823
|
end
|
1824
1824
|
|
@@ -1826,21 +1826,21 @@ describe FoodCritic::Api do
|
|
1826
1826
|
|
1827
1827
|
def all_templates
|
1828
1828
|
[
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1829
|
+
"templates/default/main.erb",
|
1830
|
+
"templates/default/included_1.erb",
|
1831
|
+
"templates/default/included_2.erb",
|
1832
1832
|
]
|
1833
1833
|
end
|
1834
1834
|
|
1835
1835
|
def template_ast(content)
|
1836
1836
|
parse_ast(FoodCritic::Template::ExpressionExtractor.new.extract(
|
1837
|
-
content).map{|e| e[:code]}.join(
|
1837
|
+
content).map { |e| e[:code] }.join(";"))
|
1838
1838
|
end
|
1839
1839
|
|
1840
1840
|
it "returns the path of the containing template when there are no partials" do
|
1841
|
-
ast = parse_ast(
|
1841
|
+
ast = parse_ast("<%= foo.erb %>")
|
1842
1842
|
api.stub :read_ast, ast do
|
1843
|
-
api.templates_included([
|
1843
|
+
api.templates_included(["foo.erb"], "foo.erb").must_equal ["foo.erb"]
|
1844
1844
|
end
|
1845
1845
|
end
|
1846
1846
|
|
@@ -1848,7 +1848,7 @@ describe FoodCritic::Api do
|
|
1848
1848
|
api.instance_variable_set(:@asts, {
|
1849
1849
|
:main => template_ast('<%= render "included_1.erb" %>
|
1850
1850
|
<%= render "included_2.erb" %>'),
|
1851
|
-
:ok => template_ast(
|
1851
|
+
:ok => template_ast("<%= @foo %>"),
|
1852
1852
|
})
|
1853
1853
|
def api.read_ast(path)
|
1854
1854
|
case path
|
@@ -1857,18 +1857,18 @@ describe FoodCritic::Api do
|
|
1857
1857
|
end
|
1858
1858
|
end
|
1859
1859
|
api.templates_included(all_templates,
|
1860
|
-
|
1861
|
-
[
|
1862
|
-
|
1863
|
-
|
1860
|
+
"templates/default/main.erb").must_equal(
|
1861
|
+
["templates/default/main.erb",
|
1862
|
+
"templates/default/included_1.erb",
|
1863
|
+
"templates/default/included_2.erb"]
|
1864
1864
|
)
|
1865
1865
|
end
|
1866
1866
|
|
1867
1867
|
it "doesn't mistake render options for partial template names" do
|
1868
1868
|
api.instance_variable_set(:@asts, {
|
1869
|
-
:main =>
|
1869
|
+
:main => template_ast('<%= render "included_1.erb",
|
1870
1870
|
:variables => {:foo => "included_2.erb"} %>'),
|
1871
|
-
:ok => template_ast(
|
1871
|
+
:ok => template_ast("<%= @foo %>"),
|
1872
1872
|
})
|
1873
1873
|
def api.read_ast(path)
|
1874
1874
|
case path
|
@@ -1877,17 +1877,17 @@ describe FoodCritic::Api do
|
|
1877
1877
|
end
|
1878
1878
|
end
|
1879
1879
|
api.templates_included(all_templates,
|
1880
|
-
|
1881
|
-
[
|
1880
|
+
"templates/default/main.erb").must_equal(
|
1881
|
+
["templates/default/main.erb", "templates/default/included_1.erb"]
|
1882
1882
|
)
|
1883
1883
|
end
|
1884
1884
|
|
1885
1885
|
it "raises if included partials have cycles" do
|
1886
1886
|
api.instance_variable_set(:@asts, {
|
1887
|
-
:main =>
|
1887
|
+
:main => template_ast('<%= render "included_1.erb" %>
|
1888
1888
|
<%= render "included_2.erb" %>'),
|
1889
1889
|
:loop => template_ast('<%= render "main.erb" %>'),
|
1890
|
-
:ok => template_ast(
|
1890
|
+
:ok => template_ast("<%= foo %>"),
|
1891
1891
|
})
|
1892
1892
|
def api.read_ast(path)
|
1893
1893
|
case path
|
@@ -1897,9 +1897,9 @@ describe FoodCritic::Api do
|
|
1897
1897
|
end
|
1898
1898
|
end
|
1899
1899
|
err = lambda do
|
1900
|
-
api.templates_included(all_templates,
|
1900
|
+
api.templates_included(all_templates, "templates/default/main.erb")
|
1901
1901
|
end.must_raise(FoodCritic::Api::RecursedTooFarError)
|
1902
|
-
err.message.must_equal
|
1902
|
+
err.message.must_equal "templates/default/main.erb"
|
1903
1903
|
end
|
1904
1904
|
end
|
1905
1905
|
|