Skip to content

Commit

Permalink
fixed bug where stories (in rails only) were loading two copies of op…
Browse files Browse the repository at this point in the history
…tions (and therefore losing the command line options, leaving only defaults).
  • Loading branch information
dchelimsky committed Mar 28, 2008
1 parent 57f9ef2 commit 438bad3
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 98 deletions.
4 changes: 4 additions & 0 deletions rspec/lib/spec/runner/formatter/profile_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def start_dump
end
@output.flush
end

def method_missing(sym, *args)
# ignore
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions rspec/lib/spec/runner/formatter/progress_bar_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def start_dump
@output.puts
@output.flush
end

def method_missing(sym, *args)
# ignore
end
end
end
end
Expand Down
13 changes: 9 additions & 4 deletions rspec/lib/spec/story/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ module Story
module Runner
class << self
def run_options # :nodoc:
@run_options ||= ::Spec::Runner::OptionParser.parse(ARGV, $stderr, $stdout)
rspec_options
# @run_options ||= ::Spec::Runner::OptionParser.parse(ARGV, $stderr, $stdout)
end

def story_runner # :nodoc:
unless @story_runner
@story_runner = StoryRunner.new(scenario_runner, world_creator)
@story_runner = create_story_runner
run_options.story_formatters.each do |formatter|
register_listener(formatter)
end
Runner.register_exit_hook
self.register_exit_hook
end
@story_runner
end
Expand All @@ -32,6 +33,10 @@ def world_creator # :nodoc:
@world_creator ||= World
end

def create_story_runner
StoryRunner.new(scenario_runner, world_creator)
end

# Use this to register a customer output formatter.
def register_listener(listener)
story_runner.add_listener(listener) # run_started, story_started, story_ended, #run_ended
Expand All @@ -40,10 +45,10 @@ def register_listener(listener)
end

def register_exit_hook # :nodoc:
puts caller(0)[1]
at_exit do
exit Runner.story_runner.run_stories unless $!
end

end

def dry_run
Expand Down
4 changes: 2 additions & 2 deletions rspec/lib/spec/story/runner/plain_text_story_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def load(path)
@story_file = path
end

def run
def run(story_runner=Spec::Story::Runner.story_runner)
raise "You must set a path to the file with the story. See the RDoc." if @story_file.nil?
mediator = Spec::Story::Runner::StoryMediator.new(steps, Spec::Story::Runner.story_runner, @options)
mediator = Spec::Story::Runner::StoryMediator.new(steps, story_runner, @options)
parser = Spec::Story::Runner::StoryParser.new(mediator)

story_text = File.read(@story_file)
Expand Down
2 changes: 1 addition & 1 deletion rspec/lib/spec/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module VERSION
TINY = 3
RELEASE_CANDIDATE = nil

BUILD_TIME_UTC = 20080325021625
BUILD_TIME_UTC = 20080328014754

STRING = [MAJOR, MINOR, TINY].join('.')
TAG = "REL_#{[MAJOR, MINOR, TINY, RELEASE_CANDIDATE].compact.join('_')}".upcase.gsub(/\.|-/, '_')
Expand Down
27 changes: 14 additions & 13 deletions rspec/spec/spec/story/runner/plain_text_story_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,51 @@ module Runner

it "should parse a story file" do
runner = PlainTextStoryRunner.new("path")

during {
runner.run
runner.run(mock('runner'))
}.expect {
@parser.should_receive(:parse).with(["this", "and that"])
}
end

it "should build up a mediator with its own steps and the singleton story_runner" do
@story_runner = mock('story runner', :null_object => true)

runner = PlainTextStoryRunner.new("path")
Spec::Story::Runner.should_receive(:story_runner).and_return(story_runner = mock("story runner"))
Spec::Story::Runner::StoryMediator.should_receive(:new).with(runner.steps, story_runner, {}).
and_return(mediator = stub("mediator", :run_stories => nil))
runner.run

Spec::Story::Runner::StoryMediator.should_receive(:new).with(
runner.steps, @story_runner, {}
).and_return(mediator = stub("mediator", :run_stories => nil))
runner.run(@story_runner)
end

it "should build up a parser with the mediator" do
runner = PlainTextStoryRunner.new("path")
Spec::Story::Runner.should_receive(:story_runner).and_return(story_runner = mock("story runner"))
Spec::Story::Runner::StoryMediator.should_receive(:new).and_return(mediator = stub("mediator", :run_stories => nil))
Spec::Story::Runner::StoryParser.should_receive(:new).with(mediator).and_return(@parser)
runner.run
runner.run(stub("story_runner"))
end

it "should tell the mediator to run the stories" do
runner = PlainTextStoryRunner.new("path")
mediator = mock("mediator")
Spec::Story::Runner::StoryMediator.should_receive(:new).and_return(mediator)
mediator.should_receive(:run_stories)
runner.run
runner.run(mock('runner'))
end

it "should accept a block instead of a path" do
runner = PlainTextStoryRunner.new do |runner|
runner.load("path/to/story")
end
File.should_receive(:read).with("path/to/story").and_return("this\nand that")
runner.run
runner.run(mock('runner'))
end

it "should tell you if you try to run with no path set" do
runner = PlainTextStoryRunner.new
lambda {
runner.run
runner.run(mock('runner'))
}.should raise_error(RuntimeError, "You must set a path to the file with the story. See the RDoc.")
end

Expand All @@ -74,7 +75,7 @@ module Runner
Spec::Story::Runner::StoryMediator.should_receive(:new).
with(anything, anything, :foo => :bar).
and_return(mediator = stub("mediator", :run_stories => nil))
runner.run
runner.run(mock('runner'))
end

it "should provide access to its options" do
Expand All @@ -83,7 +84,7 @@ module Runner
Spec::Story::Runner::StoryMediator.should_receive(:new).
with(anything, anything, :foo => :bar).
and_return(mediator = stub("mediator", :run_stories => nil))
runner.run
runner.run mock('runner')
end

end
Expand Down
4 changes: 2 additions & 2 deletions rspec/spec/spec/story/runner/story_mediator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run_stories
@runner.scenarios.first.name.should == "scenario name"
@runner.scenarios.first.story.should == @runner.stories.first
end

it "should create a given scenario step if one matches" do
pending("need to untangle the dark mysteries of the story runner - something needs to get stubbed here") do
story = @mediator.create_story "title", "narrative"
Expand Down Expand Up @@ -120,7 +120,7 @@ def run_stories
it "should pass options to the stories it creates" do
@mediator = StoryMediator.new @step_group, @runner, :foo => :bar
@mediator.create_story "story title", "story narrative"

run_stories

@runner.stories.first[:foo].should == :bar
Expand Down
131 changes: 59 additions & 72 deletions rspec/spec/spec/story/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
require File.dirname(__FILE__) + '/story_helper'
require 'spec/runner/formatter/story/plain_text_formatter'
require 'spec/runner/formatter/story/html_formatter'

module Spec
module Story
describe Runner, "module" do
def dev_null
io = StringIO.new
def io.write(str)
str.to_s.size
end
return io
end

before :each do
Kernel.stub!(:at_exit)
@stdout, $stdout = $stdout, dev_null
@argv = Array.new(ARGV)
@runner_module = Runner.dup
before(:each) do
@world_creator = World.dup
@runner_module.module_eval { @run_options = @story_runner = @scenario_runner = @world_creator = nil }
@runner_module = Runner.dup
@runner_module.instance_eval {@story_runner = nil}
@runner_module.stub!(:register_exit_hook)
end

after :each do
$stdout = @stdout
ARGV.replace @argv
@runner_module.module_eval { @run_options = @story_runner = @scenario_runner = @world_creator = nil }
def create_options(args=[])
Spec::Runner::OptionParser.parse(args, StringIO.new, StringIO.new)
end

it 'should wire up a singleton StoryRunner' do
Expand All @@ -32,75 +22,72 @@ def io.write(str)

it 'should set its options based on ARGV' do
# given
ARGV << '--dry-run'

@runner_module.should_receive(:run_options).and_return(
create_options(['--dry-run'])
)

# when
options = @runner_module.run_options

# then
options.dry_run.should be_true
end

it 'should add a reporter to the runner classes' do
# given
story_runner = mock('story runner', :null_object => true)
scenario_runner = mock('scenario runner', :null_object => true)
world_creator = mock('world', :null_object => true)

@runner_module::class_eval { @world_creator = world_creator }
@runner_module::StoryRunner.stub!(:new).and_return(story_runner)
@runner_module::ScenarioRunner.stub!(:new).and_return(scenario_runner)

# expect
world_creator.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))
story_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))
scenario_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))

# when
@runner_module.story_runner
end

it 'should add a documenter to the runner classes if one is specified' do
# given
ARGV << "--format" << "html"
story_runner = mock('story runner', :null_object => true)
scenario_runner = mock('scenario runner', :null_object => true)
world_creator = mock('world', :null_object => true)
describe "initialization" do

@runner_module::class_eval { @world_creator = world_creator }
@runner_module::StoryRunner.stub!(:new).and_return(story_runner)
@runner_module::ScenarioRunner.stub!(:new).and_return(scenario_runner)
before(:each) do
# given
@story_runner = mock('story runner', :null_object => true)
@scenario_runner = mock('scenario runner', :null_object => true)
@world_creator = mock('world', :null_object => true)

# expect
world_creator.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))
story_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))
scenario_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))
@runner_module.stub!(:world_creator).and_return(@world_creator)
@runner_module.stub!(:create_story_runner).and_return(@story_runner)
@runner_module.stub!(:scenario_runner).and_return(@scenario_runner)
end

it 'should add a reporter to the runner classes' do
@runner_module.should_receive(:run_options).and_return(
create_options
)

# when
@runner_module.story_runner
end
# expect
@world_creator.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))
@story_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))
@scenario_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::PlainTextFormatter))

# when
@runner_module.story_runner
end

it 'should add any registered listener to the runner classes' do
# given
ARGV << "--format" << "html"
story_runner = mock('story runner', :null_object => true)
scenario_runner = mock('scenario runner', :null_object => true)
world_creator = mock('world', :null_object => true)
it 'should add a documenter to the runner classes if one is specified' do

@runner_module::class_eval { @world_creator = world_creator }
@runner_module::StoryRunner.stub!(:new).and_return(story_runner)
@runner_module::ScenarioRunner.stub!(:new).and_return(scenario_runner)
@runner_module.should_receive(:run_options).and_return(
create_options(["--format","html"])
)

# expect
@world_creator.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))
@story_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))
@scenario_runner.should_receive(:add_listener).with(an_instance_of(Spec::Runner::Formatter::Story::HtmlFormatter))

listener = Object.new
# when
@runner_module.story_runner
end

it 'should add any registered listener to the runner classes' do
# given
listener = Object.new

# expect
world_creator.should_receive(:add_listener).with(listener)
story_runner.should_receive(:add_listener).with(listener)
scenario_runner.should_receive(:add_listener).with(listener)
# expect
@world_creator.should_receive(:add_listener).with(listener)
@story_runner.should_receive(:add_listener).with(listener)
@scenario_runner.should_receive(:add_listener).with(listener)

# when
@runner_module.register_listener listener
# when
@runner_module.register_listener listener
end
end
end
end
end
end
7 changes: 4 additions & 3 deletions rspec/stories/example_groups/stories.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require File.join(File.dirname(__FILE__), *%w[.. helper])

with_steps_for :running_rspec do
Dir["#{File.dirname(__FILE__)}/*"].each do |file|
run file if File.file?(file) && !(file =~ /\.rb$/)
end
run File.dirname(__FILE__) + "/nested_groups"
# Dir["#{File.dirname(__FILE__)}/*"].each do |file|
# run file if File.file?(file) && !(file =~ /\.rb$/)
# end
end
2 changes: 1 addition & 1 deletion rspec_on_rails/lib/spec/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Spec
module Rails
module VERSION #:nodoc:
BUILD_TIME_UTC = 20080325021625
BUILD_TIME_UTC = 20080328014754
end
end
end
Expand Down

0 comments on commit 438bad3

Please sign in to comment.