Skip to content

Commit

Permalink
RSpec -> FactoryBot
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Apr 13, 2023
1 parent b8409ac commit 9d0e37f
Show file tree
Hide file tree
Showing 39 changed files with 832 additions and 3,117 deletions.
8 changes: 2 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Metrics/BlockLength:

Naming/FileName:
Exclude:
- lib/rubocop-rspec.rb
- lib/rubocop-factory_bot.rb

Naming/InclusiveLanguage:
Enabled: true
Expand Down Expand Up @@ -123,10 +123,8 @@ Style/RedundantHeredocDelimiterQuotes:
Style/RedundantStringEscape:
Enabled: true

# Enable our own pending cops.
# Enable pending rubocop-rspec cops.

RSpec/BeEmpty:
Enabled: true
RSpec/BeEq:
Enabled: true
RSpec/BeNil:
Expand All @@ -145,8 +143,6 @@ RSpec/NoExpectationExample:
Enabled: true
RSpec/PendingWithoutReason:
Enabled: true
RSpec/RedundantAround:
Enabled: true
RSpec/SortMetadata:
Enabled: true
RSpec/SubjectDeclaration:
Expand Down
794 changes: 4 additions & 790 deletions CHANGELOG.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'rake'
gem 'rspec', '~> 3.11'
gem 'rubocop-performance', '~> 1.7'
gem 'rubocop-rake', '~> 0.6'
gem 'rubocop-rspec', '~> 2.18.1'
gem 'simplecov', '>= 0.19'
gem 'yard'

Expand Down
46 changes: 19 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,81 @@
# RuboCop RSpec
# RuboCop Factrory Bot

