2013-03-02 00:20:11 +00:00
|
|
|
module UnitExampleGroup
|
|
|
|
def self.included(base)
|
|
|
|
base.metadata[:type] = :unit
|
|
|
|
base.before do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow_any_instance_of(Object).to receive(:system) { |instance, *args, &block|
|
2013-03-02 00:20:11 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
2014-03-14 14:37:50 +00:00
|
|
|
allow_any_instance_of(Object).to receive(:`) { |instance, *args, &block|
|
2013-03-03 02:20:52 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
2014-03-14 14:37:50 +00:00
|
|
|
allow_any_instance_of(Object).to receive(:exec) { |instance, *args, &block|
|
2013-03-27 00:59:25 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
2014-03-14 14:37:50 +00:00
|
|
|
allow_any_instance_of(Object).to receive(:fork) { |instance, *args, &block|
|
2013-03-27 00:59:25 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
2014-03-14 14:37:50 +00:00
|
|
|
allow_any_instance_of(Object).to receive(:spawn) { |instance, *args, &block|
|
2013-04-23 11:03:29 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
2013-03-02 03:11:29 +00:00
|
|
|
require 'vagrant/util/subprocess'
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(Vagrant::Util::Subprocess).to receive(:execute) { |*args, &block|
|
2013-03-02 00:20:11 +00:00
|
|
|
UnitExampleGroup.prevent_system_calls(*args, &block)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.prevent_system_calls(*args, &block)
|
|
|
|
args.pop if args.last.is_a?(Hash)
|
|
|
|
|
|
|
|
raise <<-MSG
|
|
|
|
Somehow your code under test is trying to execute a command on your system,
|
|
|
|
please stub it out or move your spec code to an acceptance spec.
|
|
|
|
|
2013-03-27 00:59:25 +00:00
|
|
|
Block: #{block.inspect}
|
2013-03-02 00:20:11 +00:00
|
|
|
Command: "#{args.join(' ')}"
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|