Lint your Ruby code with Overcommit and static analysis tools

Using Git hooks to control code quality for Ruby, Rails and Chef

What is Linter?

Linter is a static analysis tool such as RuboCop. It checks the written code and makes suggestions based on pre-defined rules by community-driven coding style guides and common idioms.

Why linting and following style guides is important?

There are many reasons to lint the code:

  1. It helps catch unnecessary code
  2. It helps avoid conflicts between developers and also to remember about the conventions
  3. It helps optimize performance and avoid problems with security

Don’t forget to lint with Git hooks and Overcommit

Installation

gem install overcommit
gem ‘overcommit’
touch .overcommit.yml
example of .overcommit.yml file
overcommit --install
overcommit --sign

Adding RuboCop to your project

gem ‘rubocop’, require: false
rubocop
rubocop -a
touch .rubocop.yml
example of .rubocop.yml file
PreCommit:
RuboCop:
enabled: true

Most useful tools to linting of Ruby code

Fasterer

It is well known that the Ruby language not the fastest. Fasterer helps us to make our code faster by some speed improvements based on fast-ruby.

gem ‘fasterer’
fasterer
touch .fasterer.yml
example of .fasterer.yml file
PreCommit:
Fasterer:
enabled: true

bundler-audit

Check Gemfile.lock for installation from non-secured connections and the presence of versions of gems with vulnerabilities.

gem ‘bundler-audit’
bundle-audit
PreCommit:
BundleAudit:
enabled: true

Reek

gem ‘reek’
touch config.reek
example of config.reek file
PreCommit:
Reek:
enabled: true

Static analysis tools for Rails applications

rails_best_practices

gem ‘rails_best_practices’
rails_best_practices .
rails_best_practices -f html .
PreCommit:
RailsBestPractices:
enabled: true

Brakeman

gem ‘brakeman’, require: false
PrePush:
Brakeman:
enabled: true

rails_schema_up_to_date pre-commit hook

Overcommit includes useful option which can helps check to see whether the schema file is in line with the migrations.

PreCommit:
RailsSchemaUpToDate:
enabled: true

Linting for Chef

Monolithic repo mode

Add to Gemfile of Chef project:

gem ‘foodcritic’
PreCommit:
Foodcritic:
enabled: true
cookbooks_directory: ‘site-cookbooks’
environments_directory: ‘environments’
roles_directory: ‘roles’
include:
- ‘site-cookbooks/**/*’
- ‘environments/**/*’
- ‘roles/**/*’

Single cookbook repo mode

The default. Use this if your repository contains just a single cookbook. To get this to work well, you need to write into configuration something like:

PreCommit:
Foodcritic:
enabled: true
include:
— ‘attributes/**/*’
— ‘definitions/**/*’
— ‘files/**/*’
— ‘libraries/**/*’
— ‘providers/**/*’
— ‘recipes/**/*’
— ‘resources/**/*’
— ‘templates/**/*’

berksfile_check pre-commit hook

If you are using Berkshelf to manage cookbooks, it option can check that local Berksfile.lock matches Berksfile when either changes.

PreCommit:
BerksfileCheck:
enabled: true

Other tools

https://github.com/whitesmith/rubycritic — RubyCritic is a gem that wraps around static analysis gems such as Reek, Flay and Flog to provide a quality report of your Ruby code.

Software Engineer. Interested in Full-Stack Development and DevOps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store