| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 23:56:51 +02:00
										 |  |  | require "system_command" | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 17:52:44 +02:00
										 |  |  | describe SystemCommand::Result do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |   subject(:result) { | 
					
						
							| 
									
										
										
										
											2019-10-04 09:17:26 +02:00
										 |  |  |     described_class.new([], output_array, instance_double(Process::Status, exitstatus: 0, success?: true), | 
					
						
							|  |  |  |                         secrets: []) | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |   let(:output_array) { | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [:stdout, "output\n"], | 
					
						
							|  |  |  |       [:stderr, "error\n"], | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-30 10:11:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |   describe "#to_ary" do | 
					
						
							| 
									
										
										
										
											2018-07-30 10:11:00 +02:00
										 |  |  |     it "can be destructed like `Open3.capture3`" do | 
					
						
							|  |  |  |       out, err, status = result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |       expect(out).to eq "output\n" | 
					
						
							|  |  |  |       expect(err).to eq "error\n" | 
					
						
							| 
									
										
										
										
											2018-07-30 10:11:00 +02:00
										 |  |  |       expect(status).to be_a_success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |   describe "#stdout" do | 
					
						
							|  |  |  |     it "returns the standard output" do | 
					
						
							|  |  |  |       expect(result.stdout).to eq "output\n" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |   describe "#stderr" do | 
					
						
							|  |  |  |     it "returns the standard error output" do | 
					
						
							|  |  |  |       expect(result.stderr).to eq "error\n" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#merged_output" do | 
					
						
							|  |  |  |     it "returns the combined standard and standard error output" do | 
					
						
							|  |  |  |       expect(result.merged_output).to eq "output\nerror\n" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#plist" do | 
					
						
							|  |  |  |     subject { result.plist } | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 03:09:07 +02:00
										 |  |  |     let(:output_array) { [[:stdout, stdout]] } | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |     let(:garbage) { | 
					
						
							|  |  |  |       <<~EOS | 
					
						
							|  |  |  |         Hello there! I am in no way XML am I?!?! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           That's a little silly... you were expecting XML here! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         What is a parser to do? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Hopefully <not> explode! | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |     let(:plist) { | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       <<~XML | 
					
						
							| 
									
										
										
										
											2016-08-24 00:41:50 +02:00
										 |  |  |         <?xml version="1.0" encoding="UTF-8"?> | 
					
						
							|  |  |  |         <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | 
					
						
							|  |  |  |         <plist version="1.0"> | 
					
						
							|  |  |  |         <dict> | 
					
						
							|  |  |  |           <key>system-entities</key>
 | 
					
						
							|  |  |  |           <array> | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |             <dict> | 
					
						
							|  |  |  |               <key>content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_partition_map</string>
 | 
					
						
							|  |  |  |               <key>dev-entry</key>
 | 
					
						
							|  |  |  |               <string>/dev/disk3s1</string>
 | 
					
						
							|  |  |  |               <key>potentially-mountable</key>
 | 
					
						
							|  |  |  |               <false/> | 
					
						
							|  |  |  |               <key>unmapped-content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_partition_map</string>
 | 
					
						
							|  |  |  |             </dict>
 | 
					
						
							|  |  |  |             <dict> | 
					
						
							|  |  |  |               <key>content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_partition_scheme</string>
 | 
					
						
							|  |  |  |               <key>dev-entry</key>
 | 
					
						
							|  |  |  |               <string>/dev/disk3</string>
 | 
					
						
							|  |  |  |               <key>potentially-mountable</key>
 | 
					
						
							|  |  |  |               <false/> | 
					
						
							|  |  |  |               <key>unmapped-content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_partition_scheme</string>
 | 
					
						
							|  |  |  |             </dict>
 | 
					
						
							| 
									
										
										
										
											2016-08-24 00:41:50 +02:00
										 |  |  |             <dict> | 
					
						
							|  |  |  |               <key>content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_HFS</string>
 | 
					
						
							|  |  |  |               <key>dev-entry</key>
 | 
					
						
							|  |  |  |               <string>/dev/disk3s2</string>
 | 
					
						
							|  |  |  |               <key>mount-point</key>
 | 
					
						
							|  |  |  |               <string>/private/tmp/dmg.BhfS2g</string>
 | 
					
						
							|  |  |  |               <key>potentially-mountable</key>
 | 
					
						
							|  |  |  |               <true/> | 
					
						
							|  |  |  |               <key>unmapped-content-hint</key>
 | 
					
						
							|  |  |  |               <string>Apple_HFS</string>
 | 
					
						
							|  |  |  |               <key>volume-kind</key>
 | 
					
						
							|  |  |  |               <string>hfs</string>
 | 
					
						
							|  |  |  |             </dict>
 | 
					
						
							|  |  |  |           </array>
 | 
					
						
							|  |  |  |         </dict>
 | 
					
						
							|  |  |  |         </plist>
 | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |       XML | 
					
						
							| 
									
										
										
										
											2016-08-24 00:41:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |     context "when stdout contains garbage before XML" do | 
					
						
							|  |  |  |       let(:stdout) { | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |           #{garbage} | 
					
						
							|  |  |  |           #{plist} | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |       it "ignores garbage" do | 
					
						
							|  |  |  |         expect(subject["system-entities"].length).to eq(3) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |       context "when verbose" do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |         before do | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |           allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: true)) | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |         it "warns about garbage" do | 
					
						
							|  |  |  |           expect { subject } | 
					
						
							|  |  |  |             .to output(a_string_containing(garbage)).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |     context "when stdout contains garbage after XML" do | 
					
						
							|  |  |  |       let(:stdout) { | 
					
						
							|  |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |           #{plist} | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |           #{garbage} | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |         EOS | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |       it "ignores garbage" do | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |         expect(subject["system-entities"].length).to eq(3) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       context "when verbose" do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |         before do | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |           allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: true)) | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "warns about garbage" do | 
					
						
							|  |  |  |           expect { subject } | 
					
						
							|  |  |  |             .to output(a_string_containing(garbage)).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |     context "given a hdiutil stdout" do | 
					
						
							|  |  |  |       let(:stdout) { plist } | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it "successfully parses it" do | 
					
						
							|  |  |  |         expect(subject.keys).to eq(["system-entities"]) | 
					
						
							|  |  |  |         expect(subject["system-entities"].length).to eq(3) | 
					
						
							|  |  |  |         expect(subject["system-entities"].map { |e| e["dev-entry"] }) | 
					
						
							|  |  |  |           .to eq(["/dev/disk3s1", "/dev/disk3", "/dev/disk3s2"]) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |     context "when the stdout of the command is empty" do | 
					
						
							|  |  |  |       let(:stdout) { "" } | 
					
						
							| 
									
										
										
										
											2016-08-24 00:41:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 09:31:29 +02:00
										 |  |  |       it "returns nil" do | 
					
						
							|  |  |  |         expect(subject).to be nil | 
					
						
							| 
									
										
										
										
											2017-02-28 16:31:25 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-24 00:41:50 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |