Skip to content

Commit 1a7c2e1

Browse files
sakurobquorningpirj
committed
RSpec/ContextWording: Detect non-string in Prefixes
Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com> Co-authored-by: Phil Pirozhkov <pirj@users.noreply.github.com>
1 parent 39a9d89 commit 1a7c2e1

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
6+
- Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro])
67

78
## 3.5.0 (2025-02-16)
89

@@ -1040,6 +1041,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
10401041
[@rrosenblum]: https://github.com/rrosenblum
10411042
[@rspeicher]: https://github.com/rspeicher
10421043
[@rst-j]: https://github.com/RST-J
1044+
[@sakuro]: https://github.com/sakuro
10431045
[@samrjenkins]: https://github.com/samrjenkins
10441046
[@schmijos]: https://github.com/schmijos
10451047
[@seanpdoyle]: https://github.com/seanpdoyle

lib/rubocop/cop/rspec/context_wording.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ def expect_patterns
115115
end
116116

117117
def prefixes
118-
Array(cop_config.fetch('Prefixes', []))
118+
Array(cop_config.fetch('Prefixes', [])).tap do |prefixes|
119+
non_strings = prefixes.reject { |pre| pre.is_a?(String) }
120+
unless non_strings.empty?
121+
raise "Non-string prefixes #{non_strings.inspect} detected."
122+
end
123+
end
119124
end
120125
end
121126
end

spec/rubocop/cop/rspec/context_wording_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,38 @@
228228
RUBY
229229
end
230230
end
231+
232+
context 'when `Prefixes: [on]' do
233+
let(:cop_config) do
234+
YAML.safe_load(<<-CONFIG)
235+
Prefixes:
236+
- on # evaluates to true
237+
CONFIG
238+
end
239+
240+
it 'fails' do
241+
expect do
242+
expect_no_offenses(<<~RUBY)
243+
context 'on Linux' do
244+
end
245+
RUBY
246+
end.to raise_error(/Non-string prefixes .+ detected/)
247+
end
248+
end
249+
250+
context 'when `Prefixes: ["on"]' do
251+
let(:cop_config) do
252+
YAML.safe_load(<<-CONFIG)
253+
Prefixes:
254+
- "on"
255+
CONFIG
256+
end
257+
258+
it 'does not fail' do
259+
expect { expect_no_offenses(<<~RUBY) }.not_to raise_error
260+
context 'on Linux' do
261+
end
262+
RUBY
263+
end
264+
end
231265
end

0 commit comments

Comments
 (0)