This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +48
-5
lines changed Expand file tree Collapse file tree 5 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,10 @@ order of groups at each level is randomized.
13
13
14
14
With ` rand ` you can also specify a seed.
15
15
16
+ Use ` recently-modified ` to run the most recently modified files first. You can
17
+ combine it with ` --only-failures ` to find the most recent failing specs. Note
18
+ that ` recently-modified ` and ` rand ` are mutually exclusive.
19
+
16
20
## Example usage
17
21
18
22
The ` defined ` option is only necessary when you have ` --order rand ` stored in a
@@ -22,4 +26,5 @@ config file (e.g. `.rspec`) and you want to override it from the command line.
22
26
--order rand
23
27
--order rand:123
24
28
--seed 123 # same as --order rand:123
29
+ --order recently-modified
25
30
</code ></pre >
Original file line number Diff line number Diff line change @@ -58,10 +58,11 @@ def parser(options)
58
58
end
59
59
60
60
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
+ ' [recently-modified] run the most recently modified files first' ) do |o |
65
66
options [ :order ] = o
66
67
end
67
68
Original file line number Diff line number Diff line change @@ -58,6 +58,14 @@ def jenkins_hash_digest(string)
58
58
MAX_32_BIT = 4_294_967_295
59
59
end
60
60
61
+ # @private
62
+ # Orders items by modification time (most recent modified first).
63
+ class RecentlyModified
64
+ def order ( list )
65
+ list . sort_by { |item | -File . mtime ( item . metadata [ :absolute_file_path ] ) . to_i }
66
+ end
67
+ end
68
+
61
69
# @private
62
70
# Orders items based on a custom block.
63
71
class Custom
@@ -77,7 +85,8 @@ def initialize(configuration)
77
85
@configuration = configuration
78
86
@strategies = { }
79
87
80
- register ( :random , Random . new ( configuration ) )
88
+ register ( :random , Random . new ( configuration ) )
89
+ register ( :recently_modified , RecentlyModified . new )
81
90
82
91
identity = Identity . new
83
92
register ( :defined , identity )
@@ -132,6 +141,8 @@ def order=(type)
132
141
:random
133
142
elsif order == 'defined'
134
143
:defined
144
+ elsif order == 'recently-modified'
145
+ :recently_modified
135
146
end
136
147
137
148
register_ordering ( :global , ordering_registry . fetch ( ordering_name ) ) if ordering_name
Original file line number Diff line number Diff line change 130
130
end
131
131
end
132
132
133
+ describe '--order rand --order recently-modified' do
134
+ it 'overrides random ordering with recently-modified option' do
135
+ 2 . times { run_command 'spec/order_spec.rb --order rand --order recently-modified -f doc' }
136
+
137
+ expect ( stdout . string ) . not_to match ( /Randomized with seed/ )
138
+
139
+ top_level_groups { |first_run , second_run | expect ( first_run ) . to eq ( second_run ) }
140
+ nested_groups { |first_run , second_run | expect ( first_run ) . to eq ( second_run ) }
141
+ end
142
+ end
143
+
133
144
describe '--order defined on CLI with --order rand in .rspec' do
134
145
after { remove '.rspec' }
135
146
Original file line number Diff line number Diff line change @@ -81,6 +81,21 @@ def order_with(seed)
81
81
end
82
82
end
83
83
84
+ RSpec . describe RecentlyModified do
85
+ before do
86
+ allow ( File ) . to receive ( :mtime ) . with ( './file_1.rb' ) . and_return ( ::Time . new )
87
+ allow ( File ) . to receive ( :mtime ) . with ( './file_2.rb' ) . and_return ( ::Time . new + 1 )
88
+ end
89
+
90
+ it 'orders list by file modification time' do
91
+ file_1 = instance_double ( Example , :metadata => { :absolute_file_path => './file_1.rb' } )
92
+ file_2 = instance_double ( Example , :metadata => { :absolute_file_path => './file_2.rb' } )
93
+ strategy = RecentlyModified . new
94
+
95
+ expect ( strategy . order ( [ file_1 , file_2 ] ) ) . to eq ( [ file_2 , file_1 ] )
96
+ end
97
+ end
98
+
84
99
RSpec . describe Custom do
85
100
it 'uses the block to order the list' do
86
101
strategy = Custom . new ( proc { |list | list . reverse } )
You can’t perform that action at this time.
0 commit comments