Skip to content

Commit

Permalink
Merge pull request #1377 from smcgivern/expose-default-rspec-language…
Browse files Browse the repository at this point in the history
…-context

Expose 'with default RSpec/Language config' to consumers
  • Loading branch information
pirj authored Nov 3, 2022
2 parents 98e9994 + 81c9faa commit 5e7f0fd
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 5e7f0fd

Please sign in to comment.