-
-
Notifications
You must be signed in to change notification settings - Fork 753
Did you mean functionality. #2601
Changes from 23 commits
511cec5
2d7e132
b14aded
70cf47c
ae1eb14
4d48b8e
c87e5ad
af62f5f
ad46caf
178ecb9
774fae2
fa2f0d4
3433043
97c46f4
476aaa4
29d9d08
940ceed
94a6b2f
9e8833c
c783ec6
f1c7c47
8798f39
3e829e4
57a6e20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module RSpec | ||
module Core | ||
# @private | ||
# Wrapper around Ruby's `DidYouMean::SpellChecker` when available to provide file name suggestions. | ||
class DidYouMean | ||
attr_reader :relative_file_name | ||
|
||
def initialize(relative_file_name) | ||
@relative_file_name = relative_file_name | ||
end | ||
|
||
if defined?(::DidYouMean::SpellChecker) | ||
# provide probable suggestions | ||
def call | ||
checker = ::DidYouMean::SpellChecker.new(:dictionary => Dir["spec/**/*.rb"]) | ||
probables = checker.correct(relative_file_name.sub('./', ''))[0..2] | ||
return '' unless probables.any? | ||
|
||
formats probables | ||
end | ||
else | ||
# return a hint if API for ::DidYouMean::SpellChecker not supported | ||
JonRowe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def call | ||
"\nHint: Install the `did_you_mean` gem in order to provide suggestions for similarly named files." | ||
end | ||
end | ||
|
||
private | ||
|
||
def formats(probables) | ||
rspec_format = probables.map { |s, _| "rspec ./#{s}" } | ||
red_font(top_and_tail rspec_format) | ||
end | ||
|
||
def top_and_tail(rspec_format) | ||
spaces = ' ' * 20 | ||
rspec_format.insert(0, ' - Did you mean?').join("\n#{spaces}") + "\n" | ||
end | ||
|
||
def red_font(mytext) | ||
colorizer = ::RSpec::Core::Formatters::ConsoleCodes | ||
colorizer.wrap mytext, :failure | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module RSpec | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We favour There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer it too, but when I used it, I encountered the following cop: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird, we do that style elsewhere, is it possible you're using more updated version of Rubocop than us? Or not running with our config? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am using
so I can either add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No its not a blocker |
||
module Core | ||
RSpec.describe DidYouMean do | ||
describe '#call' do | ||
if defined?(::DidYouMean::SpellChecker) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to remove this so the tests run when not defined for not defined There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
context "when `DidYouMean::SpellChecker` is available", :skip => !defined?(::DidYouMean::SpellChecker) do | ||
context 'Success' do | ||
let(:name) { './spec/rspec/core/did_you_mean_spec.rb' } | ||
it 'returns a useful suggestion' do | ||
expect(DidYouMean.new(name[0..-2]).call).to include name | ||
end | ||
context 'numerous possibilities' do | ||
it 'returns a small number of suggestions' do | ||
name = './spec/rspec/core/drb_spec.r' | ||
suggestions = DidYouMean.new(name).call | ||
expect(suggestions.split("\n").size).to eq 4 | ||
end | ||
end | ||
end | ||
context 'No suitable suggestions' do | ||
it 'returns empty string' do | ||
name = './' + 'x' * 50 | ||
expect(DidYouMean.new(name).call).to eq '' | ||
end | ||
end | ||
end | ||
context "when `DidYouMean::SpellChecker` is not available", :unless => defined?(::DidYouMean::SpellChecker) do | ||
describe 'Success' do | ||
let(:name) { './spec/rspec/core/did_you_mean_spec.rb' } | ||
it 'returns a hint' do | ||
expect(DidYouMean.new(name[0..-2]).call).to include 'Hint:' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you rebase this commit will go away
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you are talking about the commit #2591 with sha 511cec5. I am concerned that if I rebase this on my local machine and then push it, then the will be problems as I have already pushed this up. As well, if I look at rspec-core master, #2591 has a sha of cb1b4ce If I do a diff between my local did_you_mean branch and my local master it is not showing any files from 511cec5. Frankly, I do not understand what is happening here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its because its based off an older branch, github does that. You can use
git push <origin> <branch> --force-with-lease
to update this branch safely after a rebase, it won't let you ruin anything then.