Skip to content

Commit 452cf70

Browse files
committed
fix: extend the scope of rescue Exception
- Process.output Process.code add exception: option - Fix func should call @status, not status
1 parent 2651108 commit 452cf70

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ Style/RaiseArgs:
2424

2525
Style/SpecialGlobalVars:
2626
EnforcedStyle: use_builtin_english_names
27+
28+
Lint/RescueException:
29+
Enabled: false

lib/rb/process.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def respond_to_missing?(name, include_private = false)
2525
end
2626

2727
def exit_code
28-
status.exitstatus
28+
@status.exitstatus
2929
end
3030

3131
def success?
32-
status.success?
32+
@status.success?
3333
end
3434

3535
alias_method :ok?, :success?
@@ -94,33 +94,37 @@ def self.run(*args, out: $stdout, err: $stderr, exception: false, **options)
9494
else
9595
Err.new(out_strio.string, err_strio.string, status)
9696
end
97-
rescue Errno::ENOENT => e
97+
rescue Exception => e
9898
raise e if exception
9999

100100
Err.new(nil, nil, $?)
101101
end
102102

103-
def self.output(*args, **options)
103+
def self.output(*args, exception: false, **options)
104104
stdout_reader, stdout_writer = IO.pipe
105105
pid = Process.spawn(*args, **options, out: stdout_writer)
106106
stdout_writer.close
107107

108108
pid, status = Process.wait2(pid)
109-
if status.exited? && status.success?
109+
if status.success?
110110
stdout_reader.read
111111
else
112112
nil
113113
end
114-
rescue Errno::ENOENT
114+
rescue Exception => e
115+
raise e if exception
116+
115117
nil
116118
ensure
117119
stdout_reader.close
118120
end
119121

120-
def self.code(...)
121-
pid, status = Process.wait2(spawn(...))
122+
def self.code(*args, exception: false, **options)
123+
pid, status = Process.wait2(spawn(*args, **options))
122124
status.exitstatus
123-
rescue Errno::ENOENT
124-
127
125+
rescue Exception => e
126+
raise e if exception
127+
128+
$?.exitstatus
125129
end
126130
end

spec/rb/process_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@
4242
end
4343
end
4444

45-
it "not raise by default when Errno::ENOENT" do
45+
it "not raise by default when Exception" do
4646
expect { Process.run("unamea") }.not_to raise_error
47-
expect { Process.run("unamea", exception: true) }.to raise_error
47+
expect { Process.run("unamea", exception: true) }.to raise_error Errno::ENOENT
48+
expect { Process.run("./Rakefile") }.not_to raise_error
49+
expect { Process.run("./Rakefile", exception: true) }.to raise_error Errno::EACCES
50+
51+
expect { Process.output("unamea") }.not_to raise_error
52+
expect { Process.output("unamea", exception: true) }.to raise_error Errno::ENOENT
53+
54+
expect { Process.output("unamea") }.not_to raise_error
55+
expect { Process.output("unamea", exception: true) }.to raise_error Errno::ENOENT
4856
end
4957

5058
it "get the output and not print" do

0 commit comments

Comments
 (0)