RuboCop in the pipeline
The first run
RuboCop is the Ruby linter: it reads the style guide and points at code that will annoy the next reader. The first run on the course project is the normal first run on any project: it fails.
Install the tool
gem install rubocop
13 gems installed
Run it in the project folder
rubocop
Inspecting 4 files
CCCC
4 files inspected, 26 offenses detected, 25 offenses autocorrectable
Four letters, one per file: C means offences, a dot means clean. All 26 are style, none is a bug, and 25 of them RuboCop can fix by itself. The cops behind them:
- Style/StringLiterals: the course writes double quotes, the default wants single, which is 19 of the 26.
- Style/StringLiteralsInInterpolation: the same question inside #{...}, twice.
- Style/FrozenStringLiteralComment: the four files have no frozen_string_literal comment, and RuboCop marks that correction unsafe.
- Style/Documentation: on class Todo, which has no comment above it.
The pipeline task from lesson 13.4 now stops at the lint step:
bundle exec rake lint
rake aborted!
Command failed with status (1): [rubocop]
That is the gate working. A pipeline must not go green on a project with 26 offences, so the next four sections make the run clean before the lint task joins the pipeline.
Put RuboCop in the Gemfile
The command above worked because this image has RuboCop installed globally. The project does not know about it: with the local bundle from the previous lesson, rubocop would not be found at all. A tool that the pipeline runs belongs in the Gemfile, and it belongs in a group the test image can leave out:
source "https://rubygems.org" gem "minitest", "~> 5.20" gem "minitest-reporters", "~> 1.6" gem "rake", "~> 13.0" group :development do gem "rubocop", require: false end
require: false matters: Bundler then installs the gem without loading it at boot, which keeps the test run free of a tool that nobody needs while tests run. The development group matters more: the test image in two lessons (Ruby in Docker images) installs the bundle without it, so RuboCop and its dependencies never enter the image.
The config file
Two of the offences are decisions, not mistakes: this course uses double quotes and does not want a frozen-string comment in every file. Write the decisions down in .rubocop.yml; the rest of the file switches off two cops that do not fit a teaching project.
AllCops: TargetRubyVersion: 3.4 NewCops: enable SuggestExtensions: false Style/StringLiterals: EnforcedStyle: double_quotes Style/StringLiteralsInInterpolation: EnforcedStyle: double_quotes Style/FrozenStringLiteralComment: Enabled: false Style/Documentation: Enabled: false Metrics/MethodLength: Max: 15
TargetRubyVersion tells the parser which syntax to accept. NewCops: enable turns on checks added in newer releases so the editor and the pipeline agree, and SuggestExtensions: false silences the tip about extensions for a project that does not use Rails. The last three blocks are the project's own choices, written where the next reader can see them.
Fix the code
The config leaves one offence, and it is a real simplification: the empty string in the interpolation. The list method builds the same string with a conditional expression, and while the file is open the Rakefile uses the symbol array form for its two-name list:
def list @items.each_with_index.map do |item, index| "#{index + 1}. #{item[:title]}#{" (done)" if item[:done]}" end end
desc "Run the whole pipeline" task ci: %i[test lint]
The tests still pass after the edit: the string is identical for a done and an undone item, and the expression says what the ternary said with less punctuation.
The clean run
Lint alone
bundle exec rubocop
Inspecting 4 files
....
4 files inspected, no offenses detected
The pipeline task
bundle exec rake ci
Run options: --seed 40531
# Running:
...
Finished in 0.001516s, 1979.0916 runs/s, 2638.7887 assertions/s.
3 runs, 4 assertions, 0 failures, 0 errors, 0 skips
rubocop
Inspecting 4 files
....
4 files inspected, no offenses detected
The test task runs first, the lint task second, and Rake stops at the first failure. The two clean outputs above are the whole pipeline: tests and style, one command.
What to watch for
Turn the linter on in the pipeline only after the project is clean.
rubocop --auto-correct fixes most style offences
in one pass; read the diff before committing it, because a whitespace fix
can hide a behaviour change in a long line.
Keep the config in the repository and pin the gem. A new RuboCop release
adds cops, and NewCops: enable makes that
intentional: the pipeline goes red, a person reads the new rule and
either fixes the code or writes the exception down.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
In a RuboCop summary, what does a
Cmean in the lineCCCC?Answer
The file has at least one offence, while a dot means it is clean
One character per inspected file, so four letters out of four mean every file has something to report.
-
What does the
[Correctable]tag on an offence mean?Answer
RuboCop can rewrite the code to fix it, and the diff is worth reading first
The autocorrect pass changes files in place, which is why the page says to read the diff before committing it.
-
What does this program print?
p %i[test lint]Answer
[:test, :lint]
The i form builds an array of symbols, which is the shape Style/SymbolArray asks for a list of names.
-
What does this program print?
item = { done: true } p "1. Buy milk#{" (done)" if item[:done]}"Answer
"1. Buy milk (done)"
An interpolation of nil adds nothing, so the conditional expression writes the marker exactly when the item is done.
-
What does
NewCops: enablein .rubocop.yml do?Answer
It turns on the cops added in newer releases, so the editor and the pipeline agree
Without it a new cop is pending and does not run, which is how a rule arrives as an intentional change rather than as a surprise.
-
What does this program print?
require "yaml" config = YAML.load("AllCops:\n TargetRubyVersion: 3.4\n") p config["AllCops"]["TargetRubyVersion"]Answer
3.4
The unquoted value is a YAML number and comes back as a Float, which p prints without quotes.
-
Why does the linter belong in the Gemfile rather than a global install?
Answer
So the version the project uses is written down and every machine installs the same one
The project pins its tools, and require: false keeps the gem out of the boot path at run time.
-
What does this program print?
p %w[test lint]Answer
["test", "lint"]
The w form builds an array of strings, the other shortcut a Rakefile or a list of file names uses.
-
Why read the diff of
rubocop --auto-correctbefore committing it?Answer
A whitespace or quote change can hide a behaviour change in a long line
The correction is mechanical but it moves code, so a person reads it once rather than trusting the tool blindly.
-
When should the lint step join the pipeline?
Answer
After the project is clean, so the gate is about new code and not about a backlog
A gate that is red from the start is ignored; clean first, then the pipeline keeps it clean.
Article author: Arthur Isaev