[![Join the chat at https://gitter.im/rubocop-rspec/Lobby](https://badges.gitter.im/rubocop-rspec/Lobby.svg)](https://gitter.im/rubocop-rspec/Lobby)
[![Gem Version](https://badge.fury.io/rb/rubocop-rspec.svg)](https://rubygems.org/gems/rubocop-rspec)
![CI](https://github.com/rubocop/rubocop-rspec/workflows/CI/badge.svg)
[![Gem Version](https://badge.fury.io/rb/rubocop-factory_bot.svg)](https://rubygems.org/gems/rubocop-factory_bot)
![CI](https://github.com/rubocop/rubocop-factory_bot/workflows/CI/badge.svg)

[RSpec](https://rspec.info/)-specific analysis for your projects, as an extension to
[Factory Bot](https://www.rubydoc.info/gems/factory_bot)-specific analysis for your projects, as an extension to
[RuboCop](https://github.com/rubocop/rubocop).

## Installation

Just install the `rubocop-rspec` gem
Just install the `rubocop-factory_bot` gem

```bash
gem install rubocop-rspec
gem install rubocop-factory_bot
```

or if you use bundler put this in your `Gemfile`

```
gem 'rubocop-rspec', require: false
gem 'rubocop-factory_bot', require: false
```

### Upgrading to RuboCop RSpec v2.x

Read all the details in our [Upgrade to Version 2.x](https://docs.rubocop.org/rubocop-rspec/2.0/upgrade_to_version_2.html) document.

## Usage

You need to tell RuboCop to load the RSpec extension. There are three
You need to tell RuboCop to load the Factory Bot extension. There are three
ways to do this:

### RuboCop configuration file

Put this into your `.rubocop.yml`.

```yaml
require: rubocop-rspec
require: rubocop-factory_bot
```
Alternatively, use the following array notation when specifying multiple extensions.
```yaml
require:
- rubocop-other-extension
- rubocop-rspec
- rubocop-factory_bot
```
Now you can run `rubocop` and it will automatically load the RuboCop RSpec
Now you can run `rubocop` and it will automatically load the RuboCop Factory Bot
cops together with the standard cops.

### Command line

```bash
rubocop --require rubocop-rspec
rubocop --require rubocop-factory_bot
```

### Rake task

```ruby
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rspec'
task.requires << 'rubocop-factory_bot'
end
```

### Code Climate

rubocop-rspec is available on Code Climate as part of the rubocop engine. [Learn More](https://marketing.codeclimate.com/changelog/55a433bbe30ba00852000fac/).

## Documentation

You can read more about RuboCop RSpec in its [official manual](https://docs.rubocop.org/rubocop-rspec).
You can read more about RuboCop Factory Bot in its [official manual](https://docs.rubocop.org/rubocop-factory_bot).

## The Cops

All cops are located under
[`lib/rubocop/cop/rspec`](lib/rubocop/cop/rspec), and contain
[`lib/rubocop/cop/factory_bot`](lib/rubocop/cop/factory_bot), and contain
examples/documentation.

In your `.rubocop.yml`, you may treat the RSpec cops just like any other
In your `.rubocop.yml`, you may treat the Factory Bot cops just like any other
cop. For example:

```yaml
RSpec/FilePath:
FactoryBot/AttributeDefinedStatically:
Exclude:
- spec/my_poorly_named_spec_file.rb
- spec/factories/my_factory.rb
```

## Contributing
Expand All @@ -92,5 +84,5 @@ Checkout the [contribution guidelines](.github/CONTRIBUTING.md).

## License

`rubocop-rspec` is MIT licensed. [See the accompanying file](MIT-LICENSE.md) for
`rubocop-factory_bot` is MIT licensed. [See the accompanying file](MIT-LICENSE.md) for
the full text.
29 changes: 13 additions & 16 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ desc 'Build config/default.yml'
task :build_config do
require 'yard'

require 'rubocop-rspec'
require 'rubocop/rspec/config_formatter'
require 'rubocop/rspec/description_extractor'

glob = File.join('lib', 'rubocop', 'cop', 'rspec',
'{,capybara,factory_bot,rails}', '*.rb')
# Due to YARD's sensitivity to file require order (as of 0.9.25),
# we have to prepend the list with our base cop, RuboCop::Cop::RSpec::Base.
# Otherwise, cop's parent class for cops loaded before our base cop class
# are detected as RuboCop::Cop::Base, and that complicates the detection
# of their relation with RuboCop RSpec.
rspec_cop_path = File.join('lib', 'rubocop', 'cop', 'rspec', 'base.rb')
require 'rubocop-factory_bot'
require 'rubocop/factory_bot/config_formatter'
require 'rubocop/factory_bot/description_extractor'

glob = File.join('lib', 'rubocop', 'cop', 'factory_bot', '*.rb')
YARD::Tags::Library.define_tag('Cop Safety Information', :safety)
YARD.parse(Dir[glob].prepend(rspec_cop_path), [])
YARD.parse(Dir[glob], [])

descriptions =
RuboCop::RSpec::DescriptionExtractor.new(YARD::Registry.all(:class)).to_h
RuboCop::FactoryBot::DescriptionExtractor.new(
YARD::Registry.all(:class)
).to_h
current_config = if Psych::VERSION >= '4.0.0' # RUBY_VERSION >= '3.1.0'
YAML.unsafe_load_file('config/default.yml')
else
Expand All @@ -54,7 +49,7 @@ task :build_config do

File.write(
'config/default.yml',
RuboCop::RSpec::ConfigFormatter.new(current_config, descriptions).dump
RuboCop::FactoryBot::ConfigFormatter.new(current_config, descriptions).dump
)
end

Expand Down Expand Up @@ -100,7 +95,9 @@ task :new_cop, [:cop] do |_task, args|
generator = RuboCop::Cop::Generator.new(cop_name)
generator.write_source
generator.write_spec
generator.inject_require(root_file_path: 'lib/rubocop/cop/rspec_cops.rb')
generator.inject_require(
root_file_path: 'lib/rubocop/cop/factory_bot_cops.rb'
)
generator.inject_config

puts generator.todo
Expand Down
Loading

0 comments on commit 9d0e37f

Please sign in to comment.