| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module Hbc | 
					
						
							|  |  |  |   class FakeSystemCommand | 
					
						
							|  |  |  |     def self.responses | 
					
						
							|  |  |  |       @responses ||= {} | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.expectations | 
					
						
							|  |  |  |       @expectations ||= {} | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.system_calls | 
					
						
							|  |  |  |       @system_calls ||= Hash.new(0) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.clear | 
					
						
							|  |  |  |       @responses    = nil | 
					
						
							|  |  |  |       @expectations = nil | 
					
						
							|  |  |  |       @system_calls = nil | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.stubs_command(command, response = "") | 
					
						
							|  |  |  |       responses[command] = response | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.expects_command(command, response = "", times = 1) | 
					
						
							|  |  |  |       stubs_command(command, response) | 
					
						
							|  |  |  |       expectations[command] = times | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.expect_and_pass_through(command, times = 1) | 
					
						
							|  |  |  |       pass_through = ->(cmd, opts) { Hbc::SystemCommand.run(cmd, opts) } | 
					
						
							|  |  |  |       expects_command(command, pass_through, times) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.verify_expectations! | 
					
						
							|  |  |  |       expectations.each do |command, times| | 
					
						
							|  |  |  |         unless system_calls[command] == times | 
					
						
							|  |  |  |           raise("expected #{command.inspect} to be run #{times} times, but got #{system_calls[command]}") | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.run(command_string, options = {}) | 
					
						
							|  |  |  |       command = Hbc::SystemCommand.new(command_string, options).command | 
					
						
							|  |  |  |       unless responses.key?(command) | 
					
						
							|  |  |  |         raise("no response faked for #{command.inspect}, faked responses are: #{responses.inspect}") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       system_calls[command] += 1
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       response = responses[command] | 
					
						
							|  |  |  |       if response.respond_to?(:call) | 
					
						
							|  |  |  |         response.call(command_string, options) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         Hbc::SystemCommand::Result.new(command, response, "", 0) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.run!(command, options = {}) | 
					
						
							|  |  |  |       run(command, options.merge(must_succeed: true)) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module FakeSystemCommandHooks | 
					
						
							|  |  |  |   def after_teardown | 
					
						
							|  |  |  |     super | 
					
						
							|  |  |  |     Hbc::FakeSystemCommand.verify_expectations! | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     Hbc::FakeSystemCommand.clear | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module MiniTest | 
					
						
							|  |  |  |   class Spec | 
					
						
							|  |  |  |     include FakeSystemCommandHooks | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | end |