Skip to content

Commit de10e38

Browse files
committed
Extract thor_command into helper run_thor_fixture_standalone
1 parent d91e8ca commit de10e38

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spec/helper.rb

+20
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,24 @@ def silence_warnings
8080
end
8181

8282
alias silence capture
83+
84+
# Runs the fixture in a different process.
85+
# Useful to deal with exit_on_failure?, which interrupts the tests when it calls `exit`
86+
# This doesn't run on ruby 1.8.7
87+
def run_thor_fixture_standalone(fixture, command)
88+
gem_dir = File.expand_path("#{File.dirname(__FILE__)}/..")
89+
lib_path = "#{gem_dir}/lib"
90+
script_path = "#{gem_dir}/spec/fixtures/#{fixture}.thor"
91+
ruby_lib = ENV['RUBYLIB'].nil? ? lib_path : "#{lib_path}:#{ENV['RUBYLIB']}"
92+
93+
if command.is_a?(String)
94+
full_command = "ruby #{script_path} #{command}"
95+
elsif command.is_a?(Array)
96+
full_command = ['ruby', script_path] + command
97+
end
98+
99+
require 'open3'
100+
stdout, stderr, status = Open3.capture3({'RUBYLIB' => ruby_lib}, *full_command)
101+
[stdout, stderr, status]
102+
end
83103
end

spec/script_exit_status_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ def thor_command(command)
2020
end
2121

2222
it "a command that raises a Thor::Error exits with a status of 1" do
23-
expect(thor_command("error")).to eq(1)
23+
_stdout, _stderr, status = run_thor_fixture_standalone('exit_status', ['error'])
24+
expect(status.exitstatus).to eq(1)
2425
end
2526

2627
it "a command that does not raise a Thor::Error exits with a status of 0" do
27-
expect(thor_command("ok")).to eq(0)
28+
_stdout, _stderr, status = run_thor_fixture_standalone('exit_status', ['ok'])
29+
expect(status.exitstatus).to eq(0)
2830
end
2931
end if RUBY_VERSION > "1.8.7"

0 commit comments

Comments
 (0)