Created
November 11, 2014 20:18
-
-
Save djburdick/5104d15f612c15dde65f to your computer and use it in GitHub Desktop.
Run rubocop with circleci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In spec_helper.rb | |
RSpec.configure do |config| | |
.... | |
rubocop_output = `rubocop` | |
print rubocop_output | |
fail "RuboCop Errors" unless rubocop_output.match(/files inspected, no offenses detected/) | |
end | |
# In .rubocop.yml | |
# Make sure to exclude unused dirs or it'll run 10,000+ files in circleci | |
AllCops: | |
Exclude: | |
- "db/**/*" | |
- "tmp/**/*" | |
- "vendor/**/*" | |
- "bin/**/*" | |
- "log/**/*" | |
This version worked better for me.
require "spec_helper"
RSpec.describe "Check that the files we have changed have correct syntax" do
before do
current_sha = `git rev-parse --verify HEAD`.strip!
files = `git diff master #{current_sha} --name-only | grep .rb`
files.tr!("\n", " ")
@report = `rubocop #{files}`
puts "Report: #{@report}"
end
it { expect(@report.match("Offenses")).to be_nil, "Rubocop offenses found.\n#{@report}" }
end
I spent a while getting this to my liking. I thought that linting did not belong inside RSpec. By splitting it out, one is able to identify that the linter specifically failed via CircleCI steps, as well as leveraging parallelism without having to spin up RSpec for all linters.
I was inspired by this article http://takemikami.com/2018/01/30/RubocopPullRequestCI.html, though I had to make significant changes in syntax to make it work on CircleCI 2.0
Take a look! https://gist.github.com/xhocquet/4e07f430efd186ab69828961bdd7b7a3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @stabenfeldt for the excellent snippet. Below is an adaptation that compares the current branch with
origin/master
to avoid running into issues if themaster
branch is not present in the test environment.