2011-06-23 18:11:55 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
|
|
|
|
|
|
|
class TestRakeTaskWithArguments < Rake::TestCase
|
|
|
|
include Rake
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
Task.clear
|
|
|
|
Rake::TaskManager.record_task_metadata = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Rake::TaskManager.record_task_metadata = false
|
2014-05-28 02:05:43 -04:00
|
|
|
Rake.application.thread_pool.join
|
2011-06-23 18:11:55 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_args_given
|
|
|
|
t = task :t
|
|
|
|
assert_equal [], t.arg_names
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_args_given
|
|
|
|
t = task :t, :a, :b
|
|
|
|
assert_equal [:a, :b], t.arg_names
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_name_and_needs
|
|
|
|
t = task(:t => [:pre])
|
|
|
|
assert_equal "t", t.name
|
|
|
|
assert_equal [], t.arg_names
|
|
|
|
assert_equal ["pre"], t.prerequisites
|
|
|
|
end
|
|
|
|
|
2013-10-11 17:35:01 -04:00
|
|
|
def test_name_args_and_prereqs
|
|
|
|
t = task(:t, [:x, :y] => [:pre])
|
|
|
|
assert_equal "t", t.name
|
|
|
|
assert_equal [:x, :y], t.arg_names
|
|
|
|
assert_equal ["pre"], t.prerequisites
|
2011-06-23 18:11:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_arg_list_is_empty_if_no_args_given
|
|
|
|
t = task(:t) { |tt, args| assert_equal({}, args.to_hash) }
|
|
|
|
t.invoke(1, 2, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tasks_can_access_arguments_as_hash
|
|
|
|
t = task :t, :a, :b, :c do |tt, args|
|
|
|
|
assert_equal({:a => 1, :b => 2, :c => 3}, args.to_hash)
|
|
|
|
assert_equal 1, args[:a]
|
|
|
|
assert_equal 2, args[:b]
|
|
|
|
assert_equal 3, args[:c]
|
|
|
|
assert_equal 1, args.a
|
|
|
|
assert_equal 2, args.b
|
|
|
|
assert_equal 3, args.c
|
|
|
|
end
|
|
|
|
t.invoke(1, 2, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_actions_of_various_arity_are_ok_with_args
|
|
|
|
notes = []
|
|
|
|
t = task(:t, :x) do
|
|
|
|
notes << :a
|
|
|
|
end
|
|
|
|
t.enhance do | |
|
|
|
|
notes << :b
|
|
|
|
end
|
|
|
|
t.enhance do |task|
|
|
|
|
notes << :c
|
|
|
|
assert_kind_of Task, task
|
|
|
|
end
|
|
|
|
t.enhance do |t2, args|
|
|
|
|
notes << :d
|
|
|
|
assert_equal t, t2
|
|
|
|
assert_equal({:x => 1}, args.to_hash)
|
|
|
|
end
|
|
|
|
t.invoke(1)
|
|
|
|
assert_equal [:a, :b, :c, :d], notes
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_arguments_are_passed_to_block
|
|
|
|
t = task(:t, :a, :b) { |tt, args|
|
2013-10-11 17:35:01 -04:00
|
|
|
assert_equal({ :a => 1, :b => 2 }, args.to_hash)
|
2011-06-23 18:11:55 -04:00
|
|
|
}
|
|
|
|
t.invoke(1, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extra_parameters_are_ignored
|
|
|
|
t = task(:t, :a) { |tt, args|
|
|
|
|
assert_equal 1, args.a
|
|
|
|
assert_nil args.b
|
|
|
|
}
|
|
|
|
t.invoke(1, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_arguments_are_passed_to_all_blocks
|
|
|
|
counter = 0
|
|
|
|
t = task :t, :a
|
|
|
|
task :t do |tt, args|
|
|
|
|
assert_equal 1, args.a
|
|
|
|
counter += 1
|
|
|
|
end
|
|
|
|
task :t do |tt, args|
|
|
|
|
assert_equal 1, args.a
|
|
|
|
counter += 1
|
|
|
|
end
|
|
|
|
t.invoke(1)
|
|
|
|
assert_equal 2, counter
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_with_no_parameters_is_ok
|
|
|
|
t = task(:t) { }
|
|
|
|
t.invoke(1, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_name_with_args
|
|
|
|
desc "T"
|
|
|
|
t = task(:tt, :a, :b)
|
|
|
|
assert_equal "tt", t.name
|
|
|
|
assert_equal "T", t.comment
|
|
|
|
assert_equal "[a,b]", t.arg_description
|
|
|
|
assert_equal "tt[a,b]", t.name_with_args
|
2013-10-11 17:35:01 -04:00
|
|
|
assert_equal [:a, :b], t.arg_names
|
2011-06-23 18:11:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_args_are_passed_to_prereqs
|
|
|
|
value = nil
|
2012-12-20 21:34:37 -05:00
|
|
|
task(:pre, :rev) { |t, args| value = args.rev }
|
2011-06-23 18:11:55 -04:00
|
|
|
t = task(:t, [:name, :rev] => [:pre])
|
|
|
|
t.invoke("bill", "1.2")
|
|
|
|
assert_equal "1.2", value
|
|
|
|
end
|
|
|
|
|
2012-11-29 14:16:46 -05:00
|
|
|
def test_args_not_passed_if_no_prereq_names_on_task
|
2012-12-20 21:34:37 -05:00
|
|
|
task(:pre) { |t, args|
|
2011-06-23 18:11:55 -04:00
|
|
|
assert_equal({}, args.to_hash)
|
|
|
|
assert_equal "bill", args.name
|
|
|
|
}
|
|
|
|
t = task(:t, [:name, :rev] => [:pre])
|
|
|
|
t.invoke("bill", "1.2")
|
|
|
|
end
|
|
|
|
|
2012-11-29 14:16:46 -05:00
|
|
|
def test_args_not_passed_if_no_prereq_names_on_multitask
|
2012-12-20 21:34:37 -05:00
|
|
|
task(:pre) { |t, args|
|
2012-11-29 14:16:46 -05:00
|
|
|
assert_equal({}, args.to_hash)
|
|
|
|
assert_equal "bill", args.name
|
|
|
|
}
|
|
|
|
t = multitask(:t, [:name, :rev] => [:pre])
|
|
|
|
t.invoke("bill", "1.2")
|
|
|
|
end
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
def test_args_not_passed_if_no_arg_names
|
2012-12-20 21:34:37 -05:00
|
|
|
task(:pre, :rev) { |t, args|
|
2011-06-23 18:11:55 -04:00
|
|
|
assert_equal({}, args.to_hash)
|
|
|
|
}
|
|
|
|
t = task(:t => [:pre])
|
|
|
|
t.invoke("bill", "1.2")
|
|
|
|
end
|
2011-06-27 22:45:29 -04:00
|
|
|
|
|
|
|
def test_values_at
|
2011-07-23 01:38:50 -04:00
|
|
|
t = task(:pre, [:a, :b, :c]) { |task, args|
|
2011-06-27 22:45:29 -04:00
|
|
|
a, b, c = args.values_at(:a, :b, :c)
|
|
|
|
assert_equal %w[1 2 3], [a, b, c]
|
|
|
|
}
|
2011-07-23 01:38:50 -04:00
|
|
|
|
2011-06-27 22:45:29 -04:00
|
|
|
t.invoke(*%w[1 2 3])
|
2011-07-23 01:38:50 -04:00
|
|
|
|
|
|
|
# HACK no assertions
|
2011-06-27 22:45:29 -04:00
|
|
|
end
|
2011-06-23 18:11:55 -04:00
|
|
|
end
|