Skip to content

Commit

Permalink
Make spec suite compatible with RSpec 4
Browse files Browse the repository at this point in the history
See changes in rspec/rspec-metagem#61
  • Loading branch information
pirj committed Jul 11, 2022
1 parent 4c4b91e commit 7b39fd0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 41 deletions.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--color
--require spec_helper
28 changes: 14 additions & 14 deletions lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'tmpdir'

RSpec.shared_context 'isolated environment', :isolated_environment do
RSpec.shared_context 'isolated environment' do
around do |example|
Dir.mktmpdir do |tmpdir|
original_home = Dir.home
Expand Down Expand Up @@ -38,7 +38,7 @@
end
end

RSpec.shared_context 'maintain registry', :restore_registry do
RSpec.shared_context 'maintain registry' do
around(:each) { |example| RuboCop::Cop::Registry.with_temporary_global { example.run } }

def stub_cop_class(name, inherit: RuboCop::Cop::Base, &block)
Expand All @@ -49,7 +49,7 @@ def stub_cop_class(name, inherit: RuboCop::Cop::Base, &block)
end

# This context assumes nothing and defines `cop`, among others.
RSpec.shared_context 'config', :config do # rubocop:disable Metrics/BlockLength
RSpec.shared_context 'config' do # rubocop:disable Metrics/BlockLength
### Meant to be overridden at will

let(:cop_class) do
Expand Down Expand Up @@ -116,46 +116,46 @@ def source_range(range, buffer: source_buffer)
end
end

RSpec.shared_context 'ruby 2.0', :ruby20 do
RSpec.shared_context 'ruby 2.0' do
let(:ruby_version) { 2.0 }
end

RSpec.shared_context 'ruby 2.1', :ruby21 do
RSpec.shared_context 'ruby 2.1' do
let(:ruby_version) { 2.1 }
end

RSpec.shared_context 'ruby 2.2', :ruby22 do
RSpec.shared_context 'ruby 2.2' do
let(:ruby_version) { 2.2 }
end

RSpec.shared_context 'ruby 2.3', :ruby23 do
RSpec.shared_context 'ruby 2.3' do
let(:ruby_version) { 2.3 }
end

RSpec.shared_context 'ruby 2.4', :ruby24 do
RSpec.shared_context 'ruby 2.4' do
let(:ruby_version) { 2.4 }
end

RSpec.shared_context 'ruby 2.5', :ruby25 do
RSpec.shared_context 'ruby 2.5' do
let(:ruby_version) { 2.5 }
end

RSpec.shared_context 'ruby 2.6', :ruby26 do
RSpec.shared_context 'ruby 2.6' do
let(:ruby_version) { 2.6 }
end

RSpec.shared_context 'ruby 2.7', :ruby27 do
RSpec.shared_context 'ruby 2.7' do
let(:ruby_version) { 2.7 }
end

RSpec.shared_context 'ruby 3.0', :ruby30 do
RSpec.shared_context 'ruby 3.0' do
let(:ruby_version) { 3.0 }
end

RSpec.shared_context 'ruby 3.1', :ruby31 do
RSpec.shared_context 'ruby 3.1' do
let(:ruby_version) { 3.1 }
end

RSpec.shared_context 'ruby 3.2', :ruby32 do
RSpec.shared_context 'ruby 3.2' do
let(:ruby_version) { 3.2 }
end
14 changes: 14 additions & 0 deletions lib/rubocop/rspec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@
RSpec.configure do |config|
config.include CopHelper
config.include HostEnvironmentSimulatorHelper
config.include_context 'config', :config
config.include_context 'isolated environment', :isolated_environment
config.include_context 'maintain registry', :restore_registry
config.include_context 'ruby 2.0', :ruby20
config.include_context 'ruby 2.1', :ruby21
config.include_context 'ruby 2.2', :ruby22
config.include_context 'ruby 2.3', :ruby23
config.include_context 'ruby 2.4', :ruby24
config.include_context 'ruby 2.5', :ruby25
config.include_context 'ruby 2.6', :ruby26
config.include_context 'ruby 2.7', :ruby27
config.include_context 'ruby 3.0', :ruby30
config.include_context 'ruby 3.1', :ruby31
config.include_context 'ruby 3.2', :ruby32
end
23 changes: 3 additions & 20 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,24 @@
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }

RSpec.configure do |config|
# This config option will be enabled by default on RSpec 4,
# but for reasons of backwards compatibility, you have to
# set it on RSpec 3.
#
# It causes the host group and examples to inherit metadata
# from the shared context.
config.shared_context_metadata_behavior = :apply_to_host_groups

# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
unless defined?(::TestQueue)
# See. https://github.com/tmm1/test-queue/issues/60#issuecomment-281948929
config.filter_run :focus
config.run_all_when_everything_filtered = true
config.filter_run_when_matching :focus
end

config.example_status_persistence_file_path = 'spec/examples.txt'
config.disable_monkey_patching!

config.include RuboCop::RSpec::ExpectOffense

config.order = :random
Kernel.srand config.seed

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
expectations.syntax = :expect # Disable `should`
end

config.mock_with :rspec do |mocks|
mocks.syntax = :expect # Disable `should_receive` and `stub`
mocks.verify_partial_doubles = true
end
config.expect_with :rspec
config.mock_with :rspec

config.before(:suite) do
RuboCop::Cop::Registry.global.freeze
Expand Down
4 changes: 2 additions & 2 deletions spec/support/alignment_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# `cop` and `source` must be declared with #let.

shared_examples_for 'misaligned' do |annotated_source, used_style|
RSpec.shared_examples_for 'misaligned' do |annotated_source, used_style|
config_to_allow_offenses = if used_style
{ 'EnforcedStyleAlignWith' => used_style.to_s }
else
Expand All @@ -29,7 +29,7 @@
end
end

shared_examples_for 'aligned' do |alignment_base, arg, end_kw, name|
RSpec.shared_examples_for 'aligned' do |alignment_base, arg, end_kw, name|
name ||= alignment_base
name = name.gsub(/\n/, ' <newline>')
it "accepts matching #{name} ... end" do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/empty_lines_around_body_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

shared_examples_for 'empty_lines_around_class_or_module_body' do |type|
RSpec.shared_examples_for 'empty_lines_around_class_or_module_body' do |type|
context 'when EnforcedStyle is empty_lines_special' do
let(:cop_config) { { 'EnforcedStyle' => 'empty_lines_special' } }

Expand Down
2 changes: 1 addition & 1 deletion spec/support/multiline_literal_brace_layout_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

shared_examples_for 'multiline literal brace layout' do
RSpec.shared_examples_for 'multiline literal brace layout' do
include MultilineLiteralBraceHelper

let(:prefix) { '' } # A prefix before the opening brace.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

shared_examples_for 'multiline literal brace layout method argument' do
RSpec.shared_examples_for 'multiline literal brace layout method argument' do
include MultilineLiteralBraceHelper

context 'when arguments to a method' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

shared_examples_for 'multiline literal brace layout trailing comma' do
RSpec.shared_examples_for 'multiline literal brace layout trailing comma' do
let(:prefix) { '' } # A prefix before the opening brace.
let(:suffix) { '' } # A suffix for the line after the closing brace.
let(:open) { nil } # The opening brace.
Expand Down

0 comments on commit 7b39fd0

Please sign in to comment.