Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
[Fix rubocop#12740] Tweak annotation pattern for expect_offense
Browse files Browse the repository at this point in the history
Fixes rubocop#12740.

This PR tweaks annotation pattern for `expect_offense`.

When there is no space between `^` annotation and message, as follows:

```ruby
expect_offense(<<~RUBY)
  #!/usr/bin/env ruby
  # encoding: utf-8
  ^^^^^^^^^^^^^^^^^Unnecessary utf-8 encoding comment.
  def foo() end
RUBY
```

## Before

Despite being correct with `expect_offense`, it suggests using `expect_no_offenses`:

```console
$ bundle exec rspec spec/rubocop/cop/style/encoding_spec.rb
(snip)

Failures:

  1) RuboCop::Cop::Style::Encoding registers an offense when encoding present on 2nd line after shebang
     Failure/Error: raise 'Use `expect_no_offenses` to assert that no offenses are found'

     RuntimeError:
       Use `expect_no_offenses` to assert that no offenses are found
     # ./lib/rubocop/rspec/expect_offense.rb:196:in `parse_annotations'
     # ./lib/rubocop/rspec/expect_offense.rb:115:in `expect_offense'
     # ./spec/rubocop/cop/style/encoding_spec.rb:37:in `block (2 levels) in <top (required)>'
```

## After

It suggests that the usage of annotation is incorrect:

```console
$ bundle exec rspec spec/rubocop/cop/style/encoding_spec.rb
(snip)

Failures:

  1) RuboCop::Cop::Style::Encoding registers an offense when encoding present on 2nd line after shebang
     Failure/Error: expect(actual_annotations).to eq(expected_annotations), ''

       Diff:
       @@ -1,5 +1,5 @@
        #!/usr/bin/env ruby
        # encoding: utf-8
       -^^^^^^^^^^^^^^^^^Unnecessary utf-8 encoding comment.
       +^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
        def foo() end

     # ./lib/rubocop/rspec/expect_offense.rb:123:in `expect_offense'
     # ./spec/rubocop/cop/style/encoding_spec.rb:37:in `block (2 levels) in <top (required)>'
```
  • Loading branch information
koic authored and bbatsov committed Mar 5, 2024
1 parent 859f6fd commit 07cad75
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/rspec/expect_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def set_formatter_options

# Parsed representation of code annotated with the `^^^ Message` style
class AnnotatedSource
ANNOTATION_PATTERN = /\A\s*(\^+|\^{}) /.freeze
ANNOTATION_PATTERN = /\A\s*(\^+|\^{}) ?/.freeze
ABBREV = "[...]\n"

# @param annotated_source [String] string passed to the matchers
Expand Down

0 comments on commit 07cad75

Please sign in to comment.