Slide 1

Slide 1 text

Kyobashi.rb offline #2 2024.03.13 (Wed) Extracting from rubocop-rspec @ydah

Slide 2

Slide 2 text

ɾ Name: Yudai Takada ɾ GitHub: @ydah / X: @ydah_ ɾ Software Engineer at ANDPAD, Inc. ɾ Co-Founder of Kyobashi.rb ɾ Member of RuboCop RSpec team ɾ Contributor of Lrama self.inspect

Slide 3

Slide 3 text

Today’s talk

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

What is RuboCop RSpec? RSpec-speci fi c analysis for your projects, as an extension to RuboCop. Project Goals • Enforce the guidelines and best practices outlined in the community RSpec style guide • Simplify the process of adopting new RSpec functionality

Slide 7

Slide 7 text

Why do we extract it as a gem?

Slide 8

Slide 8 text

Department of RuboCop RSpec We have several departments: • RSpec • Rails (for rspec-rails) • FactoryBot (for factory_bot) • Capybara (for capybara)

Slide 9

Slide 9 text

Code style checking for RSpec files

Slide 10

Slide 10 text

rubocop-rspec#1523 RSpec/FactoryBot/FactoryNameStyle false positive on rails generators

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Motivation for splitting gems Capybara and factory_bot cops are not limited to RSpec and can also be used with other test frameworks. For example: • Test::Unit • Cucumber • Minitest • Minitest::Spec

Slide 13

Slide 13 text

Why extract the RSpec/Rails department as well?

Slide 14

Slide 14 text

rubocop-rspec#1823 RSpec/Rails/HaveHttpStatus auto fi x broke test suite

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Motivation for splitting gems The Rails department is not required for non- Rails products. For example: • Web applications using Sinatra • Not a web application.

Slide 17

Slide 17 text

Control department # .rubocop.yml RSpec: Enabled: false RSpec/Capybara: Enabled: true

Slide 18

Slide 18 text

Destination and Status Department Extract to Status RSpec/Capybara rubocop-capybara Extracted in v2.18.0 RSpec/FactoryBot rubocop-factory_bot Extracted in v2.22.0 RSpec/Rails rubocop-rspec_rails Next version?

Slide 19

Slide 19 text

Migration Guide

Slide 20

Slide 20 text

Only the RSpec department is enabled

Slide 21

Slide 21 text

You don't have to do anything!

Slide 22

Slide 22 text

After major version upgrade If there is a point where `Enabled: false` is set in .rubocop.yml at the time of major upgrade, it must be deleted. # .rubocop.yml RSpec/Capybara: Enabled: false RSpec/FactoryBot: Enabled: false RSpec/Rails: Enabled: false

Slide 23

Slide 23 text

If you are using a department other than the RSpec department

Slide 24

Slide 24 text

Three Steps for Migration 1. Add the destination gem to the Gem fi le 2. Add settings to .rubocop.yml 3. Replace the cop name with the new name in .rubocop.yml or .rubocop_todo.yml

Slide 25

Slide 25 text

Add destination gem to Gemfile We currently rely on the extracted gems for backwards compatibility, but this will be removed in the next major version update. # Gemf i le gem 'rubocop - capybara', require: false gem 'rubocop - factory_bot', require: false gem ‘rubocop - rspec_rails’, require: false

Slide 26

Slide 26 text

Add settings to .rubocop.yml You can't enable the RuboCop extension cop just by adding it to the Gemfile. You need to add a setting to .rubocop.yml to load the extension into RuboCop, like this: # .rubocop.yml require: - rubocop - capybara - rubocop - factory_bot - rubocop - rspec_rails

Slide 27

Slide 27 text

Replace the cop name We need to replace it with a new department. If we need to replace it, we get a warning like this: $ bundle exec rubocop -A .rubocop.yml: RSpec/Capybara/MatchStyle has the wrong namespace - should be Capybara .rubocop.yml: RSpec/FactoryBot/ConsistentParenthesesStyle has the wrong namespace - should be FactoryBot :

Slide 28

Slide 28 text

Replace the cop name Using the cop of the FactoryBot department as an example, you can change the cop name as follows: -RSpec/FactoryBot/AttributeDef i nedStatically -RSpec/FactoryBot/ConsistentParenthesesStyle -RSpec/FactoryBot/CreateList +FactoryBot/ConsistentParenthesesStyle +FactoryBot/AttributeDef i nedStatically +FactoryBot/CreateList

Slide 29

Slide 29 text

Replace the cop name .rubocop_todo.yml can be regenerated with the following command: $ bundle exec rubocop - - regenerate - todo

Slide 30

Slide 30 text

Upgrade is complete!

Slide 31

Slide 31 text

end