From 6c1404472273d993ace5b9f832a7d83196370d9f Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Tue, 28 Jan 2020 10:31:56 -0700 Subject: [PATCH 1/2] Update `Thor::Actions#inside` to return the value yielded by the block So that calling code can inspect & use the value. --- lib/thor/actions.rb | 5 ++++- spec/actions_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb index 552eb135c..d4a12321e 100644 --- a/lib/thor/actions.rb +++ b/lib/thor/actions.rb @@ -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:: the directory to move to. # config:: give :verbose => true to log and use padding. @@ -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 @@ -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. diff --git a/spec/actions_spec.rb b/spec/actions_spec.rb index 47416e148..fe1e48356 100644 --- a/spec/actions_spec.rb +++ b/spec/actions_spec.rb @@ -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) {} From 3d5e96f5831cbdaae1ae6dd6cf023209655c558a Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Tue, 28 Jan 2020 11:01:32 -0700 Subject: [PATCH 2/2] Fix return value for Ruby < 2.6 `FileUtils.cd` only returns the value of the block in Ruby >= 2.6. --- lib/thor/actions.rb | 7 ++++--- spec/actions_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/thor/actions.rb b/lib/thor/actions.rb index d4a12321e..af5bcbd2c 100644 --- a/lib/thor/actions.rb +++ b/lib/thor/actions.rb @@ -181,12 +181,13 @@ def inside(dir = "", config = {}, &block) FileUtils.mkdir_p(destination_root) end - result = if pretend + 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 diff --git a/spec/actions_spec.rb b/spec/actions_spec.rb index fe1e48356..ffdeef265 100644 --- a/spec/actions_spec.rb +++ b/spec/actions_spec.rb @@ -174,6 +174,10 @@ def file 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