Chef Cookbook Code Quality With Foodcritic

This is the sixth section of a series on the development of Chef Broiler Plate in which we go over setting up a robust, TDD framework for Chef cookbook development.

Last time, we created a sample "Message of the Day" cookbook in a test driven way. In this short chapter, we will add Foodcritic to test the code quality of our cookbooks. More subtle than outright functionality tests, Foodcritic will reference some leading best practice rules to ensure your cookbooks are squeaky clean.

Add the Foodcritic Gem

We'll start by adding a line to our Gemfile:

gem "foodcritic", "2.1.0"

Use bundle update to update our Gems.

Creating a Rake Task

Let's add a Rake task to execute the Foodcritic against any of our cookbooks:

desc "Runs foodcritic against all the cookbooks."
task :foodcritic do
  sh "bundle exec foodcritic -f any cookbooks"

Follow the Rules!

I like the idea of adding the Custom Ink and Etsy rule repositories as detailed here. We should do that by adding some Git submodules and updating our ' file (to add clear install instructions):

git submodule add git:// test/foodcritic/customink
git submodule add git:// test/foodcritic/etsy

Next, update the one command line in the foodcritic Rake task to add the delectably stricter rules!

bundle exec foodcritic -I test/foodcritic/* -f any cookbooks

Lastly, we would need to add a line to our travis file to support the submodules:

  - git submodule update --init --recursive

Now, whenever we push code to our repository, Travis will kick off a review of our cookbook quality. Many thanks to the guys over at Custom Ink for writing a great article that helped me write this one.

Coming up…

In the next installment, we will use Knife to continue testing our cookbooks and building out the framework.

If you enjoyed reading this or learned something, please consider sharing via , , or . Thanks!