From 40d85ecb4089cdc8c1163f1afa18279599804962 Mon Sep 17 00:00:00 2001 From: Seeker Date: Tue, 12 Jan 2021 09:49:45 -0800 Subject: [PATCH] popen_spec: add tests for `safe_popen_read` and `safe_popen_write` --- Library/Homebrew/test/utils/popen_spec.rb | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Library/Homebrew/test/utils/popen_spec.rb b/Library/Homebrew/test/utils/popen_spec.rb index 7ea5474e7a..990d30360b 100644 --- a/Library/Homebrew/test/utils/popen_spec.rb +++ b/Library/Homebrew/test/utils/popen_spec.rb @@ -72,4 +72,32 @@ describe Utils do EOS end end + + describe "::safe_popen_read" do + it "does not raise an error if the command succeeds" do + expect(subject.safe_popen_read("sh", "-c", "true")).to eq("") + expect($CHILD_STATUS).to be_a_success + end + + it "raises an error if the command fails" do + expect { subject.safe_popen_read("sh", "-c", "false") }.to raise_error(ErrorDuringExecution) + expect($CHILD_STATUS).to be_a_failure + end + end + + describe "::safe_popen_write" do + it "does not raise an error if the command succeeds" do + expect( + subject.safe_popen_write("grep", "success") { |pipe| pipe.write "success\n" }.chomp, + ).to eq("success") + expect($CHILD_STATUS).to be_a_success + end + + it "raises an error if the command fails" do + expect { + subject.safe_popen_write("grep", "success") { |pipe| pipe.write "failure\n" } + }.to raise_error(ErrorDuringExecution) + expect($CHILD_STATUS).to be_a_failure + end + end end