Using RuboCop for quick automated code reviews with Docker

With RuboCop, you can carry out a static analysis of Ruby code and automatically check several contents before assigning your code review. You can install RuboCop via RubyGems, so it’s quite easy, but some users may not want to change their local environment. If so, we’ll show you how to use Docker so that RuboCop does not rely on the local environment.

About Docker

Most of you already know Docker. Simply put, it’s a Linux container platform. It doesn’t virtualize the whole OS; rather, it creates a virtual environment (called container) only with the minimum required library and application components. Therefore, it’s way lighter than virtualizing the whole OS, and being ready to use in just a few moments is another benefit. Basically, each process is protected in the virtual environment, so your resources are kept safe.

Image for post

Originally a Linux program, Docker is now also available for Windows and macOS. Here you can download (Windows / macOS) and install the appropriate version for your OS.

Testing RuboCop

So let’s see how it works. Go to the Ruby project you want to review and issue the following command: We’ll use the cagedata/rubocop — Docker Hub container. Ruby version: 2.3.

$ docker run --rm --volume "$PWD:/app" cagedata/rubocop

If this is your first time, you must download the container image. This may take several minutes. After downloading and deploying it, the following run results will be displayed:

$ docker run --rm --volume "$PWD:/app" cagedata/rubocop
Inspecting 35 files
.CCCCC.C...CWCWWCCCCWWCCCWWW..WWCCC

Offenses:

ncmb-ruby-client.gemspec:12:24: C: Use %q only for strings that contain both single quotes and double quotes.
  spec.description   = %q(A simple Ruby client for the nifty cloud mobile backend REST API)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  :

Now that you have the container image, it will be as fast as issuing RuboCop commands locally.

Version upgrade

This  (Docker settings file) is as follows:

FROM ruby:2.3
MAINTAINER "Dave Long <dlong@cagedata.com>"

ARG version=0.43.0

RUN gem install rubocop -v ${version}

WORKDIR /app
VOLUME /app

COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

RuboCop version 0.43.0 is required. If there is a version upgrade, you’ll have to manually install it and rebuild the Docker container.

This can be done easily, so give it a try.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.