diff --git a/lib/rspec/core/configuration.rb b/lib/rspec/core/configuration.rb index ae8ba22cfe..798219bf2a 100644 --- a/lib/rspec/core/configuration.rb +++ b/lib/rspec/core/configuration.rb @@ -43,8 +43,7 @@ def self.add_setting(name, opts={}) add_setting :expecting_with_rspec add_setting :default_path, :default => 'spec' add_setting :show_failures_in_pending_blocks, :default => false - add_setting :randomize, :default => false - add_setting :seed, :default => srand % 0xFFFF + add_setting :order CONDITIONAL_FILTERS = { :if => lambda { |value, metadata| metadata.has_key?(:if) && !value }, @@ -60,12 +59,15 @@ def self.add_setting(name, opts={}) /lib\/rspec\/(core|expectations|matchers|mocks)/ ] + attr_accessor :seed + def initialize @color_enabled = false self.include_or_extend_modules = [] self.files_to_run = [] self.backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup self.exclusion_filter = CONDITIONAL_FILTERS.dup + self.seed = srand % 0xFFFF end def reset @@ -466,6 +468,16 @@ def seed_to_report randomize? ? seed : nil end + def randomize? + order ? !!order.match(/rand/) : false + end + + def orderby=(type) + order, seed = type.to_s.split(':') + self.order = order + self.seed = seed.to_i if seed + end + private def assert_no_example_groups_defined(config_option) diff --git a/lib/rspec/core/option_parser.rb b/lib/rspec/core/option_parser.rb index 9f6dee73cf..8e6e53ab74 100644 --- a/lib/rspec/core/option_parser.rb +++ b/lib/rspec/core/option_parser.rb @@ -80,14 +80,12 @@ def parser(options) parser.on('-O', '--options PATH', 'Specify the path to an options file') do |path| options[:custom_options_file] = path end - - parser.on('-s', '--seed SEED', Integer, 'Sets the seed for randomization') do |seed| - options[:seed] = seed.to_i - options[:randomize] = true - end - parser.on('--rand', '--randomize', 'Run examples in a random order') do - options[:randomize] = true + parser.on('--order TYPE', 'Run examples by the specified order type', + '[rand] randomized', + '[random] alias for rand', + 'append ":SEED" to specify a seed. Example: --order random:123') do |o| + options[:orderby] = o end parser.on('-p', '--profile', 'Enable profiling of examples with output of the top 10 slowest examples') do |o| diff --git a/spec/command_line/randomize_spec.rb b/spec/command_line/order_spec.rb similarity index 93% rename from spec/command_line/randomize_spec.rb rename to spec/command_line/order_spec.rb index 6fecdeda39..42d77a0c1e 100644 --- a/spec/command_line/randomize_spec.rb +++ b/spec/command_line/order_spec.rb @@ -2,7 +2,7 @@ describe 'command line', :ui do before :all do - write_file 'spec/randomize_spec.rb', """ + write_file 'spec/order_spec.rb', """ describe 'group 1' do specify('group 1 example 1') {} specify('group 1 example 2') {} @@ -52,9 +52,9 @@ """ end - describe '--randomize' do + describe '--order rand' do it 'runs the examples and groups in a different order each time' do - 2.times { run_command 'rspec spec/randomize_spec.rb --randomize -f doc' } + 2.times { run_command 'rspec spec/order_spec.rb --order rand -f doc' } top_level_groups {|first_run, second_run| first_run.should_not eq(second_run)} nested_groups {|first_run, second_run| first_run.should_not eq(second_run)} @@ -67,9 +67,9 @@ end end - describe '--seed' do + describe '--order rand:SEED' do it 'runs the examples and groups in the same order each time' do - 2.times { run_command 'rspec spec/randomize_spec.rb --seed 123 -f doc' } + 2.times { run_command 'rspec spec/order_spec.rb --order rand:123 -f doc' } top_level_groups {|first_run, second_run| first_run.should eq(second_run)} nested_groups {|first_run, second_run| first_run.should eq(second_run)}