Run RSpec, Brakeman, Rubocop, Rails Best Practices with overcommit

Why did you decide to use overcommit

--I want to execute tests and static analysis automatically --You can use git hooks to execute a specific script at the timing of commit, push, etc. --The contents of git hooks cannot be managed by git --Overcommit allows you to manage and extend git hooks

Premise

--Rspec (test automation) --Brakeman (security check) --Rubocop (Ruby code analysis) --RailsBestPractices (Rails code analysis)

I will write on the assumption that you have already installed and are ready to type commands manually. If you have a tool that you are not using, ignore the settings in that part.

Introduction

Gemfile


group :development do
  gem 'overcommit'
$ bundle install
$ overcommit --install #Generate configuration file

.overcommit.yml


#Disable unnecessary checks
CommitMsg:
  ALL:
    enabled: false
PreCommit:
  AuthorEmail:
    enabled: false
  AuthorName:
    enabled: false
  BrokenSymlinks:
    enabled: false
  CaseConflicts:
    enabled: false
  MergeConflicts:
    enabled: false
#Required check settings
  RuboCop:
    enabled: true
  RailsBestPractices:
    enabled: true
PrePush:
  Brakeman:
    enabled: true
  RSpec:
    enabled: true
$ overcommit --sign #Reflect the setting change

Default settings is applied except for the items explicitly set in the configuration file.

Operation check

The following is an example of the operation check method. Rubocop Embed Time.now in a suitable controller and confirm that it cannot be committed RailsBestPractices Set default_scope order (created_at:: desc) to an appropriate model and confirm that you cannot commit Brakeman Set http_basic_authenticate_with name:'ID', password:'password' to the appropriate controller and confirm that Push is not possible. RSpec Write a test that doesn't pass and make sure you can't push

Where I was addicted

When I run RSpec manually, it passes, but the pre-push check fails and I get the following message:

NameError:
  uninitialized constant Capybara::DSL

When I hit it manually, I'm running bin / rspec, but when I'm pre-pushing, I'm running bundle exec rspec, so it seems that the behavior is slightly different. Solved by adding the following to spec_helper.rb.

spec/spec_helper.rb


RSpec.configure do |config|
  #Added the following 3 lines
  ENV['RAILS_ENV'] ||= 'test'
  require File.expand_path('../config/environment', __dir__)
  require 'rspec/rails'

reference

Recommended Posts

Run RSpec, Brakeman, Rubocop, Rails Best Practices with overcommit
[Rails] Test with RSpec
Run Ruby on Rails RSpec tests with GitHub Actions
Run Rails whenever with docker
Let's unit test with [rails] Rspec!
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
Run SystemSpec (RSpec) and Rubocop on CircleCI
[Ruby on Rails] Model test with RSpec
I rewrote the Rails tutorial test with RSpec
Introducing Rspec with Ruby on Rails x Docker
Rails beginners tried to get started with RSpec
[Ruby / Rails] Inactivate Lint of update_all with Rubocop