foodcritic 4.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -0
  3. data/README.md +7 -7
  4. data/bin/foodcritic +1 -1
  5. data/chef_dsl_metadata/chef_11.0.0.json +4 -0
  6. data/chef_dsl_metadata/chef_11.10.0.json +176 -1
  7. data/chef_dsl_metadata/chef_11.10.2.json +176 -1
  8. data/chef_dsl_metadata/chef_11.10.4.json +176 -1
  9. data/chef_dsl_metadata/chef_11.12.0.json +10861 -0
  10. data/chef_dsl_metadata/chef_11.12.2.json +10861 -0
  11. data/chef_dsl_metadata/chef_11.12.4.json +10800 -0
  12. data/chef_dsl_metadata/chef_11.12.8.json +10800 -0
  13. data/chef_dsl_metadata/chef_11.14.2.json +11172 -0
  14. data/chef_dsl_metadata/chef_11.14.6.json +11178 -0
  15. data/chef_dsl_metadata/chef_11.16.0.json +11236 -0
  16. data/chef_dsl_metadata/chef_11.16.2.json +11236 -0
  17. data/chef_dsl_metadata/chef_11.16.4.json +11236 -0
  18. data/chef_dsl_metadata/chef_11.18.0.json +11291 -0
  19. data/chef_dsl_metadata/chef_11.18.12.json +11291 -0
  20. data/chef_dsl_metadata/chef_11.18.6.json +11291 -0
  21. data/chef_dsl_metadata/chef_11.2.0.json +4 -0
  22. data/chef_dsl_metadata/chef_11.4.0.json +4 -0
  23. data/chef_dsl_metadata/chef_11.4.2.json +4 -0
  24. data/chef_dsl_metadata/chef_11.4.4.json +4 -0
  25. data/chef_dsl_metadata/chef_11.6.0.json +4 -0
  26. data/chef_dsl_metadata/chef_11.6.2.json +4 -0
  27. data/chef_dsl_metadata/chef_11.8.0.json +4 -0
  28. data/chef_dsl_metadata/chef_11.8.2.json +4 -0
  29. data/chef_dsl_metadata/chef_12.0.0.json +11855 -0
  30. data/chef_dsl_metadata/chef_12.0.1.json +11865 -0
  31. data/chef_dsl_metadata/chef_12.0.3.json +12054 -0
  32. data/chef_dsl_metadata/chef_12.1.0.json +12468 -0
  33. data/chef_dsl_metadata/chef_12.1.1.json +12468 -0
  34. data/chef_dsl_metadata/chef_12.1.2.json +12468 -0
  35. data/chef_dsl_metadata/chef_12.2.1.json +12645 -0
  36. data/chef_dsl_metadata/chef_12.3.0.json +12839 -0
  37. data/chef_dsl_metadata/chef_12.4.0.json +14625 -0
  38. data/chef_dsl_metadata/chef_12.4.1.json +14625 -0
  39. data/features/002_check_string_interpolation.feature +5 -0
  40. data/features/041_check_raw_download.feature +17 -9
  41. data/features/044_check_for_bare_attribute_keys.feature +10 -0
  42. data/features/048_check_for_shellout.feature +6 -1
  43. data/features/052_check_for_metadata_using_suggests_keyword.feature +15 -0
  44. data/features/053_check_for_metadata_using_recommends_keyword.feature +15 -0
  45. data/features/step_definitions/cookbook_steps.rb +66 -4
  46. data/features/support/command_helpers.rb +8 -2
  47. data/features/support/env.rb +7 -3
  48. data/lib/foodcritic/api.rb +5 -4
  49. data/lib/foodcritic/chef.rb +3 -7
  50. data/lib/foodcritic/command_line.rb +15 -0
  51. data/lib/foodcritic/domain.rb +14 -0
  52. data/lib/foodcritic/linter.rb +44 -4
  53. data/lib/foodcritic/rake_task.rb +15 -11
  54. data/lib/foodcritic/rules.rb +38 -6
  55. data/lib/foodcritic/version.rb +1 -1
  56. data/man/foodcritic.1 +8 -1
  57. data/man/foodcritic.1.ronn +7 -0
  58. data/misc/lucene.treetop +150 -0
  59. data/spec/foodcritic/command_line_spec.rb +10 -0
  60. data/spec/foodcritic/domain_spec.rb +19 -0
  61. data/spec/foodcritic/linter_spec.rb +13 -1
  62. data/spec/regression/cookbooks.txt +0 -60
  63. data/spec/regression/expected-output.txt +5 -445
  64. data/spec/regression_helpers.rb +5 -5
  65. data/spec/spec_helper.rb +7 -3
  66. metadata +38 -7
