About Foodcritic

[edit on GitHub]

Use Foodcritic to check cookbooks for common problems:

  • Style
  • Correctness
  • Syntax
  • Best practices
  • Common mistakes
  • Deprecations

Foodcritic looks for lint-like behavior and reports it!

Foodcritic is a static linting tool that analyzes all of the Ruby code that is authored in a cookbook against a number of rules, and then returns a list of violations. Because Foodcritic is a static linting tool, using it is fast. The code in a cookbook is read, broken down, and then compared to Foodcritic rules. The code is not run (a chef-client run does not occur). Foodcritic does not validate the intention of a recipe, rather it evaluates the structure of the code, and helps enforce specific behavior, detect portability of recipes, identify potential run-time failures, and spot common anti-patterns.

When Foodcritic returns a violation, this does not automatically mean the code needs to be changed. It is important to first understand the intention of the rule before making the changes it suggests. For example, rule FC003 describes a scenario where a recipe uses the search method in the Recipe DSL to retrieve data from the Chef server. Rule FC003 may suggest that a cookbook will raise an error if that cookbook is run in a situation where a Chef server is not present. Adopting this rule is only necessary when chef-solo is part of the team’s workflow (because chef-solo does not use a Chef server).

Run Foodcritic

Foodcritic is run from the command line, typically against a single cookbook and all of the Ruby files contained within it:

$ foodcritic /path/to/cookbook

Foodcritic may also be run from the root of an individual cookbook directory:

$ foodcritic .

Foodcritic returns a list, via standard output, that shows the results of the evaluation:

FC003: Check whether you are running with chef server before using server-specific features: ./recipes/ip-logger.rb:1
FC008: Generated cookbook metadata needs updating: ./metadata.rb:2
FC008: Generated cookbook metadata needs updating: ./metadata.rb:3

Output

Foodcritic output:

  • States a Foodcritic rule. For example: FC003
  • Describes the rule, based on the results of the evaluation. For example: Check whether you are running with chef server before using server-specific features
  • Specifies the file path. For example: ./recipes/ip-logger.rb
  • Specifies the line number. For example: 1

A Foodcritic evaluation has the following syntax:

RULENUMBER: MESSAGE: FILEPATH:LINENUMBER

For example:

FC008: Generated cookbook metadata needs updating: ./metadata.rb:3

Rules

A complete list of Foodcritic rules are available on the Foodcritic website.

Custom Rules

The following rules for Foodcritic have been developed by the Chef community:

Exclude Rules

Run the following command to exclude a Foodcritic rule:

$ foodcritic . --tags ~RULE

For example, to exclude rule FC003:

$ foodcritic . --tags ~FC003

Foodcritic CLI

The foodcritic command is used to run Foodcritic against one (or more) cookbooks.

This command has the following syntax:

$ foodcritic COOKBOOK_PATH

This command has the following options:

-t TAGS, --tags TAGS
Use to specify tags to include or exclude when running Foodcritic.
-l, --list
List the name and description of all rules.
-f TAGS, --epic-fail TAGS
Use to trigger a build failure if any of the specified tags are matched.
-c VERSION, --chef-version VERSION
Use to specify the chef version to evaluate against instead of Foodcritic’s default.
-B PATH, --cookbook-path PATH
Use to specify the path to a cookbook to check
-C, --[no-]context
Use to show lines matched against Foodcritic rules, rather than the default summary.
-I PATH, --include PATH
Use to specify the path to a file that contains additional Foodcritic rules.
-r, --[no-]repl
Use to drop into a REPL for interactive rule editing.
-S PATH, --search-grammar PATH
Use to specify the path to a file that contains additional grammar used when validating search syntax.
-V, --version
Use to display the version of Foodcritic.

For more information …

For more information about Foodcritic: