brew/Library/Homebrew/test/utils/popen_spec.rb
Claudia 772032f18a
Add failing tests for popen_write
When using `popen_write`, the expectation is to return the
standard output of the child process.

This expectation is evident in how `safe_popen_write` is written:

```
  def self.safe_popen_write(*args, **options, &block)
    output = popen_write(*args, **options, &block)
    return output if $CHILD_STATUS.success?

    raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
  end
```

However, no code has been written to actually *obtain* that output
from the child process. The side effects of that are described in
issue #8244. [1]

[1]: https://github.com/Homebrew/brew/issues/8244

The newly-added tests reveal that `popen_write` only returns the
number 4 instead of the expected standard output.

For example, given a file `foo` with the content `Foo\n`, one test
calls `popen_write` with `cat foo -` and an input of `Bar`.
The expected output would be `Foo\nBar\n` but the actual output is
the number 4 (which is what Ruby’s `IO#write` method returns).
2020-08-30 22:29:46 +02:00

75 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require "utils/popen"
describe Utils do
describe "::popen_read" do
it "reads the standard output of a given command" do
expect(subject.popen_read("sh", "-c", "echo success").chomp).to eq("success")
expect($CHILD_STATUS).to be_a_success
end
it "can be given a block to manually read from the pipe" do
expect(
subject.popen_read("sh", "-c", "echo success") do |pipe|
pipe.read.chomp
end,
).to eq("success")
expect($CHILD_STATUS).to be_a_success
end
it "fails when the command does not exist" do
expect(subject.popen_read("./nonexistent", err: :out))
.to eq("brew: command not found: ./nonexistent\n")
expect($CHILD_STATUS).to be_a_failure
end
end
describe "::popen_write" do
let(:foo) { mktmpdir/"foo" }
before { foo.write "Foo\n" }
it "supports writing to a command's standard input" do
subject.popen_write("grep", "-q", "success") do |pipe|
pipe.write "success\n"
end
expect($CHILD_STATUS).to be_a_success
end
it "returns the command's standard output before writing" do
child_stdout = subject.popen_write("cat", foo, "-") do |pipe|
pipe.write "Bar\n"
end
expect($CHILD_STATUS).to be_a_success
expect(child_stdout).to eq <<~EOS
Foo
Bar
EOS
end
it "returns the command's standard output after writing" do
child_stdout = subject.popen_write("cat", "-", foo) do |pipe|
pipe.write "Bar\n"
end
expect($CHILD_STATUS).to be_a_success
expect(child_stdout).to eq <<~EOS
Bar
Foo
EOS
end
it "supports interleaved writing between two reads" do
child_stdout = subject.popen_write("cat", foo, "-", foo) do |pipe|
pipe.write "Bar\n"
end
expect($CHILD_STATUS).to be_a_success
expect(child_stdout).to eq <<~EOS
Foo
Bar
Foo
EOS
end
end
end