@@ -43,3 +43,8 @@ Feature: Check String Interpolation
43
43
  Given a cookbook with a single recipe for which the first hash value is an interpolated string
44
44
  When I check the cookbook
45
45
  Then the string interpolation warning 002 should be displayed
46
+
47
+ Scenario: String interpolation in heredoc
48
+ Given a cookbook with a single recipe that logs an interpolated string heredoc
49
+ When I check the cookbook
50
+ Then the string interpolation warning 002 should not be displayed
@@ -9,18 +9,26 @@ Feature: Check for direct usage of curl or wget
9
9
  When I check the cookbook
10
10
  Then the execute resource used to run curl or wget commands warning 041 <display> be displayed
11
11
  Examples:
12
- | name | display |
13
- | curl 'http://example.org/' | should |
14
- | wget 'http://example.org/' | should |
15
- | which foo | should not |
12
+ | name | display |
13
+ | curl 'http://example.org/' | should not |
14
+ | wget 'http://example.org/' | should |
15
+ | which curl | should not |
16
+ | sudo wget 'http://example.org/' | should |
17
+ | reboot | should not |
16
18
 
17
19
  Scenario Outline: Execute resource
18
20
  Given a cookbook recipe with an execute resource that runs the command <command>
19
21
  When I check the cookbook
20
22
  Then the execute resource used to run curl or wget commands warning 041 <display> be displayed
21
23
  Examples:
22
- | command | display |
23
- | which foo | should not |
24
- | curl 'http://example.org/' | should |
25
- | wget 'http://example.org/' | should |
26
- | mkdir foo && wget 'http://example.org/' | should |
24
+ | command | display |
25
+ | which curl | should not |
26
+ | sudo wget 'http://example.org/' | should |
27
+ | curl -X POST 'http://example.org/' | should not |
28
+ | curl 'http://example.org/' | should not |
29
+ | curl 'http://example.org/' -o foo | should |
30
+ | curl 'http://example.org/' --output foo | should |
31
+ | curl 'http://example.org/' &>bar | should |
32
+ | curl -o baz 'http://example.org/'> bing | should |
33
+ | wget 'http://example.org/' | should |
34
+ | mkdir foo && wget 'http://example.org/' | should |
@@ -41,3 +41,13 @@ Feature: Check for bare attribute keys
41
41
  Given a cookbook attributes file that sets an attribute to be the result of a library call
42
42
  When I check the cookbook
43
43
  Then the bare attribute keys warning 044 should not be displayed against the library call
44
+
45
+ Scenario: Attribute chain with new line
46
+ Given a cookbook attributes file that sets an attribute to have line break
47
+ When I check the cookbook
48
+ Then the bare attribute keys warning 044 should not be displayed against the local variable
49
+
50
+ Scenario: Attribute chain with new line and parameterized name
51
+ Given a cookbook attributes file that sets an attribute to have line break and arguments
52
+ When I check the cookbook
53
+ Then the bare attribute keys warning 044 should not be displayed against the new line
@@ -29,6 +29,11 @@ Feature: Check for spawning without Mixlib::ShellOut
29
29
  Then the prefer mixlib shellout warning 048 should not be displayed
30
30
 
31
31
  Scenario: Group resource
32
- Given a cookbook recipe that contains a group resource that uses the 'system' attribute
32
+ Given a cookbook recipe that contains a group resource that uses the 'system' bool attribute
33
33
  When I check the cookbook
34
34
  Then the prefer mixlib shellout warning 048 should not be displayed against the group resource
