forked from grosser/parallel_tests
/
grouper.rb
79 lines (64 loc) · 2.61 KB
/
grouper.rb
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
module ParallelTests
class Grouper
class << self
def by_steps(tests, num_groups, options)
features_with_steps = group_by_features_with_steps(tests, options)
in_even_groups_by_size(features_with_steps, num_groups)
end
def by_scenarios(tests, num_groups, options={})
scenarios = group_by_scenarios(tests, options)
in_even_groups_by_size(scenarios, num_groups)
end
def in_even_groups_by_size(items, num_groups, options= {})
groups = Array.new(num_groups) { {:items => [], :size => 0} }
# add all files that should run in a single process to one group
single_process_patterns = options[:single_process] || []
single_items, items = items.partition do |item, _size|
single_process_patterns.any? { |pattern| item =~ pattern }
end
isolated_count = options[:isolate] ? (options[:isolated_count] || 1) : 0
if isolated_count >= 1
# add all files that should run in a multiple isolated processes to their own groups
group_features_by_size(items_to_group(single_items), groups[0..(isolated_count - 1)])
# group the non-isolated by size
group_features_by_size(items_to_group(items), groups[isolated_count..-1])
else
# add all files that should run in a single non-isolated process to first group
single_items.each { |item, size| add_to_group(groups.first, item, size) }
# group all by size
group_features_by_size(items_to_group(items), groups)
end
groups.map! { |g| g[:items].sort }
end
private
def largest_first(files)
files.sort_by{|_item, size| size }.reverse
end
def smallest_group(groups)
groups.min_by { |g| g[:size] }
end
def add_to_group(group, item, size)
group[:items] << item
group[:size] += size
end
def group_by_features_with_steps(tests, options)
require 'parallel_tests/cucumber/features_with_steps'
ParallelTests::Cucumber::FeaturesWithSteps.all(tests, options)
end
def group_by_scenarios(tests, options={})
require 'parallel_tests/cucumber/scenarios'
ParallelTests::Cucumber::Scenarios.all(tests, options)
end
def group_features_by_size(items, groups_to_fill)
items.each do |item, size|
size ||= 1
smallest = smallest_group(groups_to_fill)
add_to_group(smallest, item, size)
end
end
def items_to_group(items)
items.first && items.first.size == 2 ? largest_first(items) : items
end
end
end
end