Skip to content

Commit

Permalink
CommandLine.run returns 0/1 instead of true/false
Browse files Browse the repository at this point in the history
- Closes rspec#366
  • Loading branch information
dchelimsky committed May 7, 2011
1 parent a37ff24 commit 7410e6f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 48 deletions.
7 changes: 7 additions & 0 deletions features/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### dev

[full changelog](http://github.com/rspec/rspec-core/compare/v2.6.0.rc6...master)

* Bug fixes
* CommandLine returns exit status (0/1) instead of true/false

### 2.6.0.rc6 / 2011-05-06

[full changelog](http://github.com/rspec/rspec-core/compare/v2.6.0.rc4...v2.6.0.rc6)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run(err, out)
@configuration.reporter.report(@world.example_count) do |reporter|
begin
@configuration.run_hook(:before, :suite)
@world.example_groups.map {|g| g.run(reporter)}.all?
@world.example_groups.map {|g| g.run(reporter)}.all? ? 0 : 1
ensure
@configuration.run_hook(:after, :suite)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Runner
def self.autorun
return if autorun_disabled? || installed_at_exit? || running_in_drb?
@installed_at_exit = true
at_exit { run(ARGV, $stderr, $stdout) ? exit(0) : exit(1) }
at_exit { exit(run(ARGV, $stderr, $stdout)) }
end
AT_EXIT_HOOK_BACKTRACE_LINE = "#{__FILE__}:#{__LINE__ - 2}:in `autorun'"

Expand Down
36 changes: 36 additions & 0 deletions spec/rspec/core/command_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@

module RSpec::Core
describe CommandLine do

describe "#run" do
include_context "spec files"

let(:out) { StringIO.new }
let(:err) { StringIO.new }

def config_options(argv=[])
options = RSpec::Core::ConfigurationOptions.new(argv)
options.parse_options
options
end

def command_line(args)
RSpec::Core::CommandLine.new(config_options(args))
end

def config_options(argv=[])
options = RSpec::Core::ConfigurationOptions.new(argv)
options.parse_options
options
end

it "returns 0 if spec passes" do
err, out = StringIO.new, StringIO.new
result = command_line([passing_spec_filename]).run(err, out)
result.should be(0)
end

it "returns 1 if spec passes" do
err, out = StringIO.new, StringIO.new
result = command_line([failing_spec_filename]).run(err, out)
result.should be(1)
end
end

context "given an Array of options" do
it "assigns ConfigurationOptions built from Array to @options" do
config_options = ConfigurationOptions.new(%w[--color])
Expand Down
65 changes: 19 additions & 46 deletions spec/rspec/core/drb_command_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
let(:out) { StringIO.new }
let(:err) { StringIO.new }

include_context "spec files"

def command_line(args)
RSpec::Core::DRbCommandLine.new(config_options(args))
end

def config_options(argv=[])
options = RSpec::Core::ConfigurationOptions.new(argv)
options.parse_options
options
end

def drb_command_line(args)
RSpec::Core::DRbCommandLine.new(config_options(args))
end

def run_with(args)
drb_command_line(args).run(err, out)
command_line(args).run(err, out)
end

context "without server running" do
Expand All @@ -39,14 +41,14 @@ def with_RSPEC_DRB_set_to(val)
context "without RSPEC_DRB environment variable set" do
it "defaults to 8989" do
with_RSPEC_DRB_set_to(nil) do
drb_command_line([]).drb_port.should == 8989
command_line([]).drb_port.should == 8989
end
end

it "sets the DRb port" do
with_RSPEC_DRB_set_to(nil) do
drb_command_line(["--drb-port", "1234"]).drb_port.should == 1234
drb_command_line(["--drb-port", "5678"]).drb_port.should == 5678
command_line(["--drb-port", "1234"]).drb_port.should == 1234
command_line(["--drb-port", "5678"]).drb_port.should == 5678
end
end
end
Expand All @@ -56,15 +58,15 @@ def with_RSPEC_DRB_set_to(val)
context "without config variable set" do
it "uses RSPEC_DRB value" do
with_RSPEC_DRB_set_to('9000') do
drb_command_line([]).drb_port.should == "9000"
command_line([]).drb_port.should == "9000"
end
end
end

context "and config variable set" do
it "uses configured value" do
with_RSPEC_DRB_set_to('9000') do
drb_command_line(%w[--drb-port 5678]).drb_port.should == 5678
command_line(%w[--drb-port 5678]).drb_port.should == 5678
end
end
end
Expand All @@ -80,59 +82,30 @@ def self.run(argv, err, out)
end
end

def dummy_spec_filename
@dummy_spec_filename ||= File.expand_path(File.dirname(__FILE__)) + "/_dummy_spec#{@drb_example_file_counter}.rb"
end

before(:all) do
@drb_port = 8990
@drb_example_file_counter = 0
DRb::start_service("druby://127.0.0.1:#{@drb_port}", ::FakeDrbSpecServer)
end

before(:each) do
@drb_example_file_counter += 1
create_dummy_spec_file
end

after(:each) do
File.delete(dummy_spec_filename)
end

after(:all) do
DRb::stop_service
end

def create_dummy_spec_file
File.open(dummy_spec_filename, 'w') do |f|
f.write %q{
describe "DUMMY CONTEXT for 'DrbCommandLine with -c option'" do
it "should be output with green bar" do
true.should be_true
end
it "should be output with red bar" do
raise("I want to see a red bar!")
end
end
}
end
end

it "returns true" do
it "returns 0 if spec passes" do
err, out = StringIO.new, StringIO.new
result = drb_command_line(["--drb-port", @drb_port.to_s]).run(err, out)
result.should be_true
result = command_line(["--drb-port", @drb_port.to_s, passing_spec_filename]).run(err, out)
result.should be(0)
end

it "integrates via Runner.new.run" do
it "returns 1 if spec passes" do
err, out = StringIO.new, StringIO.new
result = RSpec::Core::Runner.run(%W[ --drb --drb-port #{@drb_port} #{dummy_spec_filename}], err, out)
result.should be_false
result = command_line(["--drb-port", @drb_port.to_s, failing_spec_filename]).run(err, out)
result.should be(1)
end

def run_spec_via_druby
run_with([dummy_spec_filename, "--colour", "--drb-port", @drb_port.to_s])
run_with([failing_spec_filename, "--colour", "--drb-port", @drb_port.to_s])
out.rewind
out.read
end
Expand Down
44 changes: 44 additions & 0 deletions spec/support/spec_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
shared_context "spec files" do
def failing_spec_filename
@failing_spec_filename ||= File.expand_path(File.dirname(__FILE__)) + "/_failing_spec.rb"
end

def passing_spec_filename
@passing_spec_filename ||= File.expand_path(File.dirname(__FILE__)) + "/_passing_spec.rb"
end

def create_passing_spec_file
File.open(passing_spec_filename, 'w') do |f|
f.write %q{
describe "passing spec" do
it "passes" do
1.should eq(1)
end
end
}
end
end

def create_failing_spec_file
File.open(failing_spec_filename, 'w') do |f|
f.write %q{
describe "failing spec" do
it "fails" do
1.should eq(2)
end
end
}
end
end

before(:all) do
create_passing_spec_file
create_failing_spec_file
end

after(:all) do
File.delete(passing_spec_filename)
File.delete(failing_spec_filename)
end

end

0 comments on commit 7410e6f

Please sign in to comment.