35
+
36
+ Scenario: User resource
37
+ Given a cookbook recipe that contains a user resource that uses the 'system' string attribute
38
+ When I check the cookbook
39
+ Then the prefer mixlib shellout warning 048 should not be displayed against the user resource
@@ -0,0 +1,15 @@
1
+ Feature: Check for metadata using suggests keyword
2
+
3
+ In order to ensure that recipe metadata is stable
4
+ As a developer
5
+ I want to identify metadata that is using unimplemented features whose definitions may change in the future
6
+
7
+ Scenario: Metadata with the suggests keyword
8
+ Given a cookbook with metadata that includes a suggests keyword
9
+ When I check the cookbook
10
+ Then the metadata using suggests warning 052 should be shown against the metadata file
11
+
12
+ Scenario: Metadata without the suggests keyword
13
+ Given a cookbook with metadata that does not include a suggests keyword
14
+ When I check the cookbook
15
+ Then the metadata using suggests warning 052 should be not shown against the metadata file
@@ -0,0 +1,15 @@
1
+ Feature: Check for metadata using recommends keyword
2
+
3
+ In order to ensure that recipe metadata is stable
4
+ As a developer
5
+ I want to identify metadata that is using unimplemented features whose definitions may change in the future
6
+
7
+ Scenario: Metadata with the recommends keyword
8
+ Given a cookbook with metadata that includes a recommends keyword
9
+ When I check the cookbook
10
+ Then the metadata using recommends warning 053 should be shown against the metadata file
11
+
12
+ Scenario: Metadata without the recommends keyword
13
+ Given a cookbook with metadata that does not include a recommends keyword
14
+ When I check the cookbook
15
+ Then the metadata using recommends warning 053 should be not shown against the metadata file
@@ -34,6 +34,22 @@ Given 'a cookbook attributes file with a do block that takes arguments' do
34
34
  }
35
35
  end
36
36
 
37
+ Given 'a cookbook attributes file that sets an attribute to have line break' do
38
+ write_attributes %q{
39
+ default['foo']['bar']['baz'][
40
+ 'bing']['blonk'] = 'stupendous'
41
+ }
42
+ end
43
+
44
+ Given 'a cookbook attributes file that sets an attribute to have line break and arguments' do
45
+ write_attributes %q{
46
+ %w{ foo bar }.each do |baz|
47
+ default['foo']["bing_#{baz}"][
48
+ 'blonk'] = baz
49
+ end
50
+ }
51
+ end
52
+
37
53
  Given /^a cookbook (attributes|recipe) file with assignment (.*)$/ do |type, assignment|
38
54
  if type == 'attributes'
39
55
  write_attributes assignment
@@ -42,7 +58,7 @@ Given /^a cookbook (attributes|recipe) file with assignment (.*)$/ do |type, ass
42
58
  end
43
59
  end
44
60
 
45
- Given "a cookbook recipe that contains a group resource that uses the 'system' attribute" do
61
+ Given "a cookbook recipe that contains a group resource that uses the 'system' bool attribute" do
46
62
  write_recipe %q{
47
63
  group "senge" do
48
64
  system true
@@ -50,6 +66,14 @@ Given "a cookbook recipe that contains a group resource that uses the 'system' a
50
66
  }
51
67
  end
52
68
 
69
+ Given "a cookbook recipe that contains a user resource that uses the 'system' string attribute" do
70
+ write_recipe %q{
71
+ user "foo" do
72
+ system "bar"
73
+ end
74
+ }
75
+ end
76
+
53
77
  Given /^a cookbook recipe that declares (too many )?execute resources varying only in the command in branching conditionals$/ do |too_many|
54
78
  extra_resource = %q{
55
79
  execute "bing" do
@@ -947,7 +971,7 @@ Given /^a cookbook that declares ([a-z]+) attributes via symbols$/ do |attribute
947
971
  end
948
972
 
949
973
  Given /^a cookbook that does not contain a definition and has (no|a) definitions directory$/ do |has_dir|
950
- create_dir 'cookbooks/example/definitions/' unless has_dir == 'no'
974
+ create_directory 'cookbooks/example/definitions/' unless has_dir == 'no'
951
975
  write_recipe %q{
952
976
  log "A defining characteristic of this cookbook is that it has no definitions"
953
977
  }
@@ -1193,6 +1217,14 @@ Given /^a cookbook with a single recipe that creates a directory resource with (
1193
1217
  'a literal and interpolated variable' => :literal_and_interpolated_symbol}[path_type])
