Browse files

New implementation without duplicates

Rename Rake::Task#prerequisite_tasks! to
Rake::Task#all_prerequisite_tasks.

Reimplement Rake::Task#all_prerequisite_tasks to not return duplicates.

Add protected helper method to recursively collect dependencies
rejecting duplicates.

Abstract tests from returned list order.

Signed-off-by: João Soares <jsoaresgeral@gmail.com>
  • Loading branch information...
1 parent 396a64a commit 2bfc48fc37881831db51fd0a9d1ed752b71a668c @jasoares jasoares committed Feb 20, 2013
Showing with 27 additions and 27 deletions.
  1. +13 −11 lib/rake/task.rb
  2. +14 −16 test/test_rake_task.rb
View
24 lib/rake/task.rb
@@ -61,18 +61,20 @@ def lookup_prerequisite(prerequisite_name)
end
private :lookup_prerequisite
- # Complete list of prerequisite tasks including subtask's prerequisites
- # recursively until no prerequisites exist or depth +limit+ reached.
- # Default +limit+ is 30.
- def prerequisite_tasks!(limit=30)
- self.prerequisite_tasks.inject([]) do |tasks, task|
- tasks << task
- unless task.prerequisite_tasks.empty? or limit < 1
- tasks += task.prerequisite_tasks!(limit - 1)
- end
- tasks
- end
+ # List of all unique prerequisite tasks including prerequisite tasks'
+ # prerequisites.
+ # Includes self when cyclic dependencies are found.
+ def all_prerequisite_tasks
+ fetch_prerequisites
+ end
+
+ def fetch_prerequisites(list=[])
+ prerequisite_tasks.each do |pre|
+ list << pre and pre.fetch_prerequisites(list) unless list.include?(pre)
+ end unless prerequisite_tasks.empty?
+ list
end
+ protected :fetch_prerequisites
# First source from a rule (nil if no sources)
def source
View
30 test/test_rake_task.rb
@@ -220,31 +220,29 @@ def test_prerequiste_tasks_honors_namespaces
assert_equal [b, c], a.prerequisite_tasks
end
- def test_deep_prerequisite_tasks_includes_deep_prerequisites
- a = task :a => ["b", "d"]
- b = task :b => "c"
- c = task :c
+ def test_all_prerequisite_tasks_includes_all_prerequisites
+ a = task :a => "b"
+ b = task :b => ["c", "d"]
+ c = task :c => "e"
d = task :d
+ e = task :e
- assert_equal [b, c, d], a.prerequisite_tasks!
+ assert_equal [b, c, d, e], a.all_prerequisite_tasks.sort_by { |t| t.name }
end
- def test_deep_prerequisite_tasks_honors_limit
- a = task :a => "b"
+ def test_all_prerequisite_tasks_does_not_include_duplicates
+ a = task :a => ["b", "c"]
b = task :b => "c"
- c = task :c => "a"
+ c = task :c
- assert a.prerequisite_tasks!.include?(a)
- assert a.prerequisite_tasks!.include?(b)
- assert a.prerequisite_tasks!.include?(c)
+ assert_equal [b, c], a.all_prerequisite_tasks.sort_by { |t| t.name }
end
- def test_deep_prerequisite_tasks_includes_duplicates
- a = task :a => ["b", "c"]
- b = task :b => "c"
- c = task :c
+ def test_all_prerequisite_tasks_includes_self_on_cyclic_dependencies
+ a = task :a => "b"
+ b = task :b => "a"
- assert_equal [b, c, c], a.prerequisite_tasks!
+ assert_equal [a, b], a.all_prerequisite_tasks.sort_by { |t| t.name }
end
def test_timestamp_returns_now_if_all_prereqs_have_no_times

0 comments on commit 2bfc48f

Please sign in to comment.