Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 091f776

Browse files
committed
Allow ordering specs by modification time
1 parent c6315d6 commit 091f776

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

features/command_line/order.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ order of groups at each level is randomized.
1313

1414
With `rand` you can also specify a seed.
1515

16+
The option `modification_time` will run the most recent modified files first. You can combine it with `--next-failure` to find the most recent failing spec.
17+
1618
## Example usage
1719

1820
The `defined` option is only necessary when you have `--order rand` stored in a
@@ -22,4 +24,5 @@ config file (e.g. `.rspec`) and you want to override it from the command line.
2224
--order rand
2325
--order rand:123
2426
--seed 123 # same as --order rand:123
27+
--modification_time
2528
</code></pre>

lib/rspec/core/option_parser.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ def parser(options)
5858
end
5959

6060
parser.on('--order TYPE[:SEED]', 'Run examples by the specified order type.',
61-
' [defined] examples and groups are run in the order they are defined',
62-
' [rand] randomize the order of groups and examples',
63-
' [random] alias for rand',
64-
' [random:SEED] e.g. --order random:123') do |o|
61+
' [defined] examples and groups are run in the order they are defined',
62+
' [rand] randomize the order of groups and examples',
63+
' [random] alias for rand',
64+
' [random:SEED] e.g. --order random:123',
65+
' [modification_time] run the most recently modified files first') do |o|
6566
options[:order] = o
6667
end
6768

lib/rspec/core/ordering.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ def jenkins_hash_digest(string)
5858
MAX_32_BIT = 4_294_967_295
5959
end
6060

61+
# @private
62+
# Orders items by modification time (most recent modified first).
63+
class ModificationTime
64+
def order(list)
65+
list.sort_by { |item| -File.mtime(item.metadata[:absolute_file_path]).to_i }
66+
end
67+
end
68+
6169
# @private
6270
# Orders items based on a custom block.
6371
class Custom
@@ -78,6 +86,7 @@ def initialize(configuration)
7886
@strategies = {}
7987

8088
register(:random, Random.new(configuration))
89+
register(:modification_time, ModificationTime.new)
8190

8291
identity = Identity.new
8392
register(:defined, identity)
@@ -132,6 +141,8 @@ def order=(type)
132141
:random
133142
elsif order == 'defined'
134143
:defined
144+
elsif order == 'modification_time'
145+
:modification_time
135146
end
136147

137148
register_ordering(:global, ordering_registry.fetch(ordering_name)) if ordering_name

spec/rspec/core/ordering_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ def order_with(seed)
8181
end
8282
end
8383

84+
RSpec.describe ModificationTime do
85+
require 'time'
86+
87+
before do
88+
allow(File).to receive(:mtime).with('./file_1.rb').and_return(::Time.parse('18:00'))
89+
allow(File).to receive(:mtime).with('./file_2.rb').and_return(::Time.parse('18:01'))
90+
end
91+
92+
it 'orders list by file modification time' do
93+
file_1 = instance_double(Example, :metadata => { :absolute_file_path => './file_1.rb' })
94+
file_2 = instance_double(Example, :metadata => { :absolute_file_path => './file_2.rb' })
95+
strategy = ModificationTime.new
96+
97+
expect(strategy.order([file_1, file_2])).to eq([file_2, file_1])
98+
end
99+
end
100+
84101
RSpec.describe Custom do
85102
it 'uses the block to order the list' do
86103
strategy = Custom.new(proc { |list| list.reverse })

0 commit comments

Comments
 (0)