From 927c56c0248a05d2ae11513ca11659e849373c3d Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 1 Mar 2013 21:20:11 -0300 Subject: [PATCH] Safety net for running unit specs --- spec/spec_helper.rb | 2 +- spec/support/unit_example_group.rb | 24 ++++++++++++++++++++++++ spec/unit_helper.rb | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 spec/support/unit_example_group.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c525fdf..7e5d3d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,7 +13,7 @@ Bundler.require require 'rspec-spies' -Dir[File.dirname(__FILE__) + "/spec/support/**/*.rb"].each { |f| require f } +Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f } RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true diff --git a/spec/support/unit_example_group.rb b/spec/support/unit_example_group.rb new file mode 100644 index 0000000..13cd1f2 --- /dev/null +++ b/spec/support/unit_example_group.rb @@ -0,0 +1,24 @@ +module UnitExampleGroup + def self.included(base) + base.metadata[:type] = :unit + base.before do + Object.any_instance.stub(:system) { |*args, &block| + UnitExampleGroup.prevent_system_calls(*args, &block) + } + Vagrant::Util::Subprocess.stub(:execute) { |*args, &block| + 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. + +Command: "#{args.join(' ')}" + MSG + end +end diff --git a/spec/unit_helper.rb b/spec/unit_helper.rb index f3b8d6c..9104290 100644 --- a/spec/unit_helper.rb +++ b/spec/unit_helper.rb @@ -2,4 +2,8 @@ require 'spec_helper' RSpec.configure do |config| config.include RSpec::Fire + + config.include UnitExampleGroup, :type => :unit, :example_group => { + :file_path => /\bspec\/unit\// + } end