1194
1218
  end
1195
1219
 
1220
+ Given 'a cookbook with a single recipe that logs an interpolated string heredoc' do
1221
+ write_recipe %q(
1222
+ Chef::Log.warn <<-LOG
1223
+ Warning stuff has gone wrong: #{stuff}
1224
+ LOG
1225
+ )
1226
+ end
1227
+
1196
1228
  Given 'a cookbook with a single recipe that searches but checks first (alternation) to see if this is server' do
1197
1229
  write_recipe %q{
1198
1230
  if Chef::Config[:solo] || we_dont_want_to_use_search
@@ -1275,6 +1307,20 @@ Given /^a cookbook with metadata that (specifies|does not specify) the cookbook
1275
1307
  }
1276
1308
  end
1277
1309
 
1310
+ Given /^a cookbook with metadata that (includes|does not include) a recommends keyword$/ do |includes|
1311
+ write_metadata %Q{
1312
+ depends "bar"
1313
+ #{"recommends 'foo'" if includes == 'includes'}
1314
+ }
1315
+ end
1316
+
1317
+ Given /^a cookbook with metadata that (includes|does not include) a suggests keyword$/ do |includes|
1318
+ write_metadata %Q{
1319
+ depends "bar"
1320
+ #{"suggests 'foo'" if includes == 'includes'}
1321
+ }
1322
+ end
1323
+
1278
1324
  Given /^a directory that contains a role file ([^ ]+) in (json|ruby) that defines role name (.*)$/ do |file_name, format, role_name|
1279
1325
  role(:role_name => %Q{"#{role_name}"}, :file_name => file_name, :format => format.to_sym)
1280
1326
  end
@@ -1708,7 +1754,7 @@ Given /^a template directory that contains a binary file (.*) that is not valid
1708
1754
  template_dir = 'cookbooks/example/templates/default'
1709
1755
  write_recipe ''
1710
1756
  write_file "#{template_dir}/innocent_template.erb", '<%= hello %>'
1711
- File.open("#{current_dir}/#{template_dir}/#{filename}", 'wb'){|f| f.putc(0x93)}
1757
+ File.open("#{expand_path('.')}/#{template_dir}/#{filename}", 'wb'){|f| f.putc(0x93)}
1712
1758
  end
1713
1759
 
1714
1760
  Given 'each role directory has a role with a name that does not match the containing file name' do
@@ -1755,7 +1801,7 @@ end
1755
1801
 
1756
1802
  When /^I check the cookbook specifying ([^ ]+) as the Chef version$/ do |version|
1757
1803
  options = ['-c', version, 'cookbooks/example']
1758
- in_current_dir do
1804
+ cd '.' do
1759
1805
  options = ['-I', 'rules/test.rb'] + options if Dir.exists?('rules')
1760
1806
  end
1761
1807
  run_lint(options)
