Skip to content
This repository
Browse code

Add block device guard.

  • Loading branch information...
commit 7b31bcdb9c047d93bf6f5271322fc9c39c5a012b 1 parent 7c06779
Vít Ondruch authored September 24, 2012
1  lib/mspec/guards.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'mspec/utils/ruby_name'
2 2
 require 'mspec/guards/background'
  3
+require 'mspec/guards/block_device'
3 4
 require 'mspec/guards/bug'
4 5
 require 'mspec/guards/compliance'
5 6
 require 'mspec/guards/conflict'
18  lib/mspec/guards/block_device.rb
... ...
@@ -0,0 +1,18 @@
  1
+require 'mspec/guards/guard'
  2
+
  3
+class BlockDeviceGuard < SpecGuard
  4
+  def match?
  5
+    block = `find /dev /devices -type b 2> /dev/null`
  6
+    !(block.nil? || block.empty?)
  7
+  end
  8
+end
  9
+
  10
+class Object
  11
+  def with_block_device
  12
+    g = BlockDeviceGuard.new
  13
+    g.name = :with_block_device
  14
+    yield if g.yield?
  15
+  ensure
  16
+    g.unregister
  17
+  end
  18
+end
36  spec/guards/block_device_spec.rb
... ...
@@ -0,0 +1,36 @@
  1
+require 'spec_helper'
  2
+require 'mspec/guards'
  3
+
  4
+describe Object, "#with_block_device" do
  5
+  before :each do
  6
+    ScratchPad.clear
  7
+
  8
+    @guard = BlockDeviceGuard.new
  9
+    BlockDeviceGuard.stub!(:new).and_return(@guard)
  10
+  end
  11
+
  12
+  it "yields if block device is available" do
  13
+    @guard.should_receive(:`).and_return("block devices")
  14
+    with_block_device { ScratchPad.record :yield }
  15
+    ScratchPad.recorded.should == :yield
  16
+  end
  17
+
  18
+  it "does not yield if block device is not available" do
  19
+    @guard.should_receive(:`).and_return(nil)
  20
+    with_block_device { ScratchPad.record :yield }
  21
+    ScratchPad.recorded.should_not == :yield
  22
+  end
  23
+
  24
+  it "sets the name of the guard to :with_block_device" do
  25
+    with_block_device { }
  26
+    @guard.name.should == :with_block_device
  27
+  end
  28
+
  29
+  it "calls #unregister even when an exception is raised in the guard block" do
  30
+    @guard.should_receive(:match?).and_return(true)
  31
+    @guard.should_receive(:unregister)
  32
+    lambda do
  33
+      with_block_device { raise Exception }
  34
+    end.should raise_error(Exception)
  35
+  end
  36
+end

0 notes on commit 7b31bcd

Please sign in to comment.
Something went wrong with that request. Please try again.