From 0091c46ff785efd5736d1c90992dc3eb1aef1140 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Thu, 7 May 2020 11:58:21 +0100 Subject: [PATCH] Extend 'default' task with RuboCop linting This adds a new 'lint' rake task that runs as part of a default call to 'rake'. Having linting run locally by default makes it easier to catch build issues, and is a prerequisite for moving to GitHub Actions, since we want to avoid listing lots of separate build steps. Note that we first wipe the 'default' rake task, since RSpec has a habbit of auto-populating it, which makes it hard to define it explicitly. The 'lint' task runs RuboCop as a separate process, which is a technique to avoid a LoadError when the gem is unavailable in production. --- Rakefile | 9 +++++---- lib/tasks/lint.rake | 11 +++-------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Rakefile b/Rakefile index 08cc98f3..08b01ffe 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,7 @@ -# Add your own tasks in files placed in lib/tasks ending in .rake, -# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. - require File.expand_path("config/application", __dir__) - Rails.application.load_tasks + +# RSpec shoves itself into the default task without asking, which confuses the ordering. +# https://github.com/rspec/rspec-rails/blob/eb3377bca425f0d74b9f510dbb53b2a161080016/lib/rspec/rails/tasks/rspec.rake#L6 +Rake::Task[:default].clear unless Rails.env.production? +task default: %i[spec lint] diff --git a/lib/tasks/lint.rake b/lib/tasks/lint.rake index cdbbd79c..32553fe0 100644 --- a/lib/tasks/lint.rake +++ b/lib/tasks/lint.rake @@ -1,9 +1,4 @@ -unless Rails.env.production? - require "rubocop/rake_task" - - RuboCop::RakeTask.new(:lint) do |t| - t.patterns = %w[app config Gemfile lib spec] - t.formatters = %w[clang] - t.options = %w[--parallel] - end +desc "Lint Ruby" +task lint: :environment do + sh "bundle exec rubocop" end