Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌈 Update Thor::Actions#inside to return the value yielded by the block #712

Merged
merged 2 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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