diff --git a/spec/exec_spec.cr b/spec/exec_spec.cr index 4e0dd92..b588790 100644 --- a/spec/exec_spec.cr +++ b/spec/exec_spec.cr @@ -37,6 +37,11 @@ describe Exec do Exec.each_line("ls spec").should eq ["exec_spec.cr\n", "spec_helper.cr\n"] end + it "ensure $? was set" do + Exec.each_line("unamea") + $?.exit_code.should eq 127 + end + it "list file with option" do Exec.each_line("ls spec", chomp: true).should eq ["exec_spec.cr", "spec_helper.cr"] end diff --git a/src/exec.cr b/src/exec.cr index a86ecc3..8caa060 100644 --- a/src/exec.cr +++ b/src/exec.cr @@ -28,12 +28,12 @@ class Exec < Process def self.each_line(command, chomp = false, &block : String ->) : Nil process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit) - output = process.output.gets_to_end - status = process.wait - $? = status - output.each_line(chomp) do |line| + process.output.each_line(chomp) do |line| yield line end + ensure + status = process.wait + $? = status end def self.each_line(command, chomp = false)