Skip to content

Commit 1a59f35

Browse files
authored
Merge pull request #692 from jonathanhefner/add-env-option-to-run
Add :env option to Actions#run
2 parents 45b95a3 + 693c2e9 commit 1a59f35

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

lib/thor/actions.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,18 @@ def run(command, config = {})
258258

259259
return if options[:pretend]
260260

261-
result = config[:capture] ? `#{command}` : system(command.to_s)
261+
env_splat = [config[:env]] if config[:env]
262262

263-
if config[:abort_on_failure]
264-
success = config[:capture] ? $?.success? : result
265-
abort unless success
263+
if config[:capture]
264+
result, status = Open3.capture2e(*env_splat, command.to_s)
265+
success = status.success?
266+
else
267+
result = system(*env_splat, command.to_s)
268+
success = result
266269
end
267270

271+
abort if config[:abort_on_failure] && !success
272+
268273
result
269274
end
270275

spec/actions_spec.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,29 +291,41 @@ def file
291291
end
292292
end
293293

294-
describe "aborting on failure" do
294+
describe "when pretending" do
295+
it "doesn't execute the command" do
296+
runner = MyCounter.new([1], %w(--pretend))
297+
expect(runner).not_to receive(:system)
298+
runner.run("ls", :verbose => false)
299+
end
300+
end
301+
302+
describe "when not capturing" do
295303
it "aborts when abort_on_failure is given and command fails" do
296304
expect { action :run, "false", :abort_on_failure => true }.to raise_error(SystemExit)
297305
end
298306

299-
it "suceeds when abort_on_failure is given and command succeeds" do
307+
it "succeeds when abort_on_failure is given and command succeeds" do
300308
expect { action :run, "true", :abort_on_failure => true }.not_to raise_error
301309
end
302310

311+
it "supports env option" do
312+
expect { action :run, "echo $BAR", :env => { "BAR" => "foo" } }.to output("foo\n").to_stdout_from_any_process
313+
end
314+
end
315+
316+
describe "when capturing" do
303317
it "aborts when abort_on_failure is given, capture is given and command fails" do
304318
expect { action :run, "false", :abort_on_failure => true, :capture => true }.to raise_error(SystemExit)
305319
end
306320

307-
it "suceeds when abort_on_failure is given and command succeeds" do
321+
it "succeeds when abort_on_failure is given and command succeeds" do
308322
expect { action :run, "true", :abort_on_failure => true, :capture => true }.not_to raise_error
309323
end
310-
end
311324

312-
describe "when pretending" do
313-
it "doesn't execute the command" do
314-
runner = MyCounter.new([1], %w(--pretend))
315-
expect(runner).not_to receive(:system)
316-
runner.run("ls", :verbose => false)
325+
it "supports env option" do
326+
silence(:stdout) do
327+
expect(runner.run "echo $BAR", :env => { "BAR" => "foo" }, :capture => true).to eq("foo\n")
328+
end
317329
end
318330
end
319331
end
@@ -367,8 +379,8 @@ def file
367379
end
368380

369381
it "captures the output when :capture is given" do
370-
expect(runner).to receive(:`).with("thor foo bar")
371-
action(:thor, "foo", "bar", :capture => true)
382+
expect(runner).to receive(:run).with("list", hash_including(:capture => true))
383+
action :thor, :list, :capture => true
372384
end
373385
end
374386
end

0 commit comments

Comments
 (0)