Skip to content

Commit d801bc6

Browse files
committed
refactor: return Err Type when not success
- migrate new implementation to v0.2 branch
1 parent 1a8a82d commit d801bc6

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

lib/rb/process.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative "process/version"
66

77
module Process
8-
class Result
8+
class Err
99
attr_reader :stdout
1010
attr_reader :stderr
1111
attr_reader :status
@@ -84,7 +84,11 @@ def self.run(*args, out: $stdout, err: $stderr, **options)
8484
end
8585

8686
pid, status = Process.wait2(pid)
87-
Result.new(output_strio.string, error_strio.string, status)
87+
if status.success?
88+
output_strio.string
89+
else
90+
Err.new(output_strio.string, error_strio.string, status)
91+
end
8892
end
8993

9094
def self.output(...)

spec/rb/process_spec.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,30 @@
1212
end
1313

1414
it "get command output but also print" do
15-
r = Process.run("uname")
16-
expect(r.success?).to eq true
17-
expect(r.ok?).to eq true
18-
expect(r.stdout).to eq "Linux\n"
19-
expect(r.exit_code).to eq 0
20-
expect(r.pid).to eq r.status.pid
15+
case Process.run("uname")
16+
in String => str
17+
expect(str.chomp).to eq "Linux"
18+
in e
19+
expect(e.success?).to eq false
20+
expect(e.ok?).to eq false
21+
expect(e.stdout).to eq ""
22+
expect(e.exit_code).to not_eq(0)
23+
end
24+
end
25+
26+
it "print once Linux\n" do
27+
expect(Process.run("uname", out: $stdout)).to eq "Linux\n"
28+
end
29+
30+
it "looks like ruby Open3 when bad" do
31+
case Process.run("echo good && echo bad >&2 && exit 1")
32+
in String => str
33+
expect(str.chomp).to eq "good"
34+
in e
35+
expect(e.stdout.chomp).to eq "good"
36+
expect(e.stderr.chomp).to eq "bad"
37+
expect(e.exit_code).to eq 1
38+
end
2139
end
2240

2341
it "get the output and not print" do
@@ -33,7 +51,7 @@
3351
end
3452

3553
it "answer with cmd with ruby style" do
36-
expect(Process.run("bash") { |pipe| pipe.puts "uname" }.stdout).to eq "Linux\n"
54+
expect(Process.run("bash") { |pipe| pipe.puts "uname" }).to eq "Linux\n"
3755
end
3856

3957
it "print and also log to file" do

0 commit comments

Comments
 (0)