Class: Overcommit::Hook::PreCommit::Foodcritic

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/foodcritic.rb

Overview

Runs ‘foodcritic` against any modified Ruby files from Chef directory structure.

There are two “modes” you can run this hook in based on the repo:

SINGLE COOKBOOK REPO MODE


The default. Use this if your repository contains just a single cookbook, i.e. the top-level repo directory contains directories called ‘attributes`, `libraries`, `recipes`, etc.

To get this to work well, you’ll want to set your Overcommit configuration for this hook to something like:

PreCommit:

Foodcritic:
  enabled: true
  include:
    - 'attributes/**/*'
    - 'definitions/**/*'
    - 'files/**/*'
    - 'libraries/**/*'
    - 'providers/**/*'
    - 'recipes/**/*'
    - 'resources/**/*'
    - 'templates/**/*'

MONOLITHIC REPO MODE


Use this if you store multiple cookbooks, environments, and roles (or any combination thereof) in a single repository.

There are three configuration options relevant here:

* `cookbooks_directory`
  When set, hook will treat the path as a directory containing cookbooks.
  Each subdirectory of this directory will be treated as a separate
  cookbook.

* `environments_directory`
  When set, hook will treat the path as a directory containing environment
  files.

* `roles_directory`
  When set, hook will treat the given path as a directory containing role
  files.

In order to run in monolithic repo mode, YOU MUST SET ‘cookbooks_directory`. The other configuration options are optional, if you happen to store environments/roles in another repo.

To get this to work well, you’ll want to set your Overcommit configuration for this hook to something like:

PreCommit:

Foodcritic:
  enabled: true
  cookbooks_directory: 'cookbooks'
  environments_directory: 'environments'
  roles_directory: 'roles'
  include:
    - 'cookbooks/**/*'
    - 'environments/**/*'
    - 'roles/**/*'

ADDITIONAL CONFIGURATION


You can disable rules using the ‘flags` hook option. For example:

PreCommit:

Foodcritic:
  enabled: true
  ...
  flags:
    - '--epic-fail=any'
    - '-t~FC011' # Missing README in markdown format
    - '-t~FC064' # Ensure issues_url is set in metadata

Any other command line flag supported by the ‘foodcritic` executable can be specified here.

If you want the hook run to fail (and not just warn), set the ‘on_warn` option for the hook to `fail`:

PreCommit:

Foodcritic:
  enabled: true
  on_warn: fail
  ...

This will treat any warnings as failures and cause the hook to exit unsuccessfully.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#runObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/overcommit/hook/pre_commit/foodcritic.rb', line 98

def run
  args = modified_cookbooks_args + modified_environments_args + modified_roles_args
  result = execute(command, args: args)

  if result.success?
    :pass
  else
    [:warn, result.stderr + result.stdout]
  end
end