Skip to content

Commit

Permalink
Expose 'with default RSpec/Language config' to consumers
Browse files Browse the repository at this point in the history
This helper is essential for anyone writing their own RSpec cops based
on `RuboCop::Cop::RSpec::Base`, because it now requires a minimal config
to even run the cop.

The wordy file name is because the RuboCop gem already defines
`rubocop/rspec/shared_contexts`, and we don't want to shadow that. This
means that consumers of this library who want to write a spec for a cop
based on `RuboCop::Cop::RSpec::Base` will need something like this in
`spec/spec_helper.rb`:

require 'rubocop/rspec/shared_contexts/default_rspec_language_config_context'

RSpec.config do |config|
  # ...
  # Add metadata as appropriate to ensure this only happens for RSpec
  # cop specs
  config.include_context 'with default RSpec/Language config'
end
  • Loading branch information
smcgivern committed Nov 3, 2022
1 parent b18433b commit 81c9faa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix `RSpec/FactoryBot/ConsistentParenthesesStyle` to ignore calls inside a Hash or an Array. ([@pirj])
- Fix `RSpec/NestedGroups` to correctly use `AllowedGroups` config. ([@samrjenkins])
- Remove `Runners` and `HookScopes` RSpec DSL elements from configuration. ([@pirj])
- Add `with default RSpec/Language config` helper to `lib` (under `rubocop/rspec/shared_contexts/default_rspec_language_config_context`), to allow use for downstream cops based on `RuboCop::Cop::RSpec::Base`. ([@smcgivern])

## 2.14.2 (2022-10-25)

Expand Down Expand Up @@ -772,6 +773,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@schmijos]: https://github.com/schmijos
[@seanpdoyle]: https://github.com/seanpdoyle
[@sl4vr]: https://github.com/sl4vr
[@smcgivern]: https://github.com/smcgivern
[@stephannv]: https://github.com/stephannv
[@t3h2mas]: https://github.com/t3h2mas
[@tdeo]: https://github.com/tdeo
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* xref:cops.adoc[Cops]
* xref:upgrade_to_version_2.adoc[Upgrade to 2.x]
* xref:third_party_rspec_syntax_extensions.adoc[RSpec syntax extensions in third-party gems]
* xref:development.adoc[Development]
* Cops Documentation
** xref:cops_rspec_capybara.adoc[Capybara]
** xref:cops_rspec_factorybot.adoc[FactoryBot]
Expand Down
32 changes: 32 additions & 0 deletions docs/modules/ROOT/pages/development.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
= Development

This page describes considerations when developing RSpec-specific cops. It is intended to be a complement to the general https://docs.rubocop.org/rubocop/development.html[RuboCop development documentation].

== Base class

The `RuboCop::Cop::RSpec::Base` class includes convenient https://docs.rubocop.org/rubocop-ast/node_pattern.html[node pattern DSL] matchers that will automatically account for any xref:usage.adoc#rspec-dsl-configuration[custom RSpec DSL configuration].

For example, if the project defines https://github.com/test-prof/test-prof/blob/master/docs/recipes/let_it_be.md[`let_it_be`] as a `Helper`, then all cops will find `let_it_be` when using the `let?` matcher.

== Writing specs

When working on RSpec-specific cops, ensure that the https://github.com/rubocop/rubocop-rspec/blob/master/config/default.yml[default language config] is loaded for all RSpec specs. For example:

[source,ruby]
----
require 'rubocop/rspec/shared_contexts/default_rspec_language_config_context'
RSpec.config do |config|
# Set metadata on all cop specs
config.define_derived_metadata(file_path: %r{/spec/rubocop/cop/}) do |meta|
meta[:type] = :cop_spec
end
# Include RuboCop's config shared context for all cop specs
config.define_derived_metadata(type: :cop_spec) do |meta|
meta[:config] = true
end
config.include_context 'with default RSpec/Language config', :config
end
----
7 changes: 5 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module SpecHelper
ROOT = Pathname.new(__dir__).parent.freeze
end

spec_helper_glob = File.expand_path('{support,shared}/*.rb', __dir__)
Dir.glob(spec_helper_glob).sort.each(&method(:require))
spec_helper_glob = '{support,shared,../lib/rubocop/rspec/shared_contexts}/*.rb'
Dir
.glob(File.expand_path(spec_helper_glob, __dir__))
.sort
.each(&method(:require))

RSpec.configure do |config|
# Set metadata so smoke tests are run on all cop specs
Expand Down

0 comments on commit 81c9faa

Please # to comment.