From 349e1ef4f122275053450b4add0269b33e7f5185 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Fri, 9 Jan 2015 17:52:39 -0700 Subject: [PATCH 1/2] Modify output on failed task to offer alternatives. --- lib/rake/task_manager.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/rake/task_manager.rb b/lib/rake/task_manager.rb index cbb9f5ee2..b4ff71b9b 100644 --- a/lib/rake/task_manager.rb +++ b/lib/rake/task_manager.rb @@ -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 } + 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: From 4b248af56ad207418a92f12f53576a1dca5d1f38 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Fri, 9 Jan 2015 19:03:08 -0700 Subject: [PATCH 2/2] Added test for alt suggestions. --- test/test_rake_task.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test_rake_task.rb b/test/test_rake_task.rb index 0416dfa8d..c72643909 100644 --- a/test/test_rake_task.rb +++ b/test/test_rake_task.rb @@ -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