Skip to content

Commit

Permalink
Merge pull request #712 from jordan-brough/return-value-from-inside
Browse files Browse the repository at this point in the history
🌈 Update `Thor::Actions#inside` to return the value yielded by the block
  • Loading branch information
rafaelfranca authored Jun 2, 2021
2 parents ce18deb + 3d5e96f commit 84c57f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions 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,16 +181,18 @@ def inside(dir = "", config = {}, &block)
FileUtils.mkdir_p(destination_root)
end

result = nil
if pretend
# In pretend mode, just yield down to the block
block.arity == 1 ? yield(destination_root) : yield
result = block.arity == 1 ? yield(destination_root) : yield
else
require "fileutils"
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
FileUtils.cd(destination_root) { result = block.arity == 1 ? yield(destination_root) : yield }
end

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

# Goes to the root and execute the given block.
Expand Down
8 changes: 8 additions & 0 deletions spec/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,19 @@ 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) {}
expect(File.exist?("bar")).to be false
end

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

describe "when verbose" do
Expand Down

0 comments on commit 84c57f6

Please sign in to comment.