diff --git a/CHANGELOG.md b/CHANGELOG.md index 51325954d..af623479c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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 diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index b50d714f8..0487d48aa 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -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] diff --git a/docs/modules/ROOT/pages/development.adoc b/docs/modules/ROOT/pages/development.adoc new file mode 100644 index 000000000..f10522d03 --- /dev/null +++ b/docs/modules/ROOT/pages/development.adoc @@ -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 +---- diff --git a/spec/shared/default_rspec_language_config_context.rb b/lib/rubocop/rspec/shared_contexts/default_rspec_language_config_context.rb similarity index 100% rename from spec/shared/default_rspec_language_config_context.rb rename to lib/rubocop/rspec/shared_contexts/default_rspec_language_config_context.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fc00e62a5..cf19e9f84 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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