Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer alternatives if rake task not found #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion lib/rake/task_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ def [](task_name, scopes=nil)
self.lookup(task_name, scopes) or
enhance_with_matching_rule(task_name) or
synthesize_file_task(task_name) or
fail "Don't know how to build task '#{task_name}'"
build_task_fail(task_name)
end

# If a task match wasn't found, try to offer suggestions.
# The task name must be at least 2 characters.
def build_task_fail(task_name)
msg = "Don't know how to build task '#{task_name}'"
if task_name.size > 1
matches = @tasks.keys.find_all{ |e| e =~ /#{task_name}/i }
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want /#{Regexp.escape task_name}/, unless no Regexp special characters can ever occur in a task name.

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've never seen one in the wild, but it's possible I suppose.

if matches.size > 0
msg += ". Did you mean one of these?\n\n" + matches.map{ |e| " #{e}" }.join("\n") + "\n"
end
end
fail msg
end

def synthesize_file_task(task_name) # :nodoc:
Expand Down
9 changes: 9 additions & 0 deletions test/test_rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ def test_find
assert_equal "Don't know how to build task 'leaves'", ex.message
end

def test_find_with_alts
task :tfindx
assert_equal "tfindx", Task[:tfindx].name
ex = assert_raises(RuntimeError) { Task[:tfind] }
assert_match /Don\'t know how to build task \'tfind\'/, ex.message
assert_match /Did you mean one of these\?/, ex.message
assert_match /tfindx/, ex.message
end

def test_defined
assert ! Task.task_defined?(:a)
task :a
Expand Down