2013-04-29 02:56:26 +08:00
|
|
|
class Cask::FakeSystemCommand
|
2013-10-25 03:52:55 +08:00
|
|
|
def self.responses
|
2013-05-12 12:00:26 +08:00
|
|
|
@responses ||= {}
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
|
|
|
|
2013-10-25 03:52:55 +08:00
|
|
|
def self.expectations
|
|
|
|
@expectations ||= {}
|
2013-05-12 12:00:26 +08:00
|
|
|
end
|
|
|
|
|
2013-10-25 03:52:55 +08:00
|
|
|
def self.system_calls
|
|
|
|
@system_calls ||= Hash.new(0)
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear
|
2013-10-25 03:52:55 +08:00
|
|
|
@responses = nil
|
|
|
|
@expectations = nil
|
|
|
|
@system_calls = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stubs_command(command, response='')
|
|
|
|
responses[command] = response
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expects_command(command, response='', times=1)
|
|
|
|
stubs_command(command, response)
|
|
|
|
expectations[command] = times
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.verify_expectations!
|
|
|
|
expectations.each do |command, times|
|
|
|
|
unless system_calls[command] == times
|
2014-01-26 04:32:20 +08:00
|
|
|
fail("expected #{command.inspect} to be run #{times} times, but got #{system_calls[command]}")
|
2013-10-25 03:52:55 +08:00
|
|
|
end
|
|
|
|
end
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 09:46:51 +08:00
|
|
|
def self.run(command, options={})
|
|
|
|
command = Cask::SystemCommand._process_options(command, options)
|
2013-10-25 03:52:55 +08:00
|
|
|
unless responses.key?(command)
|
|
|
|
fail("no response faked for #{command.inspect}, faked responses are: #{responses.inspect}")
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
2013-10-25 03:52:55 +08:00
|
|
|
system_calls[command] += 1
|
2013-09-23 03:44:31 +08:00
|
|
|
if options[:plist]
|
2013-10-25 03:52:55 +08:00
|
|
|
Plist::parse_xml(responses[command])
|
2013-09-23 03:44:31 +08:00
|
|
|
else
|
2013-10-25 03:52:55 +08:00
|
|
|
responses[command]
|
2013-09-23 03:44:31 +08:00
|
|
|
end
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
2013-10-21 05:48:55 +08:00
|
|
|
|
2014-02-16 01:20:38 +08:00
|
|
|
def self.run!(command, options={})
|
|
|
|
run(command, options.merge(:must_succeed => true))
|
2013-10-21 05:48:55 +08:00
|
|
|
end
|
2013-04-29 02:56:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
module FakeSystemCommandHooks
|
|
|
|
def after_teardown
|
|
|
|
super
|
2013-10-25 03:52:55 +08:00
|
|
|
Cask::FakeSystemCommand.verify_expectations!
|
|
|
|
ensure
|
2013-04-29 02:56:26 +08:00
|
|
|
Cask::FakeSystemCommand.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MiniTest::Spec
|
|
|
|
include FakeSystemCommandHooks
|
|
|
|
end
|