forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_system_command.rb
62 lines (52 loc) · 1.41 KB
/
fake_system_command.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Hbc::FakeSystemCommand
def self.responses
@responses ||= {}
end
def self.expectations
@expectations ||= {}
end
def self.system_calls
@system_calls ||= Hash.new(0)
end
def self.clear
@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
fail("expected #{command.inspect} to be run #{times} times, but got #{system_calls[command]}")
end
end
end
def self.run(command, options={})
command = Hbc::SystemCommand._process_options(command, options)
unless responses.key?(command)
fail("no response faked for #{command.inspect}, faked responses are: #{responses.inspect}")
end
system_calls[command] += 1
Hbc::SystemCommand::Result.new(command, responses[command], '', 0)
end
def self.run!(command, options={})
run(command, options.merge(:must_succeed => true))
end
end
module FakeSystemCommandHooks
def after_teardown
super
Hbc::FakeSystemCommand.verify_expectations!
ensure
Hbc::FakeSystemCommand.clear
end
end
class MiniTest::Spec
include FakeSystemCommandHooks
end