-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lazy task definition: take codeclimate into account
- Loading branch information
1 parent
b550157
commit 2458477
Showing
6 changed files
with
130 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
module Rake | ||
|
||
# The LazyTaskDefinition module is a mixin for managing lazy defined tasks. | ||
module LazyTaskDefinition | ||
|
||
# Execute all definitions: usefull for rake -T for instance | ||
def execute_all_lazy_definitions | ||
lazy_definitions.each do |scope_path, _definitions| | ||
execute_lazy_definitions(scope_path) | ||
end | ||
end | ||
|
||
# Execute all definitions linked to specified +scope_path+ | ||
# and its parent scopes. | ||
def execute_lazy_definitions(scope_path) | ||
scope_path_elements = scope_path.split(':') | ||
sub_scope_elements = [] | ||
scope_path_elements.each do |e| | ||
sub_scope_elements << e | ||
sub_scope_path = sub_scope_elements.join(':') | ||
definitions = lazy_definitions[sub_scope_path] | ||
next unless definitions | ||
definitions.each do |definition| | ||
definition.call | ||
end | ||
definitions.clear | ||
end | ||
end | ||
|
||
# Evaluate the block in specified +scope+. | ||
def in_scope(scope) | ||
cur_scope = @scope | ||
@scope = scope | ||
yield | ||
ensure | ||
@scope = cur_scope | ||
end | ||
|
||
# Register a block which will be called only when necessary during the lookup | ||
# of tasks | ||
def register_lazy_definition(&block) | ||
cur_scope = @scope | ||
lazy_definitions[cur_scope.path] ||= [] | ||
lazy_definitions[cur_scope.path] << ->() { in_scope(cur_scope, &block) } | ||
end | ||
|
||
def lazy_definitions | ||
@lazy_definitions ||= {} | ||
end | ||
private :lazy_definitions | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
require File.expand_path("../helper", __FILE__) | ||
|
||
class TestRakeLazyTaskDefinition < Rake::TestCase # :nodoc: | ||
|
||
def setup | ||
super | ||
|
||
@tm = Rake::TestCase::TaskManager.new | ||
@tm.extend Rake::LazyTaskDefinition | ||
end | ||
|
||
def test_lazy_definition | ||
t1, t2, t3 = nil, nil, nil | ||
lazy_definition_call_count = 0 | ||
@tm.in_namespace("a1") do | ||
@tm.register_lazy_definition do | ||
t1 = @tm.define_task(Rake::Task, :t1) | ||
lazy_definition_call_count += 1 | ||
@tm.in_namespace("a2") do | ||
t2 = @tm.define_task(Rake::Task, :t2) | ||
end | ||
end | ||
end | ||
@tm.in_namespace("b") do | ||
t3 = @tm.define_task(Rake::Task, :t3) | ||
end | ||
# task t3 is not lazy. It can be found | ||
assert_equal t3, @tm[:t3, Rake::Scope.make("b")] | ||
# lazy definition is not called until we look for task in namespace a | ||
assert_equal lazy_definition_call_count, 0 | ||
|
||
# task t2 can be found | ||
found_task_t2 = @tm[:t2, Rake::Scope.make("a1:a2")] | ||
assert_equal t2, found_task_t2 | ||
# lazy definition is expected to be called | ||
assert_equal lazy_definition_call_count, 1 | ||
|
||
# task t1 can also be found | ||
found_task_t1 = @tm[:t1, Rake::Scope.make("a1")] | ||
assert_equal t1, found_task_t1 | ||
# lazy definition is called at most once | ||
assert_equal lazy_definition_call_count, 1 | ||
end | ||
|
||
def test_execute_all_lazy_definitions | ||
lazy_definition_call_count = 0 | ||
@tm.in_namespace("a") do | ||
@tm.register_lazy_definition do | ||
lazy_definition_call_count += 1 | ||
end | ||
end | ||
assert_equal lazy_definition_call_count, 0 | ||
@tm.execute_all_lazy_definitions | ||
assert_equal lazy_definition_call_count, 1 | ||
@tm.execute_all_lazy_definitions | ||
assert_equal lazy_definition_call_count, 1 | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters