| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  | require "utils/popen" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Utils do | 
					
						
							|  |  |  |   describe "::popen_read" do | 
					
						
							|  |  |  |     it "reads the standard output of a given command" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.popen_read("sh", "-c", "echo success").chomp).to eq("success") | 
					
						
							| 
									
										
										
										
											2017-06-10 20:12:55 +03:00
										 |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "can be given a block to manually read from the pipe" do | 
					
						
							|  |  |  |       expect( | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |         described_class.popen_read("sh", "-c", "echo success") do |pipe| | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |           pipe.read.chomp | 
					
						
							|  |  |  |         end, | 
					
						
							|  |  |  |       ).to eq("success") | 
					
						
							| 
									
										
										
										
											2017-06-10 20:12:55 +03:00
										 |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "fails when the command does not exist" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       expect(described_class.popen_read("./nonexistent", err: :out)) | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  |         .to eq("brew: command not found: ./nonexistent\n") | 
					
						
							|  |  |  |       expect($CHILD_STATUS).to be_a_failure | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::popen_write" do | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |     let(:foo) { mktmpdir/"foo" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before { foo.write "Foo\n" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "supports writing to a command's standard input" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       described_class.popen_write("grep", "-q", "success") do |pipe| | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |         pipe.write "success\n" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns the command's standard output before writing" do | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       child_stdout = described_class.popen_write("cat", foo, "-") do |pipe| | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       child_stdout = described_class.popen_write("cat", "-", foo) do |pipe| | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2021-01-11 12:28:07 -08:00
										 |  |  |       child_stdout = described_class.popen_write("cat", foo, "-", foo) do |pipe| | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |         pipe.write "Bar\n" | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-06-10 20:12:55 +03:00
										 |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							| 
									
										
											  
											
												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
										 |  |  |       expect(child_stdout).to eq <<~EOS | 
					
						
							|  |  |  |         Foo | 
					
						
							|  |  |  |         Bar | 
					
						
							|  |  |  |         Foo | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-01-12 09:49:45 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::safe_popen_read" do | 
					
						
							|  |  |  |     it "does not raise an error if the command succeeds" do | 
					
						
							| 
									
										
										
										
											2021-01-13 11:21:10 -08:00
										 |  |  |       expect(described_class.safe_popen_read("sh", "-c", "true")).to eq("") | 
					
						
							| 
									
										
										
										
											2021-01-12 09:49:45 -08:00
										 |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the command fails" do | 
					
						
							| 
									
										
										
										
											2021-01-13 11:21:10 -08:00
										 |  |  |       expect { described_class.safe_popen_read("sh", "-c", "false") }.to raise_error(ErrorDuringExecution) | 
					
						
							| 
									
										
										
										
											2021-01-12 09:49:45 -08:00
										 |  |  |       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( | 
					
						
							| 
									
										
										
										
											2021-01-13 11:21:10 -08:00
										 |  |  |         described_class.safe_popen_write("grep", "success") { |pipe| pipe.write "success\n" }.chomp, | 
					
						
							| 
									
										
										
										
											2021-01-12 09:49:45 -08:00
										 |  |  |       ).to eq("success") | 
					
						
							|  |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the command fails" do | 
					
						
							|  |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2021-01-13 11:21:10 -08:00
										 |  |  |         described_class.safe_popen_write("grep", "success") { |pipe| pipe.write "failure\n" } | 
					
						
							| 
									
										
										
										
											2021-01-12 09:49:45 -08:00
										 |  |  |       }.to raise_error(ErrorDuringExecution) | 
					
						
							|  |  |  |       expect($CHILD_STATUS).to be_a_failure | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  | end |