Skip to content

Commit

Permalink
Updated MSpec source to d2e7a635.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Oct 3, 2012
1 parent 7fac3c6 commit 1eebc60
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 21 deletions.
4 changes: 4 additions & 0 deletions mspec/lib/mspec/commands/mspec-run.rb
Expand Up @@ -34,6 +34,7 @@ def options(argv=ARGV)
options.configure { |f| load f }
options.name
options.randomize
options.repeat
options.pretend
options.background
options.interrupt
Expand Down Expand Up @@ -68,6 +69,9 @@ def options(argv=ARGV)
options.doc "\n $ mspec -g fails path/to/the_file_spec.rb"
options.doc "\n 3. To start the debugger before the spec matching 'this crashes'"
options.doc "\n $ mspec --spec-debug -S 'this crashes' path/to/the_file_spec.rb"
options.doc "\n 4. To run some specs matching 'this crashes'"
options.doc "\n $ mspec -e 'this crashes' path/to/the_file_spec.rb"

options.doc ""

patterns = options.parse argv
Expand Down
1 change: 1 addition & 0 deletions mspec/lib/mspec/commands/mspec.rb
Expand Up @@ -82,6 +82,7 @@ def options(argv=ARGV)
options.doc " ci - Run the known good specs"
options.doc " tag - Add or remove tags\n"
options.doc " mspec COMMAND -h for more options\n"
options.doc " example: $ mspec run -h\n"

options.on_extra { |o| config[:options] << o }
config[:options].concat options.parse(argv)
Expand Down
1 change: 1 addition & 0 deletions mspec/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'
Expand Down
22 changes: 22 additions & 0 deletions mspec/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
6 changes: 5 additions & 1 deletion mspec/lib/mspec/helpers/tmp.rb
Expand Up @@ -7,9 +7,13 @@

SPEC_TEMP_UNIQUIFIER = "0"

SPEC_TMEM_DIR_PID = Process.pid

at_exit do
begin
Dir.delete SPEC_TEMP_DIR if File.directory? SPEC_TEMP_DIR
if SPEC_TMEM_DIR_PID == Process.pid
Dir.delete SPEC_TEMP_DIR if File.directory? SPEC_TEMP_DIR
end
rescue SystemCallError
STDERR.puts <<-EOM
Expand Down
8 changes: 4 additions & 4 deletions mspec/lib/mspec/matchers/match_yaml.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(expected)
end

def matches?(actual)
@actual = actual
@actual = actual
clean_yaml(@actual) == clean_yaml(@expected)
end

Expand All @@ -20,11 +20,11 @@ def failure_message
def negative_failure_message
["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
end

protected

def clean_yaml(yaml)
yaml.gsub(/([^-]|^---)\s+\n/, "\\1\n")
yaml.gsub(/([^-]|^---)\s+\n/, "\\1\n").sub(/\n\.\.\.\n$/, "\n")
end

def valid_yaml?(obj)
Expand Down
34 changes: 18 additions & 16 deletions mspec/lib/mspec/runner/context.rb
Expand Up @@ -197,24 +197,26 @@ def process

if protect "before :all", pre(:all)
@examples.each do |state|
@state = state
example = state.example
MSpec.actions :before, state

if protect "before :each", pre(:each)
MSpec.clear_expectations
if example
passed = protect nil, example
MSpec.actions :example, state, example
protect nil, @expectation_missing unless MSpec.expectation? or not passed
MSpec.repeat do
@state = state
example = state.example
MSpec.actions :before, state

if protect "before :each", pre(:each)
MSpec.clear_expectations
if example
passed = protect nil, example
MSpec.actions :example, state, example
protect nil, @expectation_missing unless MSpec.expectation? or not passed
end
protect "after :each", post(:each)
protect "Mock.verify_count", @mock_verify
end
protect "after :each", post(:each)
protect "Mock.verify_count", @mock_verify
end

protect "Mock.cleanup", @mock_cleanup
MSpec.actions :after, state
@state = nil
protect "Mock.cleanup", @mock_cleanup
MSpec.actions :after, state
@state = nil
end
end
protect "after :all", post(:all)
else
Expand Down
11 changes: 11 additions & 0 deletions mspec/lib/mspec/runner/mspec.rb
Expand Up @@ -24,6 +24,7 @@ module MSpec
@features = {}
@exception = nil
@randomize = nil
@repeat = nil
@expectation = nil
@expectations = false

Expand Down Expand Up @@ -229,6 +230,16 @@ def self.randomize?
@randomize == true
end

def self.repeat=(times)
@repeat = times
end

def self.repeat
(@repeat || 1).times do
yield
end
end

def self.shuffle(ary)
return if ary.empty?

Expand Down
7 changes: 7 additions & 0 deletions mspec/lib/mspec/utils/options.rb
Expand Up @@ -385,6 +385,13 @@ def randomize
end
end

def repeat
on("-R", "--repeat", "NUMBER",
"Repeatedly run an example NUMBER times") do |o|
MSpec.repeat = o.to_i
end
end

def verbose
on("-V", "--verbose", "Output the name of each file processed") do
obj = Object.new
Expand Down
46 changes: 46 additions & 0 deletions mspec/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
24 changes: 24 additions & 0 deletions mspec/spec/utils/options_spec.rb
Expand Up @@ -1092,6 +1092,30 @@
end
end

describe "The -R, --repeat option" do
before :each do
@options, @config = new_option
@options.repeat
end

it "is enabled with #repeat" do
@options.should_receive(:on).with("-R", "--repeat", "NUMBER", an_instance_of(String))
@options.repeat
end

it "registers the MSpec repeat mode" do
["-R", "--repeat"].each do |opt|
MSpec.repeat = 1
@options.parse [opt, "10"]
repeat_count = 0
MSpec.repeat do
repeat_count += 1
end
repeat_count.should == 10
end
end
end

describe "The -V, --verbose option" do
before :each do
@options, @config = new_option
Expand Down

0 comments on commit 1eebc60

Please sign in to comment.