foodcritic 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,7 +23,7 @@ module FoodCritic
23
23
  end
24
24
 
25
25
  if options[:type] == :vivified
26
- vivified_attribute_access(ast)
26
+ vivified_attribute_access(ast, options[:cookbook_dir])
27
27
  else
28
28
  standard_attribute_access(ast, options)
29
29
  end
@@ -32,13 +32,17 @@ module FoodCritic
32
32
  # Does the specified recipe check for Chef Solo?
33
33
  #
34
34
  # @param [Nokogiri::XML::Node] ast The AST of the cookbook recipe to check.
35
- # @return [Boolean] True if there is a test for Chef::Config[:solo] in the
36
- # recipe
35
+ # @return [Boolean] True if there is a test for Chef::Config[:solo],
36
+ # Chef::Config['solo'] in the recipe
37
37
  def checks_for_chef_solo?(ast)
38
38
  raise_unless_xpath!(ast)
39
39
  ! ast.xpath(%q{//if/aref[count(descendant::const[@value = 'Chef' or
40
40
  @value = 'Config']) = 2
41
- and count(descendant::ident[@value='solo']) > 0]}).empty?
41
+ and
42
+ ( count(descendant::ident[@value='solo']) > 0
43
+ or count(descendant::tstring_content[@value='solo']) > 0
44
+ )
45
+ ]}).empty?
42
46
  end
43
47
 
44
48
  # Is the chef-solo-search library available?
@@ -181,10 +185,11 @@ module FoodCritic
181
185
  # @param [String] str The string to check
182
186
  # @return [Boolean] True if this string might be an OS command
183
187
  def os_command?(str)
184
- str.start_with?('grep ', 'which ') or # common commands
185
- str.include?('|') or # a pipe, could be alternation
186
- str.match(/^[\w]+$/) or # command name only
187
- str.match(/ --?[a-z]/i) # command-line flag
188
+ str.start_with?('grep ', 'net ', 'which ') or # common commands
189
+ str.include?('|') or # a pipe, could be alternation
190
+ str.include?('/') or # file path delimiter
191
+ str.match(/^[\w]+$/) or # command name only
192
+ str.match(/ --?[a-z]/i) # command-line flag
188
193
  end
189
194
 
190
195
  # Read the AST for the given Ruby source file
@@ -216,12 +221,14 @@ module FoodCritic
216
221
  atts[:name] = name unless name.empty?
217
222
  resource.xpath('do_block/descendant::command
218
223
  [count(ancestor::do_block) = 1]').each do |att|
219
- if att.xpath('descendant::symbol').empty?
220
- att_value = att.xpath('string(descendant::tstring_content/@value)')
221
- else
222
- att_value =
224
+ att_value =
225
+ if ! att.xpath('args_add_block[count(descendant::args_add)>1]').empty?
226
+ att.xpath('args_add_block').first
227
+ elsif att.xpath('descendant::symbol').empty?
228
+ att.xpath('string(descendant::tstring_content/@value)')
229
+ else
223
230
  att.xpath('string(descendant::symbol/ident/@value)').to_sym
224
- end
231
+ end
225
232
  atts[att.xpath('string(ident/@value)')] = att_value
226
233
  end
227
234
  resource.xpath("do_block/descendant::method_add_block[
@@ -357,6 +364,21 @@ module FoodCritic
357
364
  xml_node
358
365
  end
359
366
 
367
+ def node_method?(meth, cookbook_dir)
368
+ chef_dsl_methods.include?(meth) || patched_node_method?(meth, cookbook_dir)
369
+ end
370
+
371
+ def patched_node_method?(meth, cookbook_dir)
372
+ return false if cookbook_dir.nil? || ! Dir.exists?(cookbook_dir)
373
+ cbk_tree_path = Pathname.new(File.join(cookbook_dir, '..'))
374
+ libs = Dir[File.join(cbk_tree_path.realpath, '*/libraries/*.rb')]
375
+ libs.any? do |lib|
376
+ ! read_ast(lib).xpath(%Q{//class[count(descendant::const[@value='Chef'])
377
+ > 0][count(descendant::const[@value='Node']) > 0]/descendant::def/
378
+ ident[@value='#{meth.to_s}']}).empty?
379
+ end
380
+ end
381
+
360
382
  # If the provided node is the line / column information.
361
383
  #
362
384
  # @param [Nokogiri::XML::Node] node A node within the AST
@@ -382,14 +404,14 @@ module FoodCritic
382
404
  ast.xpath(expr, AttFilter.new).sort
383
405
  end
384
406
 
385
- def vivified_attribute_access(ast)
407
+ def vivified_attribute_access(ast, cookbook_dir)
386
408
  calls = ast.xpath(%q{//*[self::call or self::field]
387
409
  [is_att_type(vcall/ident/@value) or
388
410
  is_att_type(var_ref/ident/@value)][@value='.']}, AttFilter.new)
389
411
  calls.select do |call|
390
412
  call.xpath("aref/args_add_block").size == 0 and
391
413
  (call.xpath("descendant::ident").size > 1 and
392
- ! chef_dsl_methods.include?(call.xpath("ident/@value").to_s.to_sym))
414
+ ! node_method?(call.xpath("ident/@value").to_s.to_sym, cookbook_dir))
393
415
  end.sort
