Skip to content

Commit

Permalink
Update Thor::Actions#inside to return the value yielded by the block
Browse files Browse the repository at this point in the history
So that calling code can inspect & use the value.
  • Loading branch information
jordan-brough committed Jan 28, 2020
1 parent 2899621 commit 6c14044
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/thor/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def find_in_source_paths(file)
# to the block you provide. The path is set back to the previous path when
# the method exits.
#
# Returns the value yielded by the block.
#
# ==== Parameters
# dir<String>:: the directory to move to.
# config<Hash>:: give :verbose => true to log and use padding.
Expand All @@ -179,7 +181,7 @@ def inside(dir = "", config = {}, &block)
FileUtils.mkdir_p(destination_root)
end

if pretend
result = if pretend
# In pretend mode, just yield down to the block
block.arity == 1 ? yield(destination_root) : yield
else
Expand All @@ -189,6 +191,7 @@ def inside(dir = "", config = {}, &block)

@destination_stack.pop
shell.padding -= 1 if verbose
result
end

# Goes to the root and execute the given block.
Expand Down
4 changes: 4 additions & 0 deletions spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def file
end
end

it "returns the value yielded by the block" do
expect(runner.inside("foo") { 123 }).to eq(123)
end

describe "when pretending" do
it "no directories should be created" do
runner.inside("bar", :pretend => true) {}
Expand Down

0 comments on commit 6c14044

Please sign in to comment.