@@ -1929,6 +1975,10 @@ Then /^the bare attribute keys warning 044 should not be displayed against the (
1929
1975
  expect_warning 'FC044', {:expect_warning => false, :line => 2, :file_type => :attributes}
1930
1976
  end
1931
1977
 
1978
+ Then /^the bare attribute keys warning 044 should not be displayed against the new line$/ do
1979
+ expect_warning 'FC044', {:expect_warning => false, :line => 3, :file_type => :attributes}
1980
+ end
1981
+
1932
1982
  Then 'the execute resource used to run git commands warning 040 should be displayed against the last resource' do
1933
1983
  expect_warning 'FC040', {:line => 7}
1934
1984
  end
@@ -1952,6 +2002,10 @@ Then 'the prefer mixlib shellout warning 048 should not be displayed against the
1952
2002
  expect_warning 'FC048', {:expect_warning => false, :line => 2}
1953
2003
  end
1954
2004
 
2005
+ Then 'the prefer mixlib shellout warning 048 should not be displayed against the user resource' do
2006
+ expect_warning 'FC048', {:expect_warning => false, :line => 2}
2007
+ end
2008
+
1955
2009
  Then /^the role name does not match file name warning 049 should( not)? be shown( against the second name)?$/ do |not_shown, second|
1956
2010
  expect_warning 'FC049', {:expect_warning => ! not_shown,
1957
2011
  :file => 'roles/webserver.rb', :line => second ? 2 : 1}
@@ -2241,3 +2295,11 @@ Given(/^a cookbook with an? (.*) file with an interpolated name$/) do |file_type
2241
2295
  write_definition "apache_site", content if file_type == "definition"
2242
2296
  write_library "lib", content if file_type == "library"
2243
2297
  end
2298
+
2299
+ Then /^the metadata using suggests warning 052 should be (shown|not shown) against the metadata file$/ do |show_warning|
2300
+ expect_warning('FC052', :file => "metadata.rb", :line => 2, :expect_warning => show_warning == 'shown')
2301
+ end
2302
+
2303
+ Then /^the metadata using recommends warning 053 should be (shown|not shown) against the metadata file$/ do |show_warning|
2304
+ expect_warning('FC053', :file => "metadata.rb", :line => 2, :expect_warning => show_warning == 'shown')
2305
+ end
@@ -62,6 +62,9 @@ module FoodCritic
62
62
  'FC049' => 'Role name does not match containing file name',
63
63
  'FC050' => 'Name includes invalid characters',
64
64
  'FC051' => 'Template partials loop indefinitely',
65
+ 'FC052' => 'Metadata uses the unimplemented "suggests" keyword',
66
+ 'FC053' => 'Metadata uses the unimplemented "recommends" keyword',
67
+ # FC054 was yanked and is considered reserved, do not reuse it
65
68
  'FCTEST001' => 'Test Rule'
66
69
  }
67
70
 
@@ -183,6 +186,9 @@ module FoodCritic
183
186
  {:short => 'f', :long => 'epic-fail TAGS',
184
187
  :description => "Fail the build based on tags. Use 'any' to fail on all warnings."},
185
188
 
189
+ {:short => 'l', :long => 'list',
190
+ :description => 'List all enabled rules and their descriptions.'},
191
+
186
192
  {:short => 't', :long => 'tags TAGS',
187
193
  :description => 'Check against (or exclude ~) rules with the specified tags.'},
188
194
 
@@ -273,9 +279,9 @@ module FoodCritic
273
279
  #
274
280
  # @param [Array] cmd_args The command line arguments.
275
281
  def run_lint(cmd_args)
276
- in_current_dir do
282
+ cd '.' do
277
283
  show_context = cmd_args.include?('-C')
278
- review, @status = FoodCritic::Linter.check(CommandLine.new(cmd_args))
284
+ review, @status = FoodCritic::Linter.run(CommandLine.new(cmd_args))
279
285
  @review =
280
286
  if review.nil? || (review.respond_to?(:warnings) && review.warnings.empty?)
281
287
  ''
@@ -1,6 +1,10 @@
1
- require 'simplecov'
2
- SimpleCov.start do
3
- add_filter '/features/'
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/features/'
5
+ end
6
+ rescue LoadError
7
+ warn 'warning: simplecov gem not found; skipping coverage'
4
8
  end
5
9
 
6
10
  require 'aruba/cucumber'
@@ -396,8 +396,7 @@ module FoodCritic
396
396
  doc, xml_node = xml_document(doc, xml_node)
397
397
 
398
398
  if node.respond_to?(:each)
399
- # First child is the node name
400
- node.drop(1).each do |child|
399
+ node.each do |child|
401
400
  if position_node?(child)
402
401
  xml_position_node(doc, xml_node, child)
403
402
  else
@@ -493,7 +492,7 @@ module FoodCritic
493
492
  source = if file.to_s.split(File::SEPARATOR).include?('templates')
494
493
  template_expressions_only(file)
495
494
  else
496
- File.read(file)
495
+ File.read(file).encode('utf-8', 'binary', :undef => :replace)
497
496
  end
498
497
  build_xml(Ripper::SexpBuilder.new(source).parse)
499
498
  end
@@ -550,7 +549,9 @@ module FoodCritic
550
549
  end
551
550
 
552
551
  def template_expressions_only(file)
553
- exprs = Template::ExpressionExtractor.new.extract(File.read(file))
552
+ exprs = Template::ExpressionExtractor.new.extract(
553
+ File.read(file).encode('utf-8', 'binary', :undef => :replace)
554
+ )
554
555
  lines = Array.new(exprs.map { |e| e[:line] }.max || 0, '')
555
556
  exprs.each do |e|
556
557
  lines[e[:line] - 1] += ';' unless lines[e[:line] - 1].empty?
@@ -82,14 +82,10 @@ module FoodCritic
82
82
  end
83
83
 
84
84
  class Search
85
- # The search grammars that ship with any Chef gems installed locally.
86
- # These are returned in descending version order (a newer Chef version
87
- # could break our ability to load the grammar).
88
- # Grammars are not available from Chef 11+.
85
+ # lucene.treetop used to be provided by chef gem
86
+ # We're keeping a local copy from chef 10.x
89
87
  def chef_search_grammars
90
- Gem.path.map do |gem_path|
91
- Dir["#{gem_path}/gems/chef-*/**/lucene.treetop"]
92
- end.flatten.sort.reverse
88
+ [File.expand_path('../../..', __FILE__) + "/misc/lucene.treetop"]
93
89
  end
94
90
 
95
91
  # Create the search parser from the first loadable grammar.
@@ -23,6 +23,10 @@ module FoodCritic
23
23
  'specified tags.') do |t|
24
24
  @options[:tags] << t
25
25
  end
26
+ opts.on('-l', '--list',
27
+ 'List all enabled rules and their descriptions.') do |t|
28
+ @options[:list] = t
29
+ end
26
30
  opts.on('-f', '--epic-fail TAGS',
27
31
  "Fail the build based on tags. Use 'any' to fail "\
28
32
  'on all warnings.') do |t|
@@ -55,6 +59,10 @@ module FoodCritic
55
59
  'foodcritic/rules/**/*.rb') do |g|
56
60
  @options[:search_gems] = true
57
61
  end
62
+ opts.on("-P", "--progress",
63
+ "Show progress of files being checked") do
64
+ @options[:progress] = true
65
+ end
58
66
  opts.on('-R', '--role-path PATH',
59
67
  'Role path(s) to check.') do |r|
60
68
  @options[:role_paths] << r
@@ -106,6 +114,13 @@ module FoodCritic
106
114
  @options.key?(:version)
107
115
  end
108
116
 
117
+ # Just list enabled rules, don't actually run a lint check?
118
+ #
119
+ # @return [Boolean] True if a rule listing is requested.
120
+ def list_rules?
121
+ @options.key?(:list)
122
+ end
123
+
109
124
  # The version string.
110
125
  #
111
126
  # @return [String] Current installed version.
@@ -25,6 +25,20 @@ module FoodCritic
25
25
  end
26
26
  end
27
27
 
28
+ # Simple container for rule listings.
29
+ class RuleList
30
+ attr_reader :rules
31
+ def initialize(rules = [])
32
+ @rules = rules
33
+ end
34
+
35
+ def to_s
36
+ @rules.sort { |a,b| a.code <=> b.code }.
37
+ map { |r| r.to_s }.join("\n")
38
+ end
39
+
40
+ end
41
+
28
42
  # The collected warnings (if any) raised against a cookbook tree.
29
43
  class Review
30
44
  attr_reader :cookbook_paths, :warnings
@@ -10,18 +10,21 @@ module FoodCritic
10
10
 
11
11
  # The default version that will be used to determine relevant rules. This
12
12
  # can be over-ridden at the command line with the `--chef-version` option.
13
- DEFAULT_CHEF_VERSION = '11.10.4'
13
+ DEFAULT_CHEF_VERSION = '12.4.1'
14
14
  attr_reader :chef_version
