| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  | require "sandbox" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 19:49:39 +02:00
										 |  |  | describe Sandbox, :needs_macos do | 
					
						
							| 
									
										
										
										
											2017-05-09 23:00:51 +02:00
										 |  |  |   define_negated_matcher :not_matching, :matching | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |   subject(:sandbox) { described_class.new } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 14:50:46 +01:00
										 |  |  |   let(:dir) { mktmpdir } | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  |   let(:file) { dir/"foo" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |   before do | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  |     skip "Sandbox not implemented." unless described_class.available? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   specify "#allow_write" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |     sandbox.allow_write file | 
					
						
							|  |  |  |     sandbox.exec "touch", file | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(file).to exist | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#exec" do | 
					
						
							|  |  |  |     it "fails when writing to file not specified with ##allow_write" do | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         sandbox.exec "touch", file | 
					
						
							| 
									
										
										
										
											2017-07-29 19:55:05 +02:00
										 |  |  |       }.to raise_error(ErrorDuringExecution) | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(file).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "complains on failure" do | 
					
						
							|  |  |  |       ENV["HOMEBREW_VERBOSE"] = "1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 16:06:07 +01:00
										 |  |  |       allow(Utils).to receive(:popen_read).and_call_original | 
					
						
							|  |  |  |       allow(Utils).to receive(:popen_read).with("syslog", any_args).and_return("foo") | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       expect { sandbox.exec "false" } | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  |         .to raise_error(ErrorDuringExecution) | 
					
						
							|  |  |  |         .and output(/foo/).to_stdout | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "ignores bogus Python error" do | 
					
						
							|  |  |  |       ENV["HOMEBREW_VERBOSE"] = "1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |       with_bogus_error = <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  |         foo | 
					
						
							|  |  |  |         Mar 17 02:55:06 sandboxd[342]: Python(49765) deny file-write-unlink /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/errors.pyc | 
					
						
							|  |  |  |         bar | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2021-09-01 16:06:07 +01:00
										 |  |  |       allow(Utils).to receive(:popen_read).and_call_original | 
					
						
							|  |  |  |       allow(Utils).to receive(:popen_read).with("syslog", any_args).and_return(with_bogus_error) | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       expect { sandbox.exec "false" } | 
					
						
							| 
									
										
										
										
											2017-02-27 17:38:07 +01:00
										 |  |  |         .to raise_error(ErrorDuringExecution) | 
					
						
							|  |  |  |         .and output(a_string_matching(/foo/).and(matching(/bar/).and(not_matching(/Python/)))).to_stdout | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |