Skip to content

Commit c3df79c

Browse files
committed
Clean up contributed merge_capture_results: idiomatic ruby
1 parent f4dc005 commit c3df79c

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

Diff for: lib/arduino_ci/host.rb

+18-8
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,31 @@ def self.which(cmd)
3030
nil
3131
end
3232

33+
# Execute a shell command and capture stdout, stderr, and status
34+
#
35+
# @see Process.spawn
36+
# @see https://docs.ruby-lang.org/en/2.0.0/Process.html#method-c-spawn
37+
# @return [Hash] with keys "stdout" (String), "stderr" (String), and "success" (bool)
3338
def self.run_and_capture(*args, **kwargs)
3439
stdout, stderr, status = Open3.capture3(*args, **kwargs)
3540
{ out: stdout, err: stderr, success: status.exitstatus.zero? }
3641
end
3742

38-
def self.merge_capture_results(args)
39-
result = { out: "", err: "", success: true }
40-
args.each do |a|
41-
result[:out] = result[:out] + a[:out]
42-
result[:err] = result[:err] + a[:err]
43-
result[:success] = a[:success] unless a[:success]
44-
end
45-
result
43+
# Merge multiple capture results into one aggregate value
44+
#
45+
# @param args [Array] Array of hashes from `run_and_capture`
46+
# @return [Hash] with keys "stdout" (String), "stderr" (String), and "success" (bool)
47+
def self.merge_capture_results(*args)
48+
{
49+
out: args.map { |a| a[:out] }.join(),
50+
err: args.map { |a| a[:err] }.join(),
51+
success: args.all? { |a| a[:success] }
52+
}
4653
end
4754

55+
# Execute a shell command
56+
#
57+
# @see system
4858
def self.run_and_output(*args, **kwargs)
4959
system(*args, **kwargs)
5060
end

Diff for: spec/host_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ def with_tmpdir(path)
7676
a1 = { out: "one", err: "ONE", success: true }
7777
a2 = { out: "two", err: "TWO", success: false }
7878
a3 = { out: "three", err: "THREE", success: true }
79-
res = ArduinoCI::Host.merge_capture_results([a1, a2, a3])
79+
res = ArduinoCI::Host.merge_capture_results(a1, a2, a3)
8080
expect(res[:out]).to eq("onetwothree")
8181
expect(res[:err]).to eq("ONETWOTHREE")
8282
expect(res[:success]).to eq(false)
8383
end
8484

8585
it "handles empty input" do
86-
res = ArduinoCI::Host.merge_capture_results([])
86+
res = ArduinoCI::Host.merge_capture_results()
8787
expect(res[:out]).to eq("")
8888
expect(res[:err]).to eq("")
8989
expect(res[:success]).to eq(true)

0 commit comments

Comments
 (0)