Class: ParallelCalabash::FeatureGrouper

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_calabash/feature_grouper.rb

Class Method Summary collapse

Class Method Details

.feature_files_in_folder(feature_dir) ⇒ Object



26
27
28
29
30
31
# File 'lib/parallel_calabash/feature_grouper.rb', line 26

def feature_files_in_folder(feature_dir)
  if File.directory?(feature_dir.first)
    files = Dir[File.join(feature_dir, "**{,/*/**}/*")].uniq
    files.grep(/\.feature$/)
  end
end

.feature_groups(feature_folder, group_size, weighing_factor = nil) ⇒ Object



6
7
8
# File 'lib/parallel_calabash/feature_grouper.rb', line 6

def feature_groups(feature_folder, group_size,weighing_factor = nil)
  weighing_factor.nil? ? feature_groups_by_feature_files(feature_folder, group_size) :  feature_groups_by_weight(feature_folder, group_size,weighing_factor)
end

.feature_groups_by_feature_files(feature_folder, group_size) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/parallel_calabash/feature_grouper.rb', line 10

def feature_groups_by_feature_files(feature_folder, group_size)
  files = feature_files_in_folder feature_folder
  min_number_files_per_group = files.size/group_size
  remaining_number_of_files = files.size % group_size
  groups = Array.new(group_size) { [] }
  groups.each do |group|
    min_number_files_per_group.times { group << files.delete_at(0) }
  end
  unless remaining_number_of_files==0
    groups[0..(remaining_number_of_files-1)].each do |group|
      group << files.delete_at(0)
    end
  end
  groups.reject(&:empty?)
end

.feature_groups_by_weight(feature_folder, group_size, weighing_factor) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/parallel_calabash/feature_grouper.rb', line 47

def feature_groups_by_weight(feature_folder, group_size, weighing_factor)
  features = features_with_weights feature_folder, weighing_factor
  feature_groups = Array.new(group_size).map{|e| e = []}
  features.each do |feature|
    feature_groups[index_of_lightest_group(feature_groups)] << feature
  end
  feature_groups.map{|group| group.map{|feature_hash| feature_hash[:feature]}}
end

.features_with_weights(feature_dir, weighing_factor) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/parallel_calabash/feature_grouper.rb', line 38

def features_with_weights(feature_dir, weighing_factor)
  files = feature_files_in_folder feature_dir
  features_and_weight = []
  files.each do |file|
    features_and_weight << {:feature => file, :weight => weight_of_feature(file, weighing_factor)}
  end
  features_and_weight
end

.index_of_lightest_group(feature_groups) ⇒ Object



56
57
58
59
# File 'lib/parallel_calabash/feature_grouper.rb', line 56

def index_of_lightest_group feature_groups
  lightest = feature_groups.min { |x, y| weight_of_group(x) <=> weight_of_group(y) }
  index = feature_groups.index(lightest)
end

.weight_of_feature(feature_file, weighing_factor) ⇒ Object



33
34
35
36
# File 'lib/parallel_calabash/feature_grouper.rb', line 33

def weight_of_feature(feature_file, weighing_factor)
  content = File.read(feature_file)
  content.scan(/#{weighing_factor}\b/).size
end

.weight_of_group(group) ⇒ Object



61
62
63
# File 'lib/parallel_calabash/feature_grouper.rb', line 61

def weight_of_group group
  group.inject(0) { |sum, b| sum + b[:weight] }
end