Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #31 from voxik/block-device-guard

Add block device guard.
  • Loading branch information...
commit 6cb37050bb5d8395625a9fe258f99b9f438fe355 2 parents 7c06779 + a0d2e46
@brixen brixen authored
View
1  lib/mspec/guards.rb
@@ -1,5 +1,6 @@
require 'mspec/utils/ruby_name'
require 'mspec/guards/background'
+require 'mspec/guards/block_device'
require 'mspec/guards/bug'
require 'mspec/guards/compliance'
require 'mspec/guards/conflict'
View
22 lib/mspec/guards/block_device.rb
@@ -0,0 +1,22 @@
+require 'mspec/guards/guard'
+
+class BlockDeviceGuard < SpecGuard
+ def match?
+ platform_is_not :freebsd, :windows do
+ block = `find /dev /devices -type b 2> /dev/null`
+ return !(block.nil? || block.empty?)
+ end
+
+ false
+ end
+end
+
+class Object
+ def with_block_device
+ g = BlockDeviceGuard.new
+ g.name = :with_block_device
+ yield if g.yield?
+ ensure
+ g.unregister
+ end
+end
View
46 spec/guards/block_device_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+require 'mspec/guards'
+
+describe Object, "#with_block_device" do
+ before :each do
+ ScratchPad.clear
+
+ @guard = BlockDeviceGuard.new
+ BlockDeviceGuard.stub!(:new).and_return(@guard)
+ end
+
+ platform_is_not :freebsd, :windows do
+ it "yields if block device is available" do
+ @guard.should_receive(:`).and_return("block devices")
+ with_block_device { ScratchPad.record :yield }
+ ScratchPad.recorded.should == :yield
+ end
+
+ it "does not yield if block device is not available" do
+ @guard.should_receive(:`).and_return(nil)
+ with_block_device { ScratchPad.record :yield }
+ ScratchPad.recorded.should_not == :yield
+ end
+ end
+
+ platform_is :freebsd, :windows do
+ it "does not yield, since platform does not support block devices" do
+ @guard.should_not_receive(:`)
+ with_block_device { ScratchPad.record :yield }
+ ScratchPad.recorded.should_not == :yield
+ end
+ end
+
+ it "sets the name of the guard to :with_block_device" do
+ with_block_device { }
+ @guard.name.should == :with_block_device
+ end
+
+ it "calls #unregister even when an exception is raised in the guard block" do
+ @guard.should_receive(:match?).and_return(true)
+ @guard.should_receive(:unregister)
+ lambda do
+ with_block_device { raise Exception }
+ end.should raise_error(Exception)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.