394
416
  end
395
417
 
@@ -11,7 +11,7 @@ module FoodCritic
11
11
  @original_args = args.dup
12
12
  @options = {:fail_tags => [], :tags => [], :include_rules => []}
13
13
  @parser = OptionParser.new do |opts|
14
- opts.banner = 'foodcritic [cookbook_path]'
14
+ opts.banner = 'foodcritic [cookbook_paths]'
15
15
  opts.on("-r", "--[no-]repl",
16
16
  "Drop into a REPL for interactive rule editing.") do |r|
17
17
  options[:repl] = r
@@ -76,18 +76,18 @@ module FoodCritic
76
76
  "foodcritic #{FoodCritic::VERSION}"
77
77
  end
78
78
 
79
- # If the cookbook path provided is valid
79
+ # If the cookbook paths provided are valid
80
80
  #
81
- # @return [Boolean] True if the path is a directory that exists.
82
- def valid_path?
83
- @args.length == 1 and Dir.exists?(@args[0])
81
+ # @return [Boolean] True if the paths exist.
82
+ def valid_paths?
83
+ @args.any? && @args.all? {|path| File.exists?(path) }
84
84
  end
85
85
 
86
- # The cookbook path
86
+ # The cookbook paths
87
87
  #
88
- # @return [String] Path to the cookbook(s) being checked.
89
- def cookbook_path
90
- @args[0]
88
+ # @return [Array<String>] Path(s) to the cookbook(s) being checked.
89
+ def cookbook_paths
90
+ @args
91
91
  end
92
92
 
93
93
  # Is the search grammar specified valid?
@@ -20,16 +20,16 @@ module FoodCritic
20
20
  # The collected warnings (if any) raised against a cookbook tree.
21
21
  class Review
22
22
 
23
- attr_reader :cookbook_path, :warnings
23
+ attr_reader :cookbook_paths, :warnings
24
24
 
25
25
  # Create a new review
26
26
  #
27
- # @param [String] cookbook_path The path this review was performed against
27
+ # @param [String] cookbook_paths The path this review was performed against
28
28
  # @param [Array] warnings The warnings raised in this review
29
29
  # @param [Boolean] is_failed Have warnings been raised that mean this
30
30
  # should be considered failed?
31
- def initialize(cookbook_path, warnings, is_failed)
32
- @cookbook_path = cookbook_path
31
+ def initialize(cookbook_paths, warnings, is_failed)
32
+ @cookbook_paths = cookbook_paths
33
33
  @warnings = warnings
34
34
  @is_failed = is_failed
35
35
  end
@@ -12,7 +12,7 @@ module FoodCritic
12
12
  include FoodCritic::Api
13
13
 
14
14
  # The default version that will be used to determine relevant rules
15
- DEFAULT_CHEF_VERSION = "0.10.8"
15
+ DEFAULT_CHEF_VERSION = "0.10.10"
16
16
 
17
17
  # Perform option parsing from the provided arguments and do a lint check
18
18
  # based on those arguments.
@@ -25,8 +25,8 @@ module FoodCritic
25
25
  return [cmd_line.version, 0] if cmd_line.show_version?
26
26
  if ! cmd_line.valid_grammar?
27
27
  [cmd_line.help, 4]
28
- elsif cmd_line.valid_path?
29
- review = FoodCritic::Linter.new.check(cmd_line.cookbook_path,
28
+ elsif cmd_line.valid_paths?
29
+ review = FoodCritic::Linter.new.check(cmd_line.cookbook_paths,
30
30
  cmd_line.options)
31
31
  [review, review.failed? ? 3 : 0]
32
32
  else
@@ -42,16 +42,16 @@ module FoodCritic
42
42
  # Review the cookbooks at the provided path, identifying potential
43
43
  # improvements.
44
44
  #
45
- # @param [String] cookbook_path The file path to an individual cookbook
46
- # directory
45
+ # @param [String] cookbook_paths The file path(s) to an individual
46
+ # cookbook(s) being checked
47
47
  # @param [Hash] options Options to apply to the linting
48
48
  # @option options [Array] tags The tags to filter rules based on
49
49
  # @option options [Array] fail_tags The tags to fail the build on
50
50
  # @return [FoodCritic::Review] A review of your cookbooks, with any
51
51
  # warnings issued.
52
- def check(cookbook_path, options)
53
- raise ArgumentError, "Cookbook path is required" if cookbook_path.nil?
54
- @last_cookbook_path, @last_options = cookbook_path, options
52
+ def check(cookbook_paths, options)
53
+ raise ArgumentError, "Cookbook paths are required" if cookbook_paths.nil?
54
+ @last_cookbook_paths, @last_options = cookbook_paths, options
55
55
  load_rules unless defined? @rules
56
56
  warnings = []; last_dir = nil; matched_rule_tags = Set.new
57
57
 
@@ -59,7 +59,7 @@ module FoodCritic
59
59
  matching_tags?(options[:tags], rule.tags) and
60
60
  applies_to_version?(rule, options[:chef_version] || DEFAULT_CHEF_VERSION)
61
61
  end
62
- files_to_process(cookbook_path).each do |file|
62
+ files_to_process(cookbook_paths).each do |file|
63
63
  cookbook_dir = Pathname.new(
64
64
  File.join(File.dirname(file), '..')).cleanpath
65
65
  ast = read_ast(file)
@@ -82,7 +82,7 @@ module FoodCritic
82
82
  last_dir = cookbook_dir
83
83
  end
84
84
 
85
- @review = Review.new(cookbook_path, warnings,
85
+ @review = Review.new(cookbook_paths, warnings,
86
86
  should_fail_build?(options[:fail_tags], matched_rule_tags))
87
87
 
88
88
  binding.pry if options[:repl]
@@ -92,7 +92,7 @@ module FoodCritic
92
92
  # Convenience method to repeat the last check. Intended to be used from the
93
93
  # REPL.
94
94
  def recheck
95
- check(@last_cookbook_path, @last_options)
95
+ check(@last_cookbook_paths, @last_options)
96
96
  end
97
97
 
98
98
  # Load the rules from the (fairly unnecessary) DSL.
@@ -146,14 +146,23 @@ module FoodCritic
146
146
  # Return the files within a cookbook tree that we are interested in trying
147
147
  # to match rules against.
148
148
  #
149
- # @param [String] dir The cookbook directory
150
- # @return [Array] The files underneath the provided directory to be
149
+ # @param [Array<String>] dirs The cookbook path(s)
150
+ # @return [Array] The files underneath the provided paths to be
151
151
  # processed.
152
- def files_to_process(dir)
153
- return [dir] unless File.directory? dir
154
- cookbook_glob = '{attributes,providers,recipes,resources}/*.rb'
155
- Dir.glob(File.join(dir, cookbook_glob)) +
156
- Dir.glob(File.join(dir, "*/#{cookbook_glob}"))
152
+ def files_to_process(dirs)
153
+ files = []
154
+
155
+ dirs.each do |dir|
156
+ if File.directory? dir
157
+ cookbook_glob = '{attributes,providers,recipes,resources}/*.rb'
158
+ files += Dir.glob(File.join(dir, cookbook_glob)) +
159
+ Dir.glob(File.join(dir, "*/#{cookbook_glob}"))
160
+ else
161
+ files << dir
162
+ end
163
+ end
164
+
165
+ files
157
166
  end
158
167
 
159
168
  # Whether to fail the build.
@@ -47,7 +47,7 @@ module FoodCritic
47
47
  else
48
48
  next
49
49
  end
50
- }, review.cookbook_path])
50
+ }] + review.cookbook_paths)
51
51
  Rak.send(:remove_const, :VERSION) # Prevent duplicate VERSION warning
