| 
									
										
										
										
											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 | 
					
						
							|  |  |  |       expect(subject.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( | 
					
						
							|  |  |  |         subject.popen_read("sh", "-c", "echo success") do |pipe| | 
					
						
							|  |  |  |           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 | 
					
						
							|  |  |  |       expect(subject.popen_read("./nonexistent", err: :out)) | 
					
						
							|  |  |  |         .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 | 
					
						
							|  |  |  |     it "with supports writing to a command's standard input" do | 
					
						
							|  |  |  |       subject.popen_write("grep", "-q", "success") do |pipe| | 
					
						
							|  |  |  |         pipe.write("success\n") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-06-10 20:12:55 +03:00
										 |  |  |       expect($CHILD_STATUS).to be_a_success | 
					
						
							| 
									
										
										
										
											2017-02-16 21:17:46 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |