Knife cookbook test says ruby error but does not show it


Hello boys and girls ,

This is more like of a malfunction in “knife” , because the command “knife cookbook test my-cookbook” does this :

[root@chef-learning]# knife  cookbook test  my-cookbook
WARNING: DEPRECATED: Please use ChefSpec or Cookstyle to syntax-check cookbooks.
checking my-cookbook
Running syntax check on my-cookbook
Validating ruby files
FATAL: Cookbook file recipes/default.rb has a ruby syntax error:

I can deffinetly see the errors with “ruby -c cookbooks/my-cookbook/recipes/default.rb” , and i can also see it with " cookstyle cookbooks/my-cookbook" ,

SO far so good , but the thing is that " cookstyle cookbooks/my-cookbook" shows too may offenses besides the errors that ruby sees , and those extra offenses do not stop me to upload the cookbook , nor do they stop me from fine running my recipes

Examples of meaningless offenses :
“Extra blank line detected” , “Tab detected” , “Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.” , “Extra blank line detected”

I am too lazy to run “ruby -c $PATH_OF_FILE” every time an error occurs in my “default.rb” script , how can I overcome this ?


As noted, that command is deprecated - there is no malfunction, we’re actively getting rid of that sub-command as it’s outlived it’s usefulness. This predated a time when we had things like Foodcritic or Cookstyle.

Cookstyle and Foodcritic are lint and style tools, the offenses aren’t inherently meaningless but they are opinionated as that is very much the point of this class of tooling. Most tooling along these lines will require that Ruby syntax is valid and so also catch that. These offenses don’t stop uploading a cookbook because a valid cookbook doesn’t have to pass lint/style checks, though it’s what we’d recommend. This means you can catch lint, style, and syntax errors between these tools well before upload.

We’d heartily recommend running both Cookstyle and Foodcritic against your cookbooks, both of which allow which offenses you want to ignore - you can basically write your own specification. If you really don’t want to run either of those tools then you can run ruby -c as you’ve discovered. There isn’t really any “overcoming” this issue - if you don’t want to follow the best practices then you are welcome to accomplish this task however you feel is best.