15
15
 
16
16
  # Perform a lint check. This method is intended for use by the command-line
17
17
  # wrapper. If you are programatically using foodcritic you should use
18
18
  # `#check` below.
19
- def self.check(cmd_line)
19
+ def self.run(cmd_line)
20
20
  # The first item is the string output, the second is exit code.
21
21
  return [cmd_line.help, 0] if cmd_line.show_help?
22
22
  return [cmd_line.version, 0] if cmd_line.show_version?
23
23
  if !cmd_line.valid_grammar?
24
24
  [cmd_line.help, 4]
25
+ elsif cmd_line.list_rules?
26
+ listing = FoodCritic::Linter.new.list(cmd_line.options)
27
+ [listing, 0]
25
28
  elsif cmd_line.valid_paths?
26
29
  review = FoodCritic::Linter.new.check(cmd_line.options)
27
30
  [review, review.failed? ? 3 : 0]
@@ -30,6 +33,26 @@ module FoodCritic
30
33
  end
31
34
  end
32
35
 
36
+ # List the rules that are currently in effect.
37
+ #
38
+ # The `options` are a hash where the valid keys are:
39
+ #
40
+ # * `:include_rules` - Paths to additional rules to apply
41
+ # * `:search_gems - If true then search for custom rules in installed gems.
42
+ # * `:tags` - The tags to filter rules based on
43
+ def list(options = {})
44
+ options = setup_defaults(options)
45
+ @options = options
46
+ load_rules
47
+
48
+ if options[:tags].any?
49
+ @rules = active_rules(options[:tags])
50
+ end
51
+
52
+ RuleList.new(@rules)
53
+ end
54
+
55
+
33
56
  # Review the cookbooks at the provided path, identifying potential
34
57
  # improvements.
35
58
  #
@@ -53,16 +76,23 @@ module FoodCritic
53
76
  paths = specified_paths!(options)
54
77
 
55
78
  # Loop through each file to be processed and apply the rules
56
- files_to_process(paths).each do |p|
79
+ files = files_to_process(paths)
57
80
 
81
+ if options[:progress]
82
+ puts "Food Critic"
83
+ puts "Checking #{files.count} files"
84
+ end
85
+
86
+ files.each do |p|
58
87
  relevant_tags = if options[:tags].any?
59
88
  options[:tags]
60
89
  else
61
90
  cookbook_tags(p[:filename])
62
91
  end
63
92
 
64
- active_rules(relevant_tags).each do |rule|
93
+ progress = '.'
65
94
 
95
+ active_rules(relevant_tags).each do |rule|
66
96
  state = {
67
97
  path_type: p[:path_type],
68
98
  file: p[:filename],
@@ -79,6 +109,8 @@ module FoodCritic
79
109
 
80
110
  matches = remove_ignored(matches, state[:rule], state[:file])
81
111
 
112
+ progress = 'x' if matches.any?
113
+
82
114
  # Convert the matches into warnings
83
115
  matches.each do |match|
84
116
  warnings << Warning.new(state[:rule],
@@ -87,9 +119,14 @@ module FoodCritic
87
119
  matched_rule_tags << state[:rule].tags
88
120
  end
89
121
  end
122
+
123
+ putc progress if options[:progress]
124
+
90
125
  last_dir = cookbook_dir(p[:filename])
91
126
  end
92
127
 
128
+ puts '' if options[:progress]
129
+
93
130
  Review.new(paths, warnings)
94
131
  end
95
132
 
@@ -141,6 +178,7 @@ module FoodCritic
141
178
  matches.reject do |m|
142
179
  matched_file = m[:filename] || file
143
180
  (line = m[:line]) && File.exist?(matched_file) &&
181
+ !File.directory?(matched_file) &&
144
182
  ignore_line_match?(File.readlines(matched_file)[line - 1], rule)
145
183
  end
146
184
  end
@@ -152,6 +190,8 @@ module FoodCritic
152
190
  else
153
191
  false
154
192
  end
193
+ rescue
194
+ false
155
195
  end
156
196
 
157
197
  # Some rules are version specific.