52
52
  load Gem.bin_path('rak', 'rak') # Assumes Rubygems
53
53
  end
@@ -235,8 +235,8 @@ rule "FC019", "Access node attributes in a consistent manner" do
235
235
  end
236
236
  types = [:string, :symbol, :vivified].map do |type|
237
237
  {:access_type => type, :count => files.map do |file|
238
- attribute_access(file[:ast], :type => type,
239
- :ignore_calls => true).tap do |ast|
238
+ attribute_access(file[:ast], :type => type, :ignore_calls => true,
239
+ :cookbook_dir => cookbook_dir).tap do |ast|
240
240
  if (! ast.empty?) and (! asts.has_key?(type))
241
241
  asts[type] = {:ast => ast, :path => file[:path]}
242
242
  end
@@ -303,7 +303,10 @@ rule "FC022", "Resource condition within loop may not behave as expected" do
303
303
  @value='only_if']/ancestor::*[self::method_add_block or
304
304
  self::command][1]/descendant::ident/@value}).map{|a| a.value})).empty?
305
305
  c = resource.xpath('command[count(descendant::string_embexpr) = 0]')
306
- resource unless c.empty?
306
+ resource unless c.empty? || block_vars.any? do |var|
307
+ ! resource.xpath(%Q{command/args_add_block/args_add/
308
+ var_ref/ident[@value='#{var}']}).empty?
309
+ end
307
310
  end
308
311
  end
309
312
  end.flatten.compact
@@ -359,3 +362,19 @@ rule "FC025", "Prefer chef_gem to compile-time gem install" do
359
362
  end
360
363
  end
361
364
  end
365
+
366
+ rule "FC026", "Conditional execution block attribute contains only string" do
367
+ tags %w{correctness}
368
+ applies_to {|version| version >= gem_version("0.7.4")}
369
+ recipe do |ast|
370
+ find_resources(ast).map{|r| resource_attributes(r)}.map do |resource|
371
+ [resource['not_if'], resource['only_if']]
372
+ end.flatten.compact.select do |condition|
373
+ condition.respond_to?(:xpath) and
374
+ ! condition.xpath('descendant::string_literal').empty? and
375
+ ! condition.xpath('stmts_add/string_literal').empty? and
376
+ condition.xpath('descendant::stmts_add[count(ancestor::
377
+ string_literal) = 0]').size == 1
378
+ end
379
+ end
380
+ end
@@ -1,4 +1,4 @@
1
1
  module FoodCritic
2
2
  # The current version of foodcritic
3
- VERSION = '1.2.0'
3
+ VERSION = '1.3.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foodcritic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-21 00:00:00.000000000 Z
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gherkin
@@ -193,12 +193,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  version: '0'
194
194
  segments:
195
195
  - 0
196
- hash: 700162912727168001
196
+ hash: -2700846089795891822
197
197
  requirements: []
198
198
  rubyforge_project:
199
199
  rubygems_version: 1.8.19
200
200
  signing_key:
201
201
  specification_version: 3
202
- summary: foodcritic-1.2.0
202
+ summary: foodcritic-1.3.0
203
203
  test_files: []
204
204
  has_rdoc: