Skip to content

Commit

Permalink
Show the seed in the reporter output. Make a default seed.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinko committed Oct 21, 2011
1 parent 5062363 commit 55e0181
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core/command_line.rb
Expand Up @@ -18,7 +18,7 @@ def run(err, out)
@configuration.load_spec_files
@world.announce_filters

@configuration.reporter.report(@world.example_count, @configuration.seed) do |reporter|
@configuration.reporter.report(@world.example_count, @configuration.seed_to_report) do |reporter|
begin
@configuration.run_hook(:before, :suite)
@world.example_groups.ordered.map {|g| g.run(reporter)}.all? ? 0 : @configuration.failure_exit_code
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/core/configuration.rb
Expand Up @@ -44,7 +44,7 @@ def self.add_setting(name, opts={})
add_setting :default_path, :default => 'spec'
add_setting :show_failures_in_pending_blocks, :default => false
add_setting :randomize, :default => false
add_setting :seed
add_setting :seed, :default => srand % 0xFFFF

CONDITIONAL_FILTERS = {
:if => lambda { |value, metadata| metadata.has_key?(:if) && !value },
Expand Down Expand Up @@ -462,6 +462,10 @@ def load_spec_files
raise_if_rspec_1_is_loaded
end

def seed_to_report
randomize? ? seed : nil
end

private

def assert_no_example_groups_defined(config_option)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/extensions/ordered.rb
Expand Up @@ -4,7 +4,7 @@ module Extensions
module Ordered
def ordered
if RSpec.configuration.randomize?
srand RSpec.configuration.seed if RSpec.configuration.seed
srand RSpec.configuration.seed
sort_by { rand size }
else
self
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/core/formatters/base_text_formatter.rb
Expand Up @@ -86,6 +86,11 @@ def dump_pending
end
end

def seed(number)
output.puts
output.puts "This run was randomized by the following seed: #{number}"
end

def close
output.close if IO === output && output != $stdout
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/reporter.rb
Expand Up @@ -22,7 +22,7 @@ def finish(seed)
notify :dump_pending
notify :dump_failures
notify :dump_summary, @duration, @example_count, @failure_count, @pending_count
notify :seed, seed
notify :seed, seed if seed
ensure
notify :close
end
Expand Down
18 changes: 12 additions & 6 deletions spec/command_line/randomize_spec.rb
Expand Up @@ -106,8 +106,8 @@
"""
end

def get_failures(output, number)
output.scan(/\s{1}#{number}\).+/).uniq
def get_failures(number)
all_stdout.scan(/\s{1}#{number}\).+/).uniq
end

it 'runs the example groups and examples in random order' do
Expand All @@ -116,9 +116,13 @@ def get_failures(output, number)
end

1.upto(85) do |number|
get_failures(all_stdout, number).size.should be > 1,
get_failures(number).size.should be > 1,
"Failure messages for ##{number} are the same"
end

all_stdout.should match(
/This run was randomized by the following seed: \d+/
)
end

context 'given a seed' do
Expand All @@ -127,12 +131,14 @@ def get_failures(output, number)
run_simple 'rspec spec/randomize_spec.rb --randomize --seed 123', false
end

output = only_processes.last(2).map {|p| p.stdout(@aruba_keep_ansi) }.join

1.upto(85) do |number|
get_failures(output, number).size.should eq(1),
get_failures(number).size.should eq(1),
"Failure messages for ##{number} are not the same"
end

all_stdout.should match(
/This run was randomized by the following seed: 123/
)
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -916,5 +916,26 @@ def metadata_hash(*args)
config.formatters.should be_empty
end
end

describe '#seed_to_report' do
context 'with randomize set to true' do
before do
config.randomize = true
config.seed = 123
end

it 'returns the seed' do
config.seed_to_report.should eq(123)
end
end

context 'with randomize set to false' do
before { config.randomize = false }

it 'returns nil' do
config.seed_to_report.should be_nil
end
end
end
end
end

0 comments on commit 55e0181

Please sign in to comment.