Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Did you mean functionality. #2601

Merged
merged 24 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_rspec_base.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

# This file contains defaults for RSpec projects. Individual projects
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

language: ruby
Expand Down Expand Up @@ -40,7 +40,6 @@ matrix:
- rvm: jruby-head
- rvm: ruby-head
- rvm: rbx-3
- rvm: ruby-2.6.0-preview1
fast_finish: true
branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

# DO NOT modify it by hand as your changes will get lost the next time it is generated.

version: "{build}"
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def self.world
module Core
autoload :ExampleStatusPersister, "rspec/core/example_status_persister"
autoload :Profiler, "rspec/core/profiler"
autoload :DidYouMean, "rspec/core/did_you_mean"

# @private
# This avoids issues with reporting time caused by examples that
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,11 @@ def bisect_runner_class

def load_file_handling_errors(method, file)
__send__(method, file)
rescue LoadError => ex
relative_file = Metadata.relative_path(file)
suggestions = DidYouMean.new(relative_file).call
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.#{suggestions}")
RSpec.world.wants_to_quit = true
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
relative_file = Metadata.relative_path(file)
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")
Expand Down
46 changes: 46 additions & 0 deletions lib/rspec/core/did_you_mean.rb
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
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
2 changes: 1 addition & 1 deletion script/clone_all_rspec_repos
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

set -e
Expand Down
2 changes: 1 addition & 1 deletion script/functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Expand Down
2 changes: 1 addition & 1 deletion script/predicate_functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

function is_mri {
Expand Down
2 changes: 1 addition & 1 deletion script/run_build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

set -e
Expand Down
2 changes: 1 addition & 1 deletion script/travis_functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

# Taken from:
Expand Down
2 changes: 1 addition & 1 deletion script/update_rubygems_and_install_bundler
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# This file was generated on 2019-01-03T20:34:23+00:00 from the rspec-dev repo.
# This file was generated on 2019-01-08T11:28:05+00:00 from the rspec-dev repo.
# DO NOT modify it by hand as your changes will get lost the next time it is generated.

set -e
Expand Down
39 changes: 39 additions & 0 deletions spec/rspec/core/did_you_mean_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module RSpec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We favour RSpec::Core style name spacing, saves some spaces :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:
Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style.
When I investigated further, I found this description of why the cop is there.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using 0.52.1, the same as in the Gemfile. I looked at .rubocop.yml and it has the following entry for this cop:

# Not sure what to do with this rule yet.
Style/ClassAndModuleChildren:
  Exclude:
    - lib/rspec/core/formatters.rb
    - lib/rspec/core/notifications.rb
    - lib/rspec/core/option_parser.rb
    - lib/rspec/core/reporter.rb

so I can either add did_you_mean.rb to this list, or leave the nested modules syntax in?

Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to remove this

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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