Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d6d0d35962
Fetching contributors…

Cannot retrieve contributors at this time

file 203 lines (164 sloc) 7.221 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
require 'spec_helper'

describe 'command line', :ui do
  let(:stderr) { StringIO.new }
  let(:stdout) { StringIO.new }

  before :all do
    write_file 'spec/order_spec.rb', """
describe 'group 1' do
specify('group 1 example 1') {}
specify('group 1 example 2') {}
specify('group 1 example 3') {}
specify('group 1 example 4') {}
specify('group 1 example 5') {}
specify('group 1 example 6') {}
specify('group 1 example 5') {}
specify('group 1 example 7') {}
specify('group 1 example 8') {}
specify('group 1 example 9') {}
specify('group 1 example 10') {}

describe 'group 1-1' do
specify('group 1-1 example 1') {}
specify('group 1-1 example 2') {}
specify('group 1-1 example 3') {}
specify('group 1-1 example 4') {}
specify('group 1-1 example 5') {}
specify('group 1-1 example 6') {}
specify('group 1-1 example 7') {}
specify('group 1-1 example 8') {}
specify('group 1-1 example 9') {}
specify('group 1-1 example 10') {}
end

describe('group 1-2') { specify('example') {} }
describe('group 1-3') { specify('example') {} }
describe('group 1-4') { specify('example') {} }
describe('group 1-5') { specify('example') {} }
describe('group 1-6') { specify('example') {} }
describe('group 1-7') { specify('example') {} }
describe('group 1-8') { specify('example') {} }
describe('group 1-9') { specify('example') {} }
describe('group 1-10') { specify('example') {} }
end

describe('group 2') { specify('example') {} }
describe('group 3') { specify('example') {} }
describe('group 4') { specify('example') {} }
describe('group 5') { specify('example') {} }
describe('group 6') { specify('example') {} }
describe('group 7') { specify('example') {} }
describe('group 8') { specify('example') {} }
describe('group 9') { specify('example') {} }
describe('group 10') { specify('example') {} }
"""

    write_file 'spec/order_error_spec.rb', """
raise 'example failure'
"""
  end

  describe '--order rand' do
    it 'runs the examples and groups in a different order each time' do
      run_command 'tmp/aruba/spec/order_spec.rb --order rand -f doc'
      RSpec.configuration.seed = srand && srand # reset seed in same process
      run_command 'tmp/aruba/spec/order_spec.rb --order rand -f doc'

      stdout.string.should match(/Randomized with seed \d+/)

      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)}
      examples('group 1') {|first_run, second_run| first_run.should_not eq(second_run)}
      examples('group 1-1') {|first_run, second_run| first_run.should_not eq(second_run)}
    end

    it 'outputs seed before files are loaded' do
      run_command_expecting_error 'tmp/aruba/spec/order_error_spec.rb --order rand -f doc'

      stdout.string.should match(/Randomized with seed \d+/)
    end
  end

  describe '--order rand:SEED' do
    it 'runs the examples and groups in the same order each time' do
      2.times { run_command 'tmp/aruba/spec/order_spec.rb --order rand:123 -f doc' }

      stdout.string.should match(/Randomized with seed 123/)

      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)}
      examples('group 1') {|first_run, second_run| first_run.should eq(second_run)}
      examples('group 1-1') {|first_run, second_run| first_run.should eq(second_run)}
    end

    it 'outputs seed before files are loaded' do
      run_command_expecting_error 'tmp/aruba/spec/order_error_spec.rb --order rand:123 -f doc'

      stdout.string.should match(/Randomized with seed 123/)
    end
  end

  describe '--seed SEED' do
    it "forces '--order rand' and runs the examples and groups in the same order each time" do
      2.times { run_command 'tmp/aruba/spec/order_spec.rb --seed 123 -f doc' }

      stdout.string.should match(/Randomized with seed \d+/)

      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)}
      examples('group 1') {|first_run, second_run| first_run.should eq(second_run)}
      examples('group 1-1') {|first_run, second_run| first_run.should eq(second_run)}
    end

    it 'outputs seed before files are loaded' do
      run_command_expecting_error 'tmp/aruba/spec/order_error_spec.rb --order rand:123 -f doc'

      stdout.string.should match(/Randomized with seed \d+/)
    end
  end

  describe '--order default on CLI with --order rand in .rspec' do
    it "overrides --order rand with --order default" do
      write_file '.rspec', '--order rand'

      run_command 'tmp/aruba/spec/order_spec.rb --order default -f doc'

      stdout.string.should_not match(/Randomized/)

      stdout.string.should match(
        /group 1.*group 1 example 1.*group 1 example 2.*group 1-1.*group 1-2.*group 2.*/m
      )
    end

    it 'does not output seed before files are loaded' do
      run_command_expecting_error 'tmp/aruba/spec/order_error_spec.rb --order default -f doc'

      stdout.string.should_not match(/Randomized/)
    end
  end

  context 'when a custom order is configured' do
    before do
      write_file 'spec/custom_order_spec.rb', """
RSpec.configure do |config|
config.order_groups_and_examples do |list|
list.sort_by { |item| item.description }
end
end

describe 'group B' do
specify('group B example D') {}
specify('group B example B') {}
specify('group B example A') {}
specify('group B example C') {}
end

describe 'group A' do
specify('group A example 1') {}
end
"""
    end

    it 'orders the groups and examples by the provided strategy' do
      run_command 'tmp/aruba/spec/custom_order_spec.rb -f doc'

      top_level_groups { |groups| groups.flatten.should eq(['group A', 'group B']) }
      examples('group B') do |examples|
        letters = examples.flatten.map { |e| e[/(.)\z/, 1] }
        letters.should eq(['A', 'B', 'C', 'D'])
      end
    end
  end

  def examples(group)
    yield split_in_half(stdout.string.scan(/^\s+#{group} example.*$/))
  end

  def top_level_groups
    yield example_groups_at_level(0)
  end

  def nested_groups
    yield example_groups_at_level(2)
  end

  def example_groups_at_level(level)
    split_in_half(stdout.string.scan(/^\s{#{level*2}}group.*$/))
  end

  def split_in_half(array)
    length, midpoint = array.length, array.length / 2
    return array.slice(0, midpoint), array.slice(midpoint, length)
  end

  def run_command(cmd)
    RSpec::Core::Runner.run(cmd.split, stderr, stdout)
    RSpec.reset
  end

  def run_command_expecting_error(cmd)
    expect { run_command(cmd) }.to raise_error
  end
end
Something went wrong with